c++ - 在 Xubuntu 19.04 中编译 c++ 是否应该与在 Ubuntu 18.04 中编译 c++ 不同

标签 c++ linux ubuntu

是否会出现 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/

相关文章:

c++ - CMake 在 64 位 Ubuntu 14.04 上找不到 32 位 Open SSL

C++ 函数指针和类

c++ - C++中的无序集交集

linux - 显示函数在 vim 中的所有位置的列表

linux - 是否可以在不手动单击每个链接的情况下从父目录下载所有文件?

ubuntu - 增加 Ubuntu/Upstart 的最大打开文件数(initctl)

c++ - 如何将 C++ dll 项目添加到 Visual Basic 引用中?

c++ - 预序列化原始消息的某些字段

linux - 启动FedoOne服务器

c++ - uniform_real_distribution operator() 编译错误