[英]Why does -Wl,--entry work with gcc, but not g++?
I am trying to compile a program so that it starts on a different entry point.我正在尝试编译一个程序,以便它从不同的入口点开始。 I am running WSL1 with Ubuntu 20.04.5, and GCC and G++ 9.4.0
我正在使用 Ubuntu 20.04.5、GCC 和 G++ 9.4.0 运行 WSL1
I found that adding the flag -Wl,--entry=foo
to the compiler will link foo()
as the entry function. Testing, this has worked with gcc, but not with g++.我发现将标志
-Wl,--entry=foo
添加到编译器会将foo()
链接为条目 function。测试,这适用于 gcc,但不适用于 g++。
Using the example file src/main.c:使用示例文件 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
}
When compiled with gcc -Wl,--entry=not_main -o entry.o src/main.c
the output is what I want: Entering src/main.c:not_main
.当使用
gcc -Wl,--entry=not_main -o entry.o src/main.c
,output 就是我想要的: Entering src/main.c:not_main
。
However, when compiled with g++ -Wl,--entry=not_main -o entry.o src/main.c
, the following warning appears: /usr/bin/ld: warning: cannot find entry symbol not_main; defaulting to 0000000000001080
但是用
g++ -Wl,--entry=not_main -o entry.o src/main.c
时出现如下警告: /usr/bin/ld: warning: cannot find entry symbol not_main; defaulting to 0000000000001080
/usr/bin/ld: warning: cannot find entry symbol not_main; defaulting to 0000000000001080
. /usr/bin/ld: warning: cannot find entry symbol not_main; defaulting to 0000000000001080
。
This defaults to the main()
function, outputting Entering src/main.c:main
.这默认为
main()
function,输出Entering src/main.c:main
。 The function not_main()
is not found by the linker, but it is present in the source code. function
not_main()
未被 linker 找到,但它存在于源代码中。
The documentation for g++ says: g++ 的文档说:
g++ is a program that calls GCC and automatically specifies linking against the C++ library.
g++ 是一个调用 GCC 并自动指定链接到 C++ 库的程序。
I don't see how g++ can differ from gcc, if internally one calls the other.如果在内部调用另一个,我看不出 g++ 与 gcc 有何不同。 I understand that it is not the compiler but the linker which changes the entry point, and that g++ (unlike gcc) is linking against the C++ library, but I fail to understand how that is problematic.
我知道改变入口点的不是编译器,而是 linker,而 g++(与 gcc 不同)链接到 C++ 库,但我不明白这是怎么回事。
What am I missing?我错过了什么?
Because of name mangling, the function is not not_main
but _Z8not_mainv
.由于名称修改, function 不是
not_main
而是_Z8not_mainv
。
how g++ can differ from gcc,
g++ 与 gcc 有何不同,
What is the difference between g++ and gcc? g++ 和 gcc 有什么区别? why use g++ instead of gcc to compile *.cc files?
为什么使用g++而不是gcc来编译*.cc文件?
C++, unlike C, uses name mangling to distinguish different overloads of the same function name. C++ 与 C 不同,它使用名称修改来区分相同 function 名称的不同重载。
When compiled with gcc
:使用
gcc
编译时:
$ objdump -t entry.o | grep not_main
000000000000117c g F .text 0000000000000036 not_main
When compiled with g++
:使用
g++
编译时:
$ objdump -t entry.o | grep not_main
0000000000000000 *UND* 0000000000000000 not_main
000000000000117c g F .text 0000000000000036 _Z8not_mainv
The *UND*
efined reference to not_main
was probably placed there by the linker since you requested this as the entry point.对 not_main 的
*UND*
定义引用可能由not_main
放置在那里,因为您请求将其作为入口点。 The actual not_main
function has its name mangled to _Z8not_mainv
.实际的
not_main
function 的名称被修改为_Z8not_mainv
。
To export not_main
under its original name, use extern "C"
:要以其原始名称导出
not_main
,请使用extern "C"
:
extern "C" int not_main()
{
printf("Entering %s:%s\n", __FILE__, __func__);
exit(0); // Necessary, otherwise it throws a segfault
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.