Compiling a .cpp file from the body of another .cpp file - c++

I've been working on an application that compiles raw .cpp files and analyzes their outputs, using Microsoft Visual Studio 2010 Command Prompt. I'm having a lot of trouble, and there doesn't seem to be much material about this online. Here's the troublesome code:
#include <iostream>
using namespace std;
...
string name = "cl /EHsc ";
name += "example.cpp";
system("setupcppenv.bat"); // A short batch file I wrote to launch the VC++ cmd prompt without launching another instance of cmd
system(name.c_str());
When I execute (it attempts to compile example.cpp), I get an error:
fatal error C1043: iostream: no include path set
I'm not very experienced with batch files, or using the command prompt compiler. What am I doing wrong?!
Additionally, is there a different way to compile from inside an application?
Thanks!

Each system() call invokes a separate process, so any environment variables you set in your setupcppenv.bat file will be discarded once that process ends.
What you should do instead, is to add the environment variables you are setting in your .bat file to the system environment, or at least to the environment of the cmd instance from where you launch your application, so that they are inherited by the process started by the system() call.

I don't know what's in setupcppenv.bat I would guess that you're making changes to environment variables in that batch file. What happens is that when the batch script ends, those environment variable changes are being lost becuase they're confined to the batch script's process and any children of that process.
A way to set environment variables that will work is to use the setenv() or putenv() functions in your program.

Related

How can I pass commands to the elevated application in a batch file?

I was wondering if anyone can help me. I'm currently working on a game engine project which involves its own c++ compiler. For this I'm "borrowing" the visual studio 2013 c++ compiler. everything works fine. The problem I am having is a cant figure out how I would pass commands to the elevated program in a batch file.
Let me Explain, right now I am using a program which calls the "vcvarsall.bat" file and passes in "x86" as a parameter. This is great for manual entry as it then allows me to input the commands to compile files. E.G "cl /EHsc <cpp files>"
As of now, when I add commands after I call "vcvarsall.bat", they just give me a command reference error saying the command is not recognized.
What I want to achieve is being able to call one bat file which executes and compiles all of my code for me. instead of having to manually type in the commands every time. This way the entire process is easier for the user.
Any help would be appreciated,
Thank you in advance!
when I add commands after I call "vcvarsall.bat"
Maybe it has been too long since I last did a batch file .. hope the following gets you started:
I think any .bat file will accept parameters, and internally, the .bat writer (i.e. you) uses special identifiers. Often they are named something like %1 and %2, etc. (some scripting languages use $1, and probably a few other approaches)
Without consuming these parameters in your .bat file, the command line interpreter tries to use the parameter as another command (so you get 'command not recognized')
Search all .bat files on your system for %1 (or $1 or whatever) ... maybe you'll find enough hints.
Thank you all for the help, the way I solved the problem was by finding the last batch file which was called and making the end of the file call another batch file in the main compile directory, this means I can programatically generate this batch file making it incredibly easy to generate custom compilations. thank you all,
Alister

Not reading file from environment path

I have create a project in which I am trying to generate a .txt file and then calling OS specific commands (I'm using unix so using system() function to trigger application) to open it in default application.
My code works perfect.
But the problem starts when I try to run my application after setting it in environment path. I am doing that for global visibility. File gets generated correctly by while opening the .txt using system() seems like it is trying to look at it at different directory.
I want to some how convey to the system() function to look at the specific location.
How can I solve this problem?
It sounds like you are actually just in a different working directory. Try showing the result of getcwd.

(VS2010 C++) Execute a command every time the program is run?

The IDE I'm using is VS2010 for writing C++
I want to execute the command cmd C:\utilities\unix\tail.exe -f -n15 $(ProjectDir)Log.txt every time the program I'm coding is run from within the IDE. (This command should open a console to track changes made to the file Log.txt)
There are ways to make a command run every time the program is built, but I can't find a way to make a command run whenever the program itself is run, even if it's already built. Where or how might I be able to set that kind of thing up?
I've tried putting $(TargetPath) & C:\utilities\unix\tail.exe -f -n15 $(ProjectDir)Log.txt into the project's Properties->Debugging->Command (TargetPath is the full name of the debug executable) but VS reads the entire thing as a filename and gets confused.
You can create a file run.cmd for example next to the vcxproj file, which would contain:
%1
C:\utilities\unix\tail.exe -f -n15 %2Log.txt
And then in Properties->Debugging->Command you write:
$(ProjectDir)\run.cmd
and in Command Arguments you write:
"$(TargetPath)" "$(ProjectDir)"
I may have misspelled the macros, but you get the idea: it executes first your program and then whatever you want.
Edit: Unfortunately it works only if you start without debugging (Ctrl+F5), because otherwise the debugger tries to attach to run.cmd and complains that the format is unsupported.

compiled fortran code looks for input parameter file in the incorrect directory

I have a compiled fortran 90 code "NewSourceID.exe"in folder E:\TROUBLESHOOT. This uses input file MAIN.IN in the same folder. I use a batch script run_sa.BAT in the same folder E:\TROUBLESHOOT to run this executable. This batch script is generated at run time by another VB code (this is a requirement and cannot be done away with) and the batch script reads as following.
"E:\TROUBLESHOOT\NewSourceID.exe" "E:\TROUBLESHOOT\MAIN.IN".
There are two scenarios
1. When I go to the folder E:\TROUBLESHOOT and double click the batch script run_sa.BAT the NewSourceID.exe runs correctly without any problem. It runs on the command prompt window showing the path C:\WINDOWS\system32\command.exe.
When I run the same from the VB script by generating the batch script at runtime I get the following error.
"
C:\Documents and Settings\epsuser\My Documents>"E:\TROUBLESHOOT\NewSourceID.exe"
"E:\TROUBLESHOOT\MAIN.IN"
forrtl: severe (29): file not found, unit 31, file C:\Documents and Settings\eps
user\My Documents\MAIN.IN
The code tries to find the input file MAIN.IN on the path C:\Documents and Settings\epsuser\My Documents\MAIN.IN which is not the correct path to look for the file.
This happened when I replaced the NewSourceID.exe with a modified one. Earlier the code used to run correctly even from the VB with the following path. C:\WINDOWS\system32\command.exe -E:\TROUBLESHOOT\run_sa.BAT. How can this be done?
Are you sure, the Fortran program NewSourceID reads the command line argument you pass to it? Especially older Fortran programs (before Fortran 2003) had no standard way to parse command line arguments. I guess, the name MAIN.IN is hardwired in the code you use, and it always uses the MAIN.IN file from the current directory. You could work around this by issuing a change directory command before executing the program. I am not very familiar with Windows, but something like
cd E:\TROUBLESHOOT
E:\TROUBLESHOOT\NewSourceID.exe
in your batch script would probably work.
Alternatively, you could implement proper command line argument parsing in your Fortran code, using the command_argument_count() and get_command_argument() functions. You would need a Fortran 2003 compiler for that.

Setting a VS2010 environment variable from a batch file?

I'm using a batch file to define some variables that will be used in my program.
I want the batch file to change the environment variable and use it in my code , but it's just now working - the macro is not being changed.
to be more specific and clear :
I have a program that creates a DLL and sets it's version
In the common setting of the project - I created a new macro (Common properties->User macros) : TEST_VER = 5
now I want to add a batch file , that will run in the pre-build command and change the value of TESTER
I wrote this in the batch file:
set TEST_VER=9
and used the path of the batch in the pre-build.
BUT it doesn't recognize it.
and still uses 5 as the value
I though doing :
propeties of the project - > resourcses ->general
and add : TEST_VER=$(TEST_VER)
and still didn't work
is there a way to do it??
thanks!!
When Visual Studio starts a program, it runs that program in a new sub-process. In this case, that's a new CMD.EXE, the command prompt shell. Changes made to the environment in a sub-process, a child, have no effect on the parent. Visual Studio has its own set of environment variables which it inherited when it started. Your batch file can't change those values. You can't do what you want the way you're doing it.