代码之家  ›  专栏  ›  技术社区  ›  itsaboutcode

使用gcc在另一个程序中编译程序

c
  •  2
  • itsaboutcode  · 技术社区  · 15 年前

    从命令行中,我得到了文件名,我必须使用gcc编译它。就这么说吧。

    ./a.out fileToBeCompiled.c
    

    那么,我如何在我的程序中使用gcc编译这个文件呢?比如说主控系统,它的输出是A。

    5 回复  |  直到 13 年前
        1
  •  2
  •   monksy    15 年前

    您总是可以调用gcc从程序中的shell执行命令进行编译。

    Reference to the system function.

        2
  •  5
  •   stimms    15 年前

    只需从您的程序中执行gcc。

    int main(int argc, char** argv)
    {
       execv("/usr/bin/gcc", argv);
    }
    
        3
  •  2
  •   D.Shawley    15 年前

    学习如何打电话 fork() , exec() wait() . 你可能想用 execv() 为此目的。

        4
  •  0
  •   pmg    15 年前
    ./a.out fileToBeCompiled.c && gcc fileToBeCompiled.c
    

    如果 ./a.out 失败(它返回0以外的内容)gcc将不被调用

        5
  •  0
  •   Mohanraj    13 年前

    简单地说,你可以使用函数“Stand”(在STDIO .H中定义)来执行C、C++程序中的UNIX命令。

    例如:我将在main.c内部编译sub.c

    void main(void)
    {
      system("cc sub.c -o sub");
    }
    

    可以指定动态输入和输出文件。