gpt4 book ai didi

c++ - 链接到使用不同编译器标志构建的静态库是否安全

转载 作者:可可西里 更新时间:2023-11-01 15:23:41 26 4
gpt4 key购买 nike

我使用 GoogleTest 测试我的 C++ 项目,发现 Ubuntu 包中不再分发预编译库后,我在项目网站上找到了以下内容:

If you compile Google Test and your test code using different compiler flags, they may see different definitions of the same class/function/variable (e.g. due to the use of #if in Google Test). Therefore, for your sanity, we recommend to avoid installing pre-compiled Google Test libraries. Instead, each project should compile Google Test itself such that it can be sure that the same flags are used for both Google Test and the tests.

我从中得出的结论是,将 GoogleTest 与正在测试的项目分开编译是个坏主意。我不明白的是这是否只是 GoogleTest 的事情,或者这是否是链接库的一般事情。

问题

是否存在链接到预编译的第三方库、编译器标志或其他不安全的情况,如果没有,GoogleTest 有什么特别之处?

最佳答案

有一些编译器标志,特别是那些与对齐一起使用的标志,可能会导致问题。

来自 GCC i386 and x86-64 flags

-malign-double
-mno-align-double

Control whether GCC aligns double, long double, and long long variables on a two-word boundary or a one-word boundary. Aligning double variables on a two-word boundary produces code that runs somewhat faster on a Pentium at the expense of more memory.

On x86-64, -malign-double is enabled by default.

Warning: if you use the -malign-double switch, structures containing the above types are aligned differently than the published application binary interface specifications for the 386 and are not binary compatible with structures in code compiled without that switch.

例如,在 32 位系统上使用该标志将使 double 和 long long 为 64 位对齐。如果您编译一个没有标志的库,然后尝试在使用该标志的同时使用该库,则包含上述类型的结构可能具有不同的对齐方式,并且可能无法互操作。

其他(更简单的)情况也可以确保使用同一组#defines 以确保使用相同的函数/结构/类定义(以及其他此类 ODR 违规)。例如,在 gcc 中使用 '--std=c++11',它启用标准库类的 C++ 11 版本,在某些情况下与以前的版本不同。

关于c++ - 链接到使用不同编译器标志构建的静态库是否安全,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12867161/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com