ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

clang: error: linker command failed with exit code 1

2019-03-25 11:54:56  阅读:245  来源: 互联网

标签:std code string lib linker c++ version clang toy


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

标签:std,code,string,lib,linker,c++,version,clang,toy
来源: https://www.cnblogs.com/humz/p/10592602.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有