简体   繁体   English

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

[英]Compiling program within another program using gcc

From command line I am getting file name which I have to compile using gcc. 从命令行我得到文件名,我必须使用gcc编译。 lets say its like this. 让我们这样说吧。

./a.out fileToBeCompiled.c

Then how I can compile this file using gcc within my program? 那我怎么可以在我的程序中使用gcc编译这个文件? lets say main.c for which a.out is being made. 让我们说main.c正在制作a.out。

Just exec gcc from within you program. 只需从你的程序中执行gcc。

int main(int argc, char** argv)
{
   execv("/usr/bin/gcc", argv);
}

You can always call gcc to compile from an shell execution command within your program. 您始终可以从程序中的shell执行命令调用gcc进行编译。

Reference to the system function. 参考系统功能。

Learn how to call fork() , exec() , and wait() . 学习如何调用fork()exec()wait() You probably want to use execv() for this purpose. 您可能希望使用execv()来实现此目的。

Simple, you can use the function "system"(defined inside stdio.h) to execute the unix command inside the c, c++ programs. 很简单,您可以使用函数“system”(在stdio.h中定义)来执行c,c ++程序中的unix命令。

For Example: I am going to compile sub.c inside main.c 例如:我将在main.c中编译sub.c

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

You can specify the dynamic input and output files. 您可以指定动态输入和输出文件。

./a.out fileToBeCompiled.c && gcc fileToBeCompiled.c

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

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

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