背景:
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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)