简体   繁体   English

从另一个.cpp文件的主体编译一个.cpp文件

[英]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. 我一直在使用Microsoft Visual Studio 2010命令提示符来编译原始.cpp文件并分析其输出的应用程序。 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: 当我执行(尝试编译example.cpp)时,出现错误:

fatal error C1043: iostream: no include path set 严重错误C1043:iostream:没有包含路径集

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. 每个system()调用都会调用一个单独的进程,因此,在该进程结束后,您在setupcppenv.bat文件中设置的所有环境变量都将被丢弃。

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. 您应该做的是,将您在.bat文件中设置的环境变量添加到系统环境中,或者至少添加到您启动应用程序的cmd实例的环境中,以便它们被进程继承由system()调用启动。

I don't know what's in setupcppenv.bat I would guess that you're making changes to environment variables in that batch file. 我不知道setupcppenv.bat什么,我想您正在对该批处理文件中的环境变量进行更改。 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. 设置将起作用的环境变量的一种方法是在程序中使用setenv()putenv()函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM