<stdlib.h>将 MinGw 包含目录添加到搜索路径时,未在 MinGW 中找到 [英] <stdlib.h> not found in MinGW when MinGw include directory is added to search path

查看:44
本文介绍了<stdlib.h>将 MinGw 包含目录添加到搜索路径时,未在 MinGW 中找到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到错误

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>

将 C:\MinGW\include 添加到编译器包含搜索路径时:
echo "#include "|g++ -x c++ - -isystem C:/MinGW/include -o/dev/nul

when adding C:\MinGW\include to the compiler include search path:
echo "#include <cstdlib>" | g++ -x c++ - -isystem C:/MinGW/include -o /dev/nul

但是 CMake 这样做是因为某些库(例如 libcurl)安装在 C:\MinGW 中,因此 curl 包含目录是 C:\MinGW\include

But CMake does this because some libraries (libcurl e.g.) are installed into C:\MinGW hence the curl include dir is C:\MinGW\include

我做错了什么还是MinGW中的一个错误?我使用的是 MinGW 5.0.1.

Am I doing something wrong or is this a bug in MinGW? I'm using MinGW 5.0.1.

什么有效:echo "#include "|g++ -x c++ - -IC:/MinGW/include -o/dev/nul 但我不想包含 curl 包含目录等作为非系统包含.

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.

相关mingw/include/c++/cstdlib: stdlib.h: 没有那个文件或目录

背景:我使用 cmake 来生成 makefile.所以在 CMakelists.txt 中有一个 find_package(Curl) 和一个 include_directories(SYSTEM CURL_INCLUDE_DIRS).当 libcurl 安装到 C:/MinGW 时,CURL_INCLUDE_DIRS 将是 C:/MinGW/include,因此 -isystem 包含.我不想省略 SYSTEM,因为这可能会导致为 libcurl 标头生成警告.当然还有更多的库也以同样的方式安装,我想保持 cmake 文件的可移植性.

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.

推荐答案

问题在于C++标准头文件include_next的使用.根据 https://gcc.gnu.org/onlinedocs/cpp/Wrapper-Headers.html 它将包含标题在找到当前文件的目录之后搜索头文件目录列表.标准包含目录(使用 g++ -v)是(更正):

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

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

因此 cstdlib 将在 c:\mingw\lib/gcc/mingw32/6.3.0/include/c++include_next "stdlib.h" 将在此列表的更深处找到它,并在 c:\mingw\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.

现在的问题是:将库安装到 C:\mingw(使用 libbininclude> 库中的文件夹)将使 CMake 在那里正确找到它们,并将 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. 添加为 -I:这将被 g++ 忽略,ignoring ... 因为它是一个复制系统目录的非系统目录
  2. 添加为 -isystem:这会将目录前置到上面的列表中,并将其作为重复项从其余目录中删除(使用 -v 选项).这意味着当找到 cstdlib 并评估 include_next 时,它只会向下搜索列表.但是包含 stdlib.h 的目录不再在列表中,而是向上,因此不会被搜索.因此出现错误.
    注意:我发现了 include_next 的另一个定义,它只丢弃包含标题的目录.这在这种情况下可行,但可能导致循环并已更改为所描述的行为.
  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.

目前的解决方案只是将库安装或复制到 C:\mingw\mingw32.

Solution so far is simply installing or copying the libraries to C:\mingw\mingw32 instead.

这篇关于&amp;lt;stdlib.h&gt;将 MinGw 包含目录添加到搜索路径时,未在 MinGW 中找到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆