Search code examples
visual-studiovisual-c++visual-studio-2012preprocessorconditional-compilation

Compile another file from the current file


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" ?


Solution

  • 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());