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
^
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 " to "#include"
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++,并设置根据不同的需要在不同版本之间切换。
可以通过如下命令查看当前安装的版本:
ls /usr/bin/gcc*
安装gcc-4.8 和gcc-5:
sudo apt install gcc-4.8 g++ -4.8
sudo apt install gcc-5 g++ -5
使用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
使用如下命令选择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 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
验证是否修改成功:
gcc -v g++ -v
删除
删除某个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