从另一个 .cpp 文件的主体编译 .cpp 文件
我一直在开发一个使用 Microsoft Visual Studio 2010 命令提示符编译原始 .cpp 文件并分析其输出的应用程序。我遇到了很多麻烦,而且网上似乎没有太多关于此的材料。这是麻烦的代码:
#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());
当我执行(它尝试编译 example.cpp)时,我收到错误:
致命错误 C1043:iostream:未设置包含路径
我对批处理文件或使用命令提示符编译器不太有经验。我做错了什么?!
此外,是否有其他方法从应用程序内部进行编译?
谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
每个
system()
调用都会调用一个单独的进程,因此一旦该进程结束,您在setupcppenv.bat
文件中设置的任何环境变量都将被丢弃。相反,您应该做的是将您在 .bat 文件中设置的环境变量添加到系统环境中,或者至少添加到您启动应用程序的 cmd 实例的环境中,以便进程继承它们由
system()
调用启动。Each
system()
call invokes a separate process, so any environment variables you set in yoursetupcppenv.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.我不知道 setupcppenv.bat 中有什么,我猜您正在更改该批处理文件中的环境变量。发生的情况是,当批处理脚本结束时,这些环境变量更改将会丢失,因为它们仅限于批处理脚本的进程以及该进程的任何子进程。
设置有效的环境变量的一种方法是在程序中使用
setenv()
或putenv()
函数。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()
orputenv()
functions in your program.