【问题标题】:How to ask, programmatically, a compiler to compile a file in C++?如何以编程方式询问编译器以在 C++ 中编译文件?
【发布时间】:2016-07-09 02:56:48
【问题描述】:

以下是我的 C++ 程序:

main.cpp

#include <iostream>
#include <fstream>

using namespace std;

int main() {

    ofstream fileWriter;
    fileWriter.open ("firstFile.cpp");
    fileWriter << "#include <iostream>" << endl;
    fileWriter << "int main() {" << endl;
    fileWriter << "\tstd::cout << \"hello world\" << std::endl;" << endl;
    fileWriter << "\treturn 0;" << endl;
    fileWriter << "}" << endl;
    fileWriter.close();

    return 0;
}

当上述程序执行时,它会创建一个名为“firstFile.cpp”的文本文件,其中包含以下代码:

firstFile.cpp

#include <iostream>
int main() {
    std::cout << "hello world" << std::endl;
    return 0;
}

它在执行时会在屏幕上打印“hello world”。

所以,我想在 main.cpp 文件中添加几行代码,要求 GCC 编译刚刚创建的新 firstFile.cpp

我在 Ubuntu 和 Windows 平台上都使用 GNU gcc。

是否有可能通过调用编译器获得任何错误代码?如果不是为什么。

【问题讨论】:

  • system 函数可能会有所帮助。
  • 首先用g++代替gcc。 gcc 用于编译 C 源代码。 g++ 用于 C++ 源代码。
  • @secretgenes man gcc: gcc - GNU project C and C++ compiler
  • @OrangeDog:技术上正确,但实际上无关紧要。 gcc 错过了正确编译 C++ 程序所需的许多默认选项(关于链接器、包含路径等)。如果您尝试使用 gcc 编译任何重要的 C++ 程序,您通常会遇到链接器错误或(在更“复杂”的目标上)细微的故障。
  • @OrangeDog :虽然您可以使用 gcc 编译器编译 C++ 源代码,但它存在对象链接问题。 g++ 链接对象时会自动链接到标准 C++ 库中,而 gcc 不是这种情况。

标签: c++ gcc


【解决方案1】:

使用std::system 命令并不太难。另外raw string literals 允许我们插入多行文本,这对于在程序部分中键入很有用:

#include <cstdlib>
#include <fstream>

// Use raw string literal for easy coding
auto prog = R"~(

#include <iostream>

int main()
{
    std::cout << "Hello World!" << '\n';
}

)~"; // raw string literal stops here

int main()
{
    // save program to disk
    std::ofstream("prog.cpp") << prog;

    std::system("g++ -o prog prog.cpp"); // compile
    std::system("./prog"); // run
}

输出:

Hello World!

【讨论】:

  • 注意各种攻击 - 此代码运行默认的 shell 进程。不要将任何不受信任的用户输入传递给system,并注意谁可以控制环境,例如 PATH、.bashrc 等。
  • 感谢您提供有关原始字符串文字的额外信息。
  • 谢谢,我已经编写 C++ 有一段时间了,但我从不了解原始字符串文字。我认为这只是 C# 甚至 PHP 具有但 C++ 缺乏的那些方便的特性之一......很高兴被证明是错误的!
  • @CompuChip - 公平地说,它们是一个相对较新的特性(从 C++11 开始)。较旧的编译器可能不支持它们,因此如果您与使用遗留系统的人共享代码,您可能希望避免使用它们。否则,是的,它们非常方便。
  • @Galik,你是对的。事实上,我认为您应该对除 UI 元素之外的所有内容都使用正斜杠。原因仅仅是与其他操作系统的兼容性。我认为在使用 HTTP(等)URL 时避免在约定之间切换也是理想的。正斜杠也不需要转义,而反斜杠通常需要转义(因此可能存在等待错误)。
【解决方案2】:

gcc 是一个可执行文件,因此您必须使用system("gcc myfile.cpp")popen("gcc myfile.cpp"),这会为您提供文件流。

但是由于无论如何您都在生成代码,因此您甚至不需要将其写入文件。您可以使用FILE* f = popen("gcc -x ++ &lt;whatever flags&gt;") 打开 gcc 进程。然后你可以使用fwrite(f, "&lt;c++ code&gt;") 编写代码。我知道这是c 而不是真正的c++,但它可能有用。 (我认为没有popen()c++ 版本)。

【讨论】:

【解决方案3】:

您只需在创建文件后添加以下行。

system("g++ firstFile.cpp -o hello");

适用于 OS X,所以我希望它也适用于您。

【讨论】:

    【解决方案4】:

    要在源文件中使用编译器的命令行,请使用系统函数。

    语法是:

    int system (const char* command); //built in function of g++ compiler.
    

    你的情况应该是这样的

    system("g++ firstFile.cpp");
    

    PS:系统函数不会抛出异常。

    计划

    #include <iostream>
    #include <fstream>
    #include <cstdlib>
    
    using namespace std;
    
    int main() {
    
        ofstream fileWriter;
        fileWriter.open ("firstFile.cpp");
        fileWriter << "#include <iostream>" << endl;
        fileWriter << "int main() {" << endl;
        fileWriter << "\tstd::cout << \"hello world\" << std::endl;" << endl;
        fileWriter << "\treturn 0;" << endl;
        fileWriter << "}" << endl;
        fileWriter.close();
    
        system("g++ firstFile.cpp");
    
        return 0;
    }
    

    【讨论】:

    • C++ 没有其他方法可以获取异常@secretgenes
    • @Ninda :你可以在 try 块中出现异常时捕获异常,但在系统函数的情况下,不会生成异常,因此它不会抛出任何要在 catch 块中捕获的异常。
    • system 不需要cstdlib
    • @CoolGuy:我的错,它需要 cstdlib。但它在 vs2012 中运行良好,甚至不包括 cstdlib。
    • @secretgenes 有没有办法在 windows10 上做同样的事情?
    【解决方案5】:

    根据您实际想要实现的目标,您还可以考虑将embedding 一些 C++ 编译器添加到您的应用程序中。

    请注意,这远不像调用外部可执行文件那么容易,并且可能受到许可限制 (GPL) 的约束。

    另请注意,通过使用std::system 或类似机制,您可以在目标环境中添加要求,以使调用的编译器实际可用(除非您以某种方式将其与您的应用程序捆绑在一起)。

    【讨论】:

      【解决方案6】:

      类似这样的:

      #include <iostream>
      #include <fstream>
      
      using namespace std;
      
      int main() {
      
          ofstream fileWriter;
          fileWriter.open ("firstFile.cpp");
          fileWriter << "#include <iostream>" << endl;
          fileWriter << "int main() {" << endl;
          fileWriter << "\tstd::cout << \"hello world\" << std::endl;" << endl;
          fileWriter << "\treturn 0;" << endl;
          fileWriter << "}" << endl;
          fileWriter.close();
      
          system("c firstFile.cpp");
      
          return 0;
      }
      

      或任何适合您正在使用的编译器的命令。

      【讨论】:

      • 不需要system 需要cstdlib
      • @CoolGuy 是的,你需要#include 才能使用系统
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-06
      • 2015-08-05
      • 1970-01-01
      • 1970-01-01
      • 2015-03-09
      • 2012-12-12
      相关资源
      最近更新 更多