开发者

Compiling a .cpp file from the body of another .cpp file

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜