c++编译器配置错误问题clang: error: linker command failed with exit code 1 (use -v to see invocation)

背景:

ubuntu18.04系统, 之前装其他程序的时候安装了一些软件,不知道什么时候g++编译器从/usr/bin/c++组里面给删掉了,默认的编译器成了clang++(之前安装的,如果没有备选的c++编译器,cmake 时候会报错unkonw),类似这个博客 中.之前报错的时候工程已经编译了,没有注意到cmake中默认的g++编译器版本, ubuntu18.04中gcc 和g++默认都是 GNU 7.5.0,也没有注意编译器版本已经换掉了,在报题目的错误同时还会有如下类似错误:

/usr/lib/x86_64-linux-gnu/libstdc++.so.6: error adding symbols: DSO missing from command line

各种百度谷歌之后,找到了一个临时解决方案,在cmake中设置CMAKE_CXX_FLAGS

set(CMAKE_CXX_FLAGS -stdlib=libstdc++)

这样之前所有的cmake工程都需要配置一下,极其麻烦,还有个别工程即使配置完无法编译通过, 后面才明白gcc和clang编译时所依赖的标准库不一样,彼此不兼容,参考博客中所说如下:

-stdlib= <libstdc++/libc++> -stdlib指编译器编译时依赖的标准库。

gcc编译器没有-stdlib 这样命令行选项,LLVM clang编译器支持。 这是因为clang为您提供链接LLVM标准C ++库( libc ++ )或GNU标准C ++库( libstdc ++ ),而gcc只支持 libstdc ++ 。 简单说就是两个都是 C++ 标准库,libc++ 是针对 Clang 编译器特别重写的 C++ 标准库,而 libstdc++ 则是 GCC 的对应 C++ 标准库。

编译程序范例指定CXXFLAGS = -std=c++11 -stdlib=libstdc++

解决方案:

1.cmake在第一次编译的时候开始会输出使用的c和cxx编译器,如下:

 cmake ..
-- The C compiler identification is GNU 7.5.0
-- The CXX compiler identification is Clang 6.0.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
...

2.更新c++默认编译器

由于之前已经删除了g++-7对应的编译器链接,所以执行 sudo update-alternatives --config g++ 显示

sudo update-alternatives --config c++
There is only one alternative in link group c++ (providing /usr/bin/c++): /usr/bin/clang++-libc++
Nothing to configure.

添加g++编译器到group c++

sudo update-alternatives --install /usr/bin/c++ c++ /usr/bin/g++ 100

再次执行即可看到:默认编译器已经切换为g++

sudo update-alternatives --config c++
There are 2 choices for the alternative c++ (providing /usr/bin/c++).

  Selection    Path                     Priority   Status
------------------------------------------------------------
* 0            /usr/bin/g++              100       auto mode
  1            /usr/bin/clang++-libc++   5         manual mode
  2            /usr/bin/g++              100       manual mode

Press <enter> to keep the current choice[*], or type selection number: 

查看编译器版本g++-7

ll /usr/bin/g++
lrwxrwxrwx 1 root root 5 5月  21  2019 /usr/bin/g++ -> g++-7*

后续升级编译器也是类似操作.

3.验证测试

删除项目的build内容,重新编译查看cmake默认编译器均为GNU 7.5.0, 正常!

cmake ..
-- The C compiler identification is GNU 7.5.0
-- The CXX compiler identification is GNU 7.5.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值