When/why libstdc++ 和 libc++ 符号不兼容吗?

When/why are libstdc++ and libc++ symbols incompatible?

设置:

test.cpp

#include <set>
#include <string>

void common_config_file_iterator(const std::set<std::string>& allowed_options) {}

include.cpp

#include <set>
#include <string>

void common_config_file_iterator(const std::set<std::string>&) noexcept;

int main() {
    std::set<std::string> set;
    common_config_file_iterator(set);
    return 0;
}

test.sh

clang++-7 test.cpp -c -O3 -fno-rtti -fno-exceptions -o test.o
g++-8 test.o include.cpp -O3 -fno-rtti -fno-exceptions -o test

输出:

Undefined symbols for architecture x86_64:
  "common_config_file_iterator(std::set<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::less<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > const&)", referenced from:
      _main in ccWoGgrX.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status

所以我做了 nm -g test.o:

0000000000000000 T __Z27common_config_file_iteratorRKNSt3__13setINS_12basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEEENS_4lessIS6_EENS4_IS6_EEEE

根据demangler.com,意思是:

common_config_file_iterator(std::__1::set<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::less<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > const&)

Libc++ says:

[Features and Goals:] ABI compatibility with gcc's libstdc++ for some low-level features such as exception objects, rtti and memory allocation.

所以,问题是std::allocator<char>

请注意,我使用的是 macOS 汇编程序。

this issue and boost/program-options引起的好奇心。

So, is the problem std::allocator<char>?

什么?不。在你的例子中是一切

您引用的文档明确表示目标是 "low-level features such as exception objects, rtti and memory allocation".

的兼容性

std::setstd::string 不是 "low-level features such as exception objects, rtti and memory allocation"。它们在 libc++ 和 libstdc++ 之间绝对不兼容,它们是完全不同的库,具有完全不同的实现。

兼容的部分是 std::type_infostd::exception(以及 <stdexcept> 中的派生异常类型),因为它们是基本语言运行时的一部分。超出此范围的任何内容,例如容器、字符串、算法、I/O、区域设置等都不兼容。