abi:cxx11 的 GCC 未定义引用

GCC undefined references with abi:cxx11

我正在用 gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.11) 编译 C++ 程序。编译时一切正常。

然后我 link 那些带有用 gcc version 4.8.4 (Ubuntu 4.8.4-2ubuntu1~14.04.3) 构建的库的程序。

然后我得到未定义的引用 link 错误:

CMakeFiles/FOSSSim.dir/RigidBodies/RigidBodyGRVelocityProjectionCollisionResolver.cpp.o:(.rodata._ZTV46RigidBodyGRVelocityProjectionCollisionResolver[_ZTV46RigidBodyGRVelocityProjectionCollisionResolver]+0x18): undefined reference to `RigidBodyGRVelocityProjectionCollisionResolver::getName[abi:cxx11]() const'
CMakeFiles/FOSSSim.dir/RigidBodies/RigidBodyGRLCPCollisionResolver.cpp.o:(.rodata._ZTV31RigidBodyGRLCPCollisionResolver[_ZTV31RigidBodyGRLCPCollisionResolver]+0x18): undefined reference to `RigidBodyGRLCPCollisionResolver::getName[abi:cxx11]() const'
CMakeFiles/FOSSSim.dir/RigidBodies/RigidBodyVelocityProjectionCollisionResolver.cpp.o:(.rodata._ZTV44RigidBodyVelocityProjectionCollisionResolver[_ZTV44RigidBodyVelocityProjectionCollisionResolver]+0x18): undefined reference to `RigidBodyVelocityProjectionCollisionResolver::getName[abi:cxx11]() const'
CMakeFiles/FOSSSim.dir/RigidBodies/RigidBodyLCPCollisionResolver.cpp.o:(.rodata._ZTV29RigidBodyLCPCollisionResolver[_ZTV29RigidBodyLCPCollisionResolver]+0x18): undefined reference to `RigidBodyLCPCollisionResolver::getName[abi:cxx11]() const'
CMakeFiles/FOSSSim.dir/RigidBodies/RigidBodyAllPairsCollisionDetector.cpp.o:(.rodata._ZTV34RigidBodyAllPairsCollisionDetector[_ZTV34RigidBodyAllPairsCollisionDetector]+0x18): undefined reference to `RigidBodyAllPairsCollisionDetector::getName[abi:cxx11]() const'

通过一些谷歌搜索,我发现一些 link 像 linking-problems-due-to-symbols-with-abicxx11 处理 -D_GLIBCXX_USE_CXX11_ABI=0 编译选项。不幸的是,当我使用该选项时,我收到 Cmake 警告:

CMake Warning:
  Manually-specified variables were not used by the project:

    _GLIBCXX_USE_CXX11_ABI

有什么解决问题的线索吗?知道我宁愿避免更改编译器版本。

谢谢。

_GLIBCXX_USE_CXX11_ABI 是一个预处理器宏,它控制 GNU C++ 标准库实现的行为。它需要传递给预处理器,而不是 CMake。

在您的 CMakeLists.txt 中使用 add_compile_definitions(_GLIBCXX_USE_CXX11_ABI=0) 或通过命令行中的 CMAKE_CXX_FLAGS 传递它:

cmake -DCMAKE_CXX_FLAGS='-D_GLIBCXX_USE_CXX11_ABI=0' path/to/your/project

请注意,虽然这可能会让您的程序得以构建,但它可能不足以让一切正常运行。混合由不同版本的 GCC 编译的目标代码充满了问题。