我正在尝试编译一个程序,以便它从不同的入口点启动。我正在使用 Ubuntu 20.04.5、GCC 和 G++ 9.4.0 运行 WSL1
我发现将标志添加-Wl,--entry=foo到编译器中会foo()作为入口函数进行链接。测试,这适用于 gcc,但不适用于 g++。
使用示例文件 src/main.c:
#include <stdlib.h>
#include <stdio.h>
int main()
{
printf("Entering %s:%s\n", __FILE__, __func__);
return 0;
}
int not_main()
{
printf("Entering %s:%s\n", __FILE__, __func__);
exit(0); // Necessary, otherwise it throws a segfault
}
Run Code Online (Sandbox Code Playgroud)
编译后的gcc -Wl,--entry=not_main -o entry.o src/main.c输出就是我想要的:Entering src/main.c:not_main。
但是,当使用 编译时g++ -Wl,--entry=not_main -o entry.o src/main.c,会出现以下警告:/usr/bin/ld: warning: cannot find entry symbol not_main; defaulting to 0000000000001080。
这默认为main()函数输出Entering …