Recently I switched to gcc-6 in my OS X, and faced to VARIOUS of disasterS. One of them is the gcc compile cannot find header "stdlib.h"

For detail, we can find error message as follow:

In file included from /usr/local/Cellar/gcc/6.2.0/include/c++/6.2.0/ext/string_conversions.h:41:0,
                 from /usr/local/Cellar/gcc/6.2.0/include/c++/6.2.0/bits/basic_string.h:5402,
                 from /usr/local/Cellar/gcc/6.2.0/include/c++/6.2.0/string:52,
                 from /Users/yu/.local/src/xgboost/dmlc-core/include/dmlc/base.h:171,
                 from /Users/yu/.local/src/xgboost/dmlc-core/src/recordio.cc:3:
/usr/local/Cellar/gcc/6.2.0/include/c++/6.2.0/cstdlib:75:25: fatal error: stdlib.h: No such file or directory
 #include_next <stdlib.h>
                         ^
compilation terminated.

Thousands of similar questions can be found in google and most of them are stupid s**t. After hours' check, this follow up gives me the answer.

Afaik new GCC 6 trips if you manually pass /usr/include in the wrong order. What happens if you remove that obvious CPLUS_INCLUDE_PATH, and check the makefile/configure script for any -I /usr/include and subsequently remove them? Not on my arch would test that myself otherwise.

This problem is from my environment settings. At first, my environment parameter CPLUS_INCLUDE_PATH written as follow:

export CPLUS_INCLUDE_PATH=$CPLUS_INCLUDE_PATH:/usr/include:/usr/local/include:$HOME/.local/include:/some/other/paths

This sequence is incorrect, because we can find stdlib.h in /usr/include , but that stdlib.h is not expected by gcc-6, and an fatal error comes.

To fix this problem, a fix may as follow:

export CPLUS_INCLUDE_PATH=/usr/local/Cellar/gcc/6.2.0/include/c++/6.2.0:/usr/include:/usr/local/include:$HOME/.local/include:$CPLUS_INCLUDE_PATH

And finally, it works.

PS:

How to install gcc-6 in OS X?

We shall install homebrew first, and then type:

brew install gcc --without-multilib

brew is the de facto standard of yum/dnf/apt-get on OS X. You can use parameter "--without-multilib" to obtain the support OpenMP, and it will take you quite a while (~30 min) to compile the whole gcc. You can check here and here for more details.

How to change default C/C++ compile?

You can edit your ~/.bash_profile or ~/.bashrc, create one if not exists, and then append lines as follow:

CC=/usr/local/bin/gcc-6
CXX=/usr/local/bin/g++-6

Your cmake or most of other makefiles will detect these environment parameters and use them as default C/C++ compile

来自的你,很高兴你能看到这儿。若本文对你有所用处,或者内容有什么不足之处,敬请毫不犹豫给个回复。谢谢!