简体   繁体   English

如何使用 system() 函数 (MSVC) 编译 C++ 代码?

[英]How to compile c++ code with system() function (MSVC)?

i am trying to compile a c++ file with system() function using c++.我正在尝试使用 C++ 编译带有 system() 函数的 C++ 文件。

void CodeGenerator::GenerateExe(ProgramNode* parsed_tree, std::string folder_path, std::string file_name)
{
    std::string cpp_name = folder_path + file_name + ".cpp";

    GenerateCppFile(parsed_tree, cpp_name);
    std::string command;
    command += "cl ";
    command += cpp_name + " /OUT ";
    command += folder_path;

    system(command.c_str());
}

I know I can do that in developer cmd, but the system() function opens regular cmd, so it cannot recognize the "cl" command.我知道我可以在开发人员 cmd 中执行此操作,但是 system() 函数会打开常规 cmd,因此它无法识别“cl”命令。

Please tell how to open developer cmd or how to setup the environtment to compile the code via c++.请告诉如何打开开发人员cmd或如何设置环境以通过c ++编译代码。

You can do this by adding the path of cl to the environment variables:您可以通过将cl的路径添加到环境变量中来做到这一点:

This can be done like so:这可以像这样完成:

  • Open a run window, you can do this by typing run in search.打开一个运行窗口,您可以通过在搜索中键入run来执行此操作。
  • type sysdm.cpl输入sysdm.cpl
  • Choose Advanced separator选择高级分隔符
  • Click environment variables点击环境变量
  • In system variables select path and click edit在系统变量中选择path并单击edit
  • Click new and add the path of your cl command.单击new并添加cl命令的路径。 In my case the cl.exe file is in在我的情况下, cl.exe文件在

C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\VC\\Tools\\MSVC\\14.24.28314\\bin\\Hostx64\\x64

It doesn't vary all that much but you must use the correct path in your machine.它变化不大,但您必须在机器中使用正确的路径。

  • After this you must open a new cmd window for the changes can be detected and run the cl command from any place in your machine.在此之后,您必须打开一个新的 cmd 窗口,以便可以检测到更改并从您机器的任何位置运行cl命令。

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

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