是否会出现 C++ 代码在 Ubuntu 18.04 中编译没有错误而在 Xubuntu 19.04 中无法编译的情况。我最初的猜测是不,但后来一位教授告诉我可能存在一些链接器问题。另外让我补充一点,代码是使用相同的标准和 g++ 编译的。
最佳答案
Will there ever be a case where C++ code compiles with no errors in Ubuntu 18.04 and not compile in Xubuntu 19.04.
可能,但它只出现在极端情况下。大多数 C++ 代码不会受到影响。
我认为这取决于 GCC 版本。大约在那个时候,GCC 开始默认启用 PIE,这导致了一些链接问题。参见,例如,Initial SUPERCOP Haswell results for round 2 .来自邮件列表消息:
... The rest of this message explains the -fPIC -fPIE above. On exactly the same long-term-support version of Ubuntu, with the supposedly compatible system-provided versions of gcc and clang, if you run
gcc -c x.c; clang -c y.c; gcc -o x x.o y.o
where x.c says
#include <stdio.h> extern int thenumber(void); int main() { printf("%d\n",thenumber()); return 0; }
and y.c says
static int myconstant = 5; int thenumber(void) { return myconstant; }
then compilation fails, while doing
gcc -c x.c; clang -c y.c; clang -o x x.o y.o
or
gcc -c x.c; gcc -c y.c; gcc -o x x.o y.o
or
clang -c x.c; clang -c y.c; clang -o x x.o y.o
works fine. The underlying problem is compiler-writer mismanagement of an ongoing transition to
-fPIC: compiling libraries as "position-independent code" (this is typically advertised as being important for shared libraries);
-fPIE: compiling main() etc. as position-independent code (this is typically advertised as being important for the claimed security benefits of address-space layout randomization); and
-pie: linking position-independent executables.
Code that's compiled as position-independent code can be linked into position-dependent executables or position-independent executables. A correctly managed transition would have consisted of
turning on -fPIC and -fPIE by default,
issuing automatic warnings for any position-dependent code,
waiting a specified number of years for people to get rid of any previously compiled position-dependent code, and finally
turning on -pie by default.
What happened instead was gcc suddenly turning on -pie, clumsily breaking all existing position-dependent code and even managing to break compatibility with clang on the same system---clang still produces position-dependent code, and then gcc fails to produce an executable. This is also why gcc now breaks crypto_kem/ntruhrss701/avx2.
关于c++ - 在 Xubuntu 19.04 中编译 c++ 是否应该与在 Ubuntu 18.04 中编译 c++ 不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57930878/