clang: error: linker command failed with exit code 1

摘要:
我们可以用下面这个简单的C++11程序来测试默认的C++runtimelibrary是libc++还是libstdc++。#include#includeintmain(){int&&x=10;std::cout˂˂x˂˂std::endl;return0;}编译之后使用otool查看链接的binary。g++-std=c++11random.cpp-orandomotool-Lrandom输出如下:random:/usr/lib/libc++.1.dylib/usr/lib/libSystem.B.dylib可以看到链接的是libc++。改用brew安装的g++-4.9编译,输出为:random:/usr/local/opt/gcc@4.9/lib/gcc/4.9/libstdc++.6.dylib/usr/lib/libSystem.B.dylib/usr/local/lib/gcc/4.9/libgcc_s.1.dylib系统也提供了libstdc++,路径为/usr/lib/libstdc++.6.dylib。要解决上述原因导致的如题的问题,有两种方法。

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

免责声明:文章转载自《clang: error: linker command failed with exit code 1》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇wpa_supplicant 常用操作转载【微信支付】jsapi支付之传参问题(使用微信官方SDK之PHP版本) V3之WxpayPubHelper 亲测有效,V3WxpayAPI_php_v3.zip版未测试,理论上也是一样的。下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Activity的生命周期

今天对于Activity的生命周期又有了一点深入的理解。做个总结吧。 一、正常情况下的生命周期 什么叫正常情况下的生命周期呢?也就是我们经常了解的一个活动的正常的生命流程。不用过度解释, 总结如下: (1)onCreate()方法 活动第一次创建时被调用。 (2)onStart() 活动由不可见变为可见的时候调用 (3)onResume() 当活动准备开始...

springboot 学习之路 27(实现ip白名单功能)

背景:   最近项目中遇到关于对部分请求设置ip白名单的功能需求,在这简单梳理一下我的开发步骤与思路 实践步骤:   思路: 关于实现ip白名单的过滤我大致会想到三种实现方式: 对相关需要过滤的控制类进行单独设置  -------------- 代码臃肿,不利于维护 利用相关拦截器进行统一实现                --------------相...

使用事务和SqlBulkCopy批量插入数据

SqlBulkCopy是.NET Framework 2.0新增的类,位于命名空间System.Data.SqlClient下,主要提供把其他数据源的数据有效批量的加载到SQL Server表中的功能。类似与 Microsoft SQL Server 包中名为 bcp 的命令行应用程序。但是使用 SqlBulkCopy 类可以编写托管代码解决方案,性能上优...

c# 通过程序修改hosts文件

1 根据ip替换 var OSInfo =Environment.OSVersion; string pathpart = "hosts"; if (OSInfo.Platform ==PlatformID.Win32NT) { //is windows NT pathpart = "system32\drivers\etc\hosts";...

Android笔记之强大的buildConfigField

在进行项目开发或维护时,经常会遇到调试和发布所用到的参数值不一致的情况 例如,服务器会分测试和正式,为了能方便地更改(自动更换)服务器地址,buildConfigField就派上用场了 以前都是手动更改的,极易出错T_T buildConfigField语法如下 buildConfigField "TypeName", "FieldName", "Fiel...

美团热修复Robust-源码篇

上一篇主要分析了Robust的使用方法,这一篇就来总结一下Robust的源码分析。 我个人倾向于将Robust框架分为两个部分,自动插入代码和动态加载Patch。 一、Robust源码分析 目前我的分析将Robust动态加载分为两个部分,一部分是插桩后的代码逻辑,一部分是拉取Patch的逻辑。 我们首先来看插桩后的代码(这里面套用的是官方的代码,可能有些过...