1

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.

Harry Johnston
  • 35,639
  • 6
  • 68
  • 158
Flamefire
  • 5,313
  • 3
  • 35
  • 70
  • 1
    Why suspecting a bug in MinGW?? It's about g++ no? And even a bug in g++ is more unlikely than you did something wrong. – user0042 Oct 04 '17 at 00:27
  • I don't know where the error is. It seems to be MinGW related as the include dir of MinGW causes the bug. If I did something wrong, then I'd be happy to know what so I can fix that. – Flamefire Oct 04 '17 at 00:34
  • I don't get the relation with the curl includes? Why not just use `-IC:/MinGW/include`? – user0042 Oct 04 '17 at 00:36
  • Are you sure that `c:\MinGW\include` isn't already in the search path? (The error looks as if it might be caused by the search path becoming misordered.) I believe you can check [using something like](https://gcc.gnu.org/onlinedocs/cpp/Search-Path.html) `cpp -v /dev/null -o /dev/null` – Harry Johnston Oct 04 '17 at 01:31
  • Updated the question for the origin of the include. There is a "ignoring duplicate directory /mingw/include" message in the -v output but is does not appear in the "include <> starts here" list. But in this list is a "c:/MinGW/mingw32/include". Maybe the libs should be installed in mingw/mingw32 instead? – Flamefire Oct 04 '17 at 04:57
  • I don't know, but naively I'd have expected the install directory for libcurl to be `c:\MinGW\libcurl` or something. Any libcurl users reading? – Harry Johnston Oct 04 '17 at 20:12

1 Answers1

4

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:

  1. Adding as -I: This will be ignored by g++ with ignoring ... as it is a non-system directory that duplicates a system directory
  2. 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 when cstdlib is found and include_next is evaluated it will search only downward the list. But the directory containing the stdlib.h is not down the list anymore but upwards and therefore not searched. Hence the error.
    Note: I found another definition of include_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.

Flamefire
  • 5,313
  • 3
  • 35
  • 70