clang: error: linker command failed with exit code 1

之前在 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

原文地址:https://www.cnblogs.com/humz/p/10592602.html