Skip to content

macOS 上 'include/c++/v1' 的搜索路径

macOS 10.15.7中,使用g++ -E -v -xc /dev/null会得到如下输出:

bash
#include <...> search starts here:
 /usr/local/include
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/12.0.0/include
 /Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include
 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include
 /Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/System/Library/Frameworks (framework directory)
End of search list.

我发现我所需要引用的头文件__cxx_version位于目录/Library/Developer/CommandLineTools/usr/include/c++/v1/下,但是编译器的搜索路径并不包含它。

我检查了我所使用的两个macOS 10.15.7系统,发现:

  • 系统A在/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1//Library/Developer/CommandLineTools/usr/include/c++/v1下均提供了__cxx_version
  • 系统B仅在/Library/Developer/CommandLineTools/usr/include/c++/v1下提供了__cxx_version,但在/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/下提供的所有文件中恰恰缺少了__cxx_version,其他均和系统A相同。

这导致系统B在编译项目时失败,因为编译器并不会在/Library/Developer/CommandLineTools/usr/include/c++/v1中搜索头文件。

资料表示:

/Library/Developer/CommandLineTools/ only exists if the user has explicitly installed the command line tools with xcode-select --install (or from a pkg downloaded from the developer portal). Homebrew does recommend / require this for some packages so I imagine for many users that’s the most likely reason they would be installed.

但是目前仍不清楚为何系统B与A存在如此差异。

可以通过sudo cp /Library/Developer/CommandLineTools/usr/include/c++/v1/__cxx_version /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/来解决编译问题。

不可以通过export CPLUS_INCLUDE_PATH=/Library/Developer/CommandLineTools/usr/include/c++/v1:$CPLUS_INCLUDE_PATH来解决编译问题,会报很多变量未声明的错误。

如果还是有问题,可以重装一下XCode。