Previously, we built llvm and clang on MacOS 10.13 with reference to the official documents. However, when compiling with clang + +
, we sometimes encounter problems like this. The specific error information is as follows:
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)
The reason is that the default C + + runtime library is libc + +
, instead of libstdc + +
, the former puts STD:: String
in non-standard namespace STD::_ 1::_ String
below
We can use the following simple C + + 11 program to test whether the default C + + runtime library is libc + + or libstdc + +
#include <iostream>
#include <random>
int main() {
int&& x = 10;
std::cout << x << std::endl;
return 0;
}
After compiling, use otool
to view the binary of the link
g++ -std=c++11 random.cpp -o random
otool -L random
The output is as follows:
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)
You can see the link is libc + +
Compile with G + + - 4.9
installed by brew, and the output is:
random:
/usr/local/opt/[email protected]/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)
The system also provides libstdc + +, the path is /usr/lib/libstdc + +. 6. Dylib
There are two ways to solve the above problems
One is to specify stdlib
:
g++ -std=c++11 -stdlib=libstdc++ source.cpp -o source
Second, rebuild LLVM and Clang by modifying the following code snippet in 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;
For:
case llvm::Triple::x86:
case llvm::Triple::x86_64:
IsBaseFound = AddGnuCPlusPlusIncludePaths("/usr/local/opt/[email protected]/include/c++/4.9.4",
"x86_64-apple-darwin17.3.0", "",
"x86_64", triple);
break;