之前在 macOS 10.13 上參照官方文檔 build 了 LLVM 和 Clang,而在使用 clang++
編譯時有時會遇到如題的問題,具體報錯信息如下:
Undefined symbols for architecture x86_64:
"std::string::compare(char const*) const", referenced from:
get_token() in toy-28f990.o
"std::string::_M_replace_aux(unsigned long, unsigned long, unsigned long, char)", referenced from:
get_token() in toy-28f990.o
"std::string::_Rep::_M_destroy(std::allocator<char> const&)", referenced from:
get_token() in toy-28f990.o
"std::string::_Rep::_S_empty_rep_storage", referenced from:
get_token() in toy-28f990.o
__GLOBAL__sub_I_toy.cpp in toy-28f990.o
"std::string::reserve(unsigned long)", referenced from:
get_token() in toy-28f990.o
"std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()", referenced from:
__GLOBAL__sub_I_toy.cpp in toy-28f990.o
ld: symbol(s) not found for architecture x86_64
clang-9: error: linker command failed with exit code 1 (use -v to see invocation)
原因是默認的 C++ runtime library 是 libc++
,而非 libstdc++
,前者把 std::string
放在非標准 namespace std::_1::_string
下。
我們可以用下面這個簡單的 C++11 程序來測試默認的 C++ runtime library 是 libc++ 還是 libstdc++。
#include <iostream>
#include <random>
int main() {
int&& x = 10;
std::cout << x << std::endl;
return 0;
}
編譯之后使用 otool
查看鏈接的 binary。
g++ -std=c++11 random.cpp -o random
otool -L random
輸出如下:
random:
/usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 400.9.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1252.50.4)
可以看到鏈接的是 libc++。
改用 brew 安裝的 g++-4.9
編譯,輸出為:
random:
/usr/local/opt/gcc@4.9/lib/gcc/4.9/libstdc++.6.dylib (compatibility version 7.0.0, current version 7.20.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1252.50.4)
/usr/local/lib/gcc/4.9/libgcc_s.1.dylib (compatibility version 1.0.0, current version 1.0.0)
系統也提供了 libstdc++,路徑為 /usr/lib/libstdc++.6.dylib
。
要解決上述原因導致的如題的問題,有兩種方法。
一是在編譯時指定 stdlib
:
g++ -std=c++11 -stdlib=libstdc++ source.cpp -o source
二是重新 build LLVM 和 Clang,修改 llvm-project/clang/lib/Frontend/InitHeaderSearch.cpp
中以下代碼段:
case llvm::Triple::x86:
case llvm::Triple::x86_64:
IsBaseFound = AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
"i686-apple-darwin10", "",
"x86_64", triple);
IsBaseFound |= AddGnuCPlusPlusIncludePaths(
"/usr/include/c++/4.0.0", "i686-apple-darwin8", "", "", triple);
break;
為:
case llvm::Triple::x86:
case llvm::Triple::x86_64:
IsBaseFound = AddGnuCPlusPlusIncludePaths("/usr/local/opt/gcc@4.9/include/c++/4.9.4",
"x86_64-apple-darwin17.3.0", "",
"x86_64", triple);
break;
參考:Compile clang against libstdc++ with C++11 support on a Mac