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

发布于 2024-12-07 11:34:45 字数 577 浏览 0 评论 0原文

我一直在开发一个使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凹づ凸ル 2024-12-14 11:34:45

每个 system() 调用都会调用一个单独的进程,因此一旦该进程结束,您在 setupcppenv.bat 文件中设置的任何环境变量都将被丢弃。

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

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.

太傻旳人生 2024-12-14 11:34:45

我不知道 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() or putenv() functions in your program.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文