在c中使用gcc进行动态文件编译
我正在尝试从程序中找到一种方法,检查一个或多个“ .c”文件的存在并加载一个或多个功能。
基本上,我将拥有一个“主”程序,该程序将检查是否存在“ script.c”文件,并将启动(如果存在)该文件的主要()函数。
这是我的“主要”程序的内容:
int main(int argc, char *argv[]){
...
if(argc == 1){
FILE *file;
if((file = fopen("script.c", "r"))){
printf("file script.c loaded.");
// to compile the script.c file and modify on it the "main()" function to "_main()" (in order to be able to recompile it with the main library) before restarting the program
int ret = system("gcc -c script.c && objcopy --redefine-sym main=_main script.o && gcc main script.o -o main && ./main -l script.o");
printf("ret(0)=%d\n", ret);
}
else{
int ret = system("./main -l");
printf("ret(1)=%d\n", ret);
}
}
else{
if(argc == 3 && strcmp(argv[2], "script.o") == 0){
_main(argc, argv);
}
else{
printf("no file found.\n");
}
}
...
}
他是我的“脚本”文件的内容:
int main(int argc, char *argv[]){
...
printf("main() function in script.c loaded.\n");
...
}
如果存在脚本。ClunMain应该给出:
file script.c loaded.
ret(0)=0
main() function in script.c loaded.
如果脚本.c文件不存在,则运行主要应该给出:
file script.c loaded.
ret(1)=0
no file found.
显然,这是出于多种原因而起作用的。
- 使用“主”程序重新编译脚本。
- 不可能 第一次发布,如果没有找到脚本,也有可能在第二次发射)
您是否有想法可以实现自己的目标? 因此,从单个可执行程序(此处为“ main”)可以检查外部文件的存在(shere“ script.c”)并从中启动一个或多个函数... PS:在其他项目中已经看过它,我知道这是可能的,但是我找不到解决方案。 PS2:只有可执行文件(main)和可能存在脚本。将被打开和执行)
I'm trying to find a method to, from a program, check the presence of one or more ".c" files and load one or more functions of it.
Basically, I would have a "main" program which will check if the "script.c" file exists, and will launch (if it exists) the main() function of this one.
Here is the content of my "main" program before compilation:
int main(int argc, char *argv[]){
...
if(argc == 1){
FILE *file;
if((file = fopen("script.c", "r"))){
printf("file script.c loaded.");
// to compile the script.c file and modify on it the "main()" function to "_main()" (in order to be able to recompile it with the main library) before restarting the program
int ret = system("gcc -c script.c && objcopy --redefine-sym main=_main script.o && gcc main script.o -o main && ./main -l script.o");
printf("ret(0)=%d\n", ret);
}
else{
int ret = system("./main -l");
printf("ret(1)=%d\n", ret);
}
}
else{
if(argc == 3 && strcmp(argv[2], "script.o") == 0){
_main(argc, argv);
}
else{
printf("no file found.\n");
}
}
...
}
He is the content of my "script.c" file:
int main(int argc, char *argv[]){
...
printf("main() function in script.c loaded.\n");
...
}
If the script.c file exists, running main should give:
file script.c loaded.
ret(0)=0
main() function in script.c loaded.
If the script.c file does not exist, running main should give:
file script.c loaded.
ret(1)=0
no file found.
Obviously, this does not work for several reasons.
- It is impossible to use the "main" program to recompile the script.o file (especially since this is supposed to be in use)
- It is impossible to compile my "main" program with a _main() function that does not exist (on the 1st launch, and potentially on the second too if script.c dont found)
Do you have an idea for me to achieve my goal ?
So from a single executable program (here "main") to be able to check the presence of an external file (here "script.c") and launch one or more functions from it...
PS: Having already seen it in other projects, I know it's possible, but I can't find the solution.
PS2: Only the executable file (main) and potentially the script.c file must be present (therefore no main.c...which therefore perhaps suggests that a "main" file should be merged with the "main.o" associated which would be unpacked and executed)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我将做出一个假设您提到的 main 函数并不是最重要的,并展示了如何从程序内部将函数集合编译到您加载的共享库中,然后执行函数(除了
main
) 中。一个简单的驱动程序可能如下所示:
您需要链接上面的程序带有 dl 库,所以类似这样的东西应该可以工作:
现在,如果您创建一些示例文件:
然后运行:
如果一切顺利,它应该产生以下输出:
我还做了一个测试如果我将
main
放入库中,看看效果如何。也就是说,将start_func_t
签名更改为:并加载并调用
main
:并稍微更改测试程序:
这也工作得很好。然而,
main
有点特殊,我不确定这是否违反了任何规则。I'm going to make an assumption that the
main
function you mentioned is not the most important and show how you can, from within your program, compile collections of functions into shared libraries that you load and then execute functions (other thanmain
) in.A simple driver could look like this:
You need to link the above program with the
dl
library, so something like this should work:Now, if you create some example files:
and then run:
It should produce this output if everything works out:
I also made a test to see how this works out if I put
main
in the library. That is, change thestart_func_t
signature to:and load and call
main
instead:and change the test programs slightly:
and this worked fine too. However,
main
is a bit special and I'm not sure if this violates any rules.