简体   繁体   English

使用 VC++ 编译生成的 C++ 代码

[英]Compile Generated C++ Code Using VC++

I need to write a program that generate c++ code, compile it, and run it, so I finished the code generation part, and wrote it to a file called GeneratedCode.cpp.我需要编写一个生成c++代码的程序,编译并运行它,所以我完成了代码生成部分,并将其写入了一个名为GeneratedCode.cpp的文件。 Now I'm stuck at the compile part, I wrote:现在我被困在编译部分,我写道:

system(R"(call "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat")");
system(R"(call "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin\vcvars32.bat")");
system("cl GeneratedCode.cpp");

First of all, is this the right way to do this?首先,这是正确的方法吗? Info about this subject is quite scarce, especially for VC++.关于这个主题的信息非常稀少,尤其是对于 VC++。 If it is, then I have problems both when using an IDE or not.如果是,那么无论是否使用 IDE,我都会遇到问题。

When using an IDE it gives me: "GeneratedCode.cpp(2): fatal error C1034: iostream: no include path set".使用 IDE 时,它给了我:“GeneratedCode.cpp(2): fatal error C1034: iostream: no include path set”。 I'm using iostream header of course.我当然使用 iostream 标头。

When running the exe directly, it gives me: " 'cl' is not recognized as an internal or external command, operable program or batch file".直接运行 exe 时,它​​给我:“'cl' 不是内部或外部命令,也不是可运行的程序或批处理文件”。 Even though I called vcvarsall.bat and vcvars32.bat.即使我调用了 vcvarsall.bat 和 vcvars32.bat。

Each call to system or _wsystem creates a new environment which is lost when the call terminates.system_wsystem每次调用_wsystem创建一个新环境,该环境在调用终止时丢失。 If your program requires environment variables to be set up like the Visual C++ Command Prompt's LIB , INCLUDE , etc. then you aren't going to succeed by calling system multiple times.如果您的程序需要像 Visual C++ 命令提示符的LIBINCLUDE等那样设置环境变量,那么多次调用system是不会成功的。

Here are your options, in the order that I would recommend them:以下是您的选择,按照我推荐的顺序:

  1. Eschew system all together.一起避开system Set up the required environment block for cl.exe to work properly with string concatenation functions, as demonstrated here , and call CreateProcess with that environment.建立了必要的环境块cl.exe至正常工作与字符串连接功能,这表现在这里,和呼叫CreateProcess与环境。
  2. Have your program write a batch file that contains all the prerequisite commands for cl.exe to work properly, call system on that, then delete the batch file.让您的程序编写一个批处理文件,其中包含cl.exe正常工作的所有先决命令,调用system ,然后删除批处理文件。
  3. Chain the commands together with && and call system once with a big string of multiple commands.将命令与&&链接在一起,并使用&&多个命令调用system一次。

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

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