GCC版本问题:编译找不到 math.h stdlib.h: fatal error: stdlib.h: No such file or director

6 篇文章 0 订阅

cmake编译时报错: 找不到头文件math.h stdlib.h
fatal error: stdlib.h: No such file or directory
fatal error: math.h: No such file or directory

In file included from /usr/local/include/c++/6.2.0/bits/stl_algo.h:59:0,
                 from /usr/local/include/c++/6.2.0/algorithm:62,
                 from /home/dmelo/proj2/opencv/modules/core/include/opencv2/core/base.hpp:55,
                 from /home/dmelo/proj2/opencv/modules/core/include/opencv2/core.hpp:54,
                 from /home/dmelo/proj2/opencv/modules/highgui/include/opencv2/highgui.hpp:46,
                 from /home/dmelo/proj2/opencv/build/modules/highgui/precomp.hpp:45:
/usr/local/include/c++/6.2.0/cstdlib:75:25: fatal error: stdlib.h: No such file or directory
 #include_next <stdlib.h>
                         ^
compilation terminated.

解决方法1
修改头文件

Solution is Simple...
1: just go to usr/include/c++
2: chose whatever version you are using. In my case it was 6, So chose folder 6
3: look for cmath.c and open it. Change "#include_next <math.h>" to "#include<math.h>"
4: Save it and Done..

If you are having error with stdlib.h then also do the same for "cstdlib.c" file

解决方法2
选择编译器版本,使用特定版本编译器编译,例如用g++ gcc 5

CC=/usr/bin/gcc-5 CXX=/usr/bin/g++-5 cmake .
make

或者修改 CMakeLists.txt 文件,添加如下命令

SET(CMAKE_C_COMPILER “/usr/bin/gcc-5”)
SET(CMAKE_CXX_COMPILER “/usr/bin/g++-5”)

解决方法3

关闭预编译头文件

cmake  . -DENABLE_PRECOMPILED_HEADERS=OFF
make

解决办法4

一些库依赖没有下下来,新建的库拉一下submodule

git submodule init
git submodule updata

上述几种办法中解决方法2是正解
下面讲解linux下gcc、g++不同版本的安装和切换

Ubuntu 18.04预装GCC版本为7.3,但有时在编译是需要用的不同gcc版本,下面介绍,如何安装不同的gcc 和g++,并设置根据不同的需要在不同版本之间切换。

  1. 可以通过如下命令查看当前安装的版本:
    ls /usr/bin/gcc*

  2. 安装gcc-4.8 和gcc-5:

sudo apt install gcc-4.8 g++ -4.8
sudo apt install gcc-5 g++ -5

  1. 使用update-alternatives设置gcc和g++:
    update-alternatives是ubuntu系统中专门维护系统命令链接符的工具,通过它可以很方便的设置系统默认使用哪个命令、哪个软件版本。
    其中40 ,50 ,70是优先级数值可以自己设定,–slave能保证gcc和g++保持相同的版本。

    sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.8 40 --slave /usr/bin/g++ g++ /usr/bin/g+±4.8
    sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-5 50 --slave /usr/bin/g++ g++ /usr/bin/g+±5
    sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-7 70 --slave /usr/bin/g++ g++ /usr/bin/g+±7

  2. 使用如下命令选择gcc的版本:

sudo update-alternatives --config gcc

可以看到当前gcc默认的版本是gcc-7,下面我们修改为gcc-4.8,直接选择编号即可。

$ sudo update-alternatives --config gcc
There are 3 choices for the alternative gcc (providing /usr/bin/gcc).
* 0            /usr/bin/gcc-7     70        auto mode
  1            /usr/bin/gcc-4.8   40        manual mode
  2            /usr/bin/gcc-5     50        manual mode
  3            /usr/bin/gcc-7     70        manual mode

Press <enter> to keep the current choice[*], or type selection number: 1
update-alternatives: using /usr/bin/gcc-4.8 to provide /usr/bin/gcc (gcc) in manual mode
  1. 验证是否修改成功:

    gcc -v
    g++ -v
    
  2. 删除
    删除某个gcc版本的选项的话,可以使用

sudo update-alternatives --remove gcc /usr/bin/gcc-4.5

再使用apt-remove删除即可

参考:
https://github.com/highfidelity/hifi/issues/8047
https://blog.argcv.com/articles/4655.c
https://www.jianshu.com/p/f66eed3a3a25
http://tuxamito.com/wiki/index.php/Installing_newer_GCC_versions_in_Ubuntu

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值