1

I am using Visual Studio 2012.
I have a Win32 Console project containing the source file "1.cpp". I want to compile some other C++ file (let's say "2.cpp") from within the source code of "1.cpp".
I tried this-:

int main()
{
 system("C:\\\"Program Files (x86)\"\\\"Microsoft Visual Studio 11.0\"\\VC\\vcvarsall.bat");
 system("C:\\\"Program Files (x86)\"\\\"Microsoft Visual Studio 11.0\"\\VC\\bin\\cl.exe /EHsc 2.cpp");
} 

I am doing this because I want to compile "2.cpp" using the /D option to define a macro inside "2.cpp" whose value is calculate within "1.cpp".
Something like this -:

int main()
{
 string mystring;
 system("C:\\\"Program Files (x86)\"\\\"Microsoft Visual Studio 11.0\"\\VC\\vcvarsall.bat");
 system(("C:\\\"Program Files (x86)\"\\\"Microsoft Visual Studio 11.0\"\\VC\\bin\\cl.exe /EHsc /DMYMACRO="+mystring+" 2.cpp").c_str());
}  

Despite executing "vcvarsall.bat" before invoking "cl.exe", I am still getting an error saying "The program can't start because mspdb110.dll is missing from your computer. Try reinstalling the program to fix this problem."

As mentioned here, I might need to set some environment variables.
Is there any method of doing this without the need to set environment variables ? If no, then how can I set those variables from within the source "1.cpp" ?

Community
  • 1
  • 1
Anmol Singh Jaggi
  • 8,376
  • 4
  • 36
  • 77

2 Answers2

2

I found an easy way of doing this -:

system(("C:\\\"Program Files...vcvarsall.bat && C:\\\"Program Files...cl.exe /EHsc /DMYMACRO="+mystring+" 2.cpp").c_str());
Anmol Singh Jaggi
  • 8,376
  • 4
  • 36
  • 77
1

Each call to "system" spawns a new process. The call to cvarsall in one process doesn’t affect the other process.

You need to create a batch file with both commands and call "system" for

cmd /c mybat.bat

harper
  • 13,345
  • 8
  • 56
  • 105
  • I don't know batch programming. Could you please show exactly how to convert the `system()` commands into their corresponding batch commands ? In other words, what will be the exact contents of "mybat.bat" ? – Anmol Singh Jaggi Jun 06 '14 at 18:00
  • 1
    Will it be something like [this](http://stackoverflow.com/a/87052/1925388) ? If yes, then I guess I'll need to create this batch file from within the program using file handling, and then invoke it using `system("cmd /c mybat.bat")`. Or do you mean something else ? – Anmol Singh Jaggi Jun 06 '14 at 18:11