为什么 -Wl,--entry 适用于 gcc,但不适用于 g++?

RoG*_*art 3 c c++ gcc g++

我正在尝试编译一个程序,以便它从不同的入口点启动。我正在使用 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 src/main.c:main. not_main()链接器找不到该函数,但它存在于源代码中。

g++ 的文档说:

g++ 是一个调用 GCC 并自动指定与 C++ 库的链接的程序。

如果内部一个调用另一个,我不明白 g++ 与 gcc 有什么不同。我知道不是编译器而是链接器改变了入口点,并且 g++(与 gcc 不同)正在链接到 C++ 库,但我无法理解这是如何出现问题的。

我缺少什么?

Kam*_*Cuk 5

由于名称修改,该函数不是not_mainbut _Z8not_mainv。

g++ 与 gcc 有何不同,

g++ 和 gcc 有什么区别? 为什么使用 g++ 而不是 gcc 来编译 *.cc 文件?