问题
I'm getting the error
c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\cstdlib:75:25: fatal error: stdlib.h: No such file or directory
#include_next <stdlib.h>
when adding C:\MinGW\include to the compiler include search path:echo "#include <cstdlib>" | g++ -x c++ - -isystem C:/MinGW/include -o /dev/nul
But CMake does this because some libraries (libcurl e.g.) are installed into C:\MinGW hence the curl include dir is C:\MinGW\include
Am I doing something wrong or is this a bug in MinGW? I'm using MinGW 5.0.1.
What works: echo "#include <cstdlib>" | g++ -x c++ - -IC:/MinGW/include -o /dev/nul
but I don't want to include the curl include dirs etc. as non-system includes.
Related to mingw/include/c++/cstdlib: stdlib.h: No such file or directory
Background: I'm using cmake to generate the makefiles. So there is a find_package(Curl)
and a include_directories(SYSTEM CURL_INCLUDE_DIRS)
in the CMakelists.txt. As libcurl is installed to C:/MinGW the CURL_INCLUDE_DIRS will be C:/MinGW/include and hence the -isystem include. I don't want to omit the SYSTEM because this might cause warnings to be generated for the libcurl headers. Of course there are more libraries that are also installed in the same way and I want to keep the cmake files portable.
回答1:
The problem lies in the use of include_next
of the C++ standard header. According to https://gcc.gnu.org/onlinedocs/cpp/Wrapper-Headers.html it will include the header searching the list of header file directories after the directory in which the current file was found
. The standard include directories (using g++ -v
) are (corrected):
c:\mingw\lib/gcc/mingw32/6.3.0/include/c++
c:\mingw\lib/gcc/mingw32/6.3.0/include/c++/mingw32
c:\mingw\lib/gcc/mingw32/6.3.0/include/c++/backward
c:\mingw\lib/gcc/mingw32/6.3.0/include
c:\mingw\include
c:\mingw\lib/gcc/mingw32/6.3.0/include-fixed
c:\mingw\mingw32/include
Hence the cstdlib
will be found in c:\mingw\lib/gcc/mingw32/6.3.0/include/c++
and include_next "stdlib.h"
will go further down this list and will find it in c:\mingw\include
.
Now the problem: Installing the libraries into C:\mingw
(using the lib
, bin
and include
folders from the library) will make CMake correctly find them there and add the C:\mingw\include
folder explicitly to the include list. The 2 cases work out as following:
- Adding as
-I
: This will be ignored by g++ withignoring ... as it is a non-system directory that duplicates a system directory
- Adding as
-isystem
: This will prepend the directory to the list above and remove it from the rest as a duplicate (verified with the-v
option). This means that whencstdlib
is found andinclude_next
is evaluated it will search only downward the list. But the directory containing thestdlib.h
is not down the list anymore but upwards and therefore not searched. Hence the error.
Note: I found another definition ofinclude_next
which only discards the directory containing the header. That would work in this case but can lead to loops and was changed to the described behaviour.
Solution so far is simply installing or copying the libraries to C:\mingw\mingw32
instead.
回答2:
Why don't you prefer to use a C++ IDE.There are many good IDE's like Code Blocks,Dev C++.I use Dev C++.You won't face with that problem if you use IDE.
来源:https://stackoverflow.com/questions/46555273/stdlib-h-not-found-in-mingw-when-mingw-include-directory-is-added-to-search-pa