Clang++ 已更新,无法找到某些 headers 尽管它们在包含目录中

Clang++ updated, can't find certain headers despite them being in the include directory

所以我在 mac 并且想尝试最新的 llvm 版本,而不必等待它们在 xcode 命令行工具上传递。

所以我从他们的下载页面下载了 LLVM 10 版本 pre-built 二进制文件,并将它放在一个名为 llvm 的文件夹中。所以可以在 ~/SDKs/LLVM/bin.

中找到 clang 可执行文件

我制作这个程序:

#include <string>
#include <iostream>

int main(int argc, char const *argv[])
{
    std::string myString("Hello World");
    std::cout << myString;
    return 0;
}

和运行:

~/SDKs/Clang+LLVM10/bin/clang++ main.cpp

我收到这个致命错误:

~/SDKs/Clang+LLVM10/bin/../include/c++/v1/string.h:60:15: fatal error: 
      'string.h' file not found
#include_next <string.h>
              ^~~~~~~~~~

这没有意义,因为我可以使用 finder 手动找到 string.h,它就在错误中引用的文件夹中。

我猜这与 clang++ 首先搜索我的系统包含路径有关,在那里找到一个较旧的 string.h,然后选择不使用更新的 string.h,导致错误?


如果有帮助,请补充一些信息:

运行 这个(新编译器)

~/Programming/SDKs/Clang+LLVM10/bin/clang++ -Wp,-v -E -xc -x c++ /dev/null

给我:

clang -cc1 version 10.0.0 based upon LLVM 10.0.0 default target x86_64-apple-darwin18.7.0
ignoring nonexistent directory "/usr/include/c++/v1"
ignoring nonexistent directory "/usr/include"
#include "..." search starts here:
#include <...> search starts here:
 ~/SDKs/Clang+LLVM10/bin/../include/c++/v1
 /usr/local/include
 ~/SDKs/Clang+LLVM10/lib/clang/10.0.0/include
 /System/Library/Frameworks (framework directory)
 /Library/Frameworks (framework directory)
End of search list.
# 1 "/dev/null"
# 1 "<built-in>" 1
# 1 "<built-in>" 3
# 393 "<built-in>" 3
# 1 "<command line>" 1
# 1 "<built-in>" 2
# 1 "/dev/null" 2

运行(这是我的 mac 自带的默认编译器)

clang++ -Wp,-v -E -xc -x c++ /dev/null

给我

clang -cc1 version 11.0.0 (clang-1100.0.33.17) default target x86_64-apple-darwin18.7.0
ignoring nonexistent directory "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1"
ignoring nonexistent directory "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/local/include"
ignoring nonexistent directory "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/Library/Frameworks"
#include "..." search starts here:
#include <...> search starts here:
 /usr/local/include
 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1
 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/11.0.0/include
 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include
 /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include
 /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks (framework directory)
End of search list.
# 1 "/dev/null"
# 1 "<built-in>" 1
# 1 "<built-in>" 3
# 374 "<built-in>" 3
# 1 "<command line>" 1
# 1 "<built-in>" 2
# 1 "/dev/null" 2

我也遇到了这个错误。经过我的挖掘,我找到了原因: include_next 是一个 gnu 编译器扩展,似乎在 clang 中不存在。虽然clang(/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++)已经将原来的std lib打包到目录下: “ /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/”,但作为苹果Mac的编译源代码,一般都是通过选项“-”指定系统根路径isysroot”,一般命令是这样的:“-isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk” , 覆盖默认的标准库搜索路径。 因此,也许您必须覆盖默认的系统根目录以确保链接正确的标准库。