通过自制软件(macOS)安装的clang++:编译错误



安装 clang++ 后(尝试 v. 6.0.1 和 7.0(:
brew install --with-toolchain llvm

非常简单的程序会导致以下错误:

In file included from test.cpp:1:
In file included from /usr/local/Cellar/llvm/7.0.0/include/c++/v1/iostream:38:
In file included from /usr/local/Cellar/llvm/7.0.0/include/c++/v1/ios:215:
In file included from /usr/local/Cellar/llvm/7.0.0/include/c++/v1/iosfwd:90:
/usr/local/Cellar/llvm/7.0.0/include/c++/v1/wchar.h:119:15: fatal error: 'wchar.h' file not found
#include_next <wchar.h>

用于编译的命令:

clang++7() {
LDFLAGS="-L/usr/local/opt/llvm/lib -Wl,-rpath,/usr/local/opt/llvm/lib"
CPPFLAGS="-I/usr/local/opt/llvm/include"
/usr/local/opt/llvm/bin/clang++ -std=c++11 $CPPFLAGS $LDFLAGS $1
}

是否可以使用官方的叮当而不是苹果的版本?
对于苹果的版本,我们甚至不知道它到底是哪个版本的LLVM......

从Mojave(10.14(开始,Xcode似乎不再在/usr/include中安装系统标头。有一个兼容包可以这样做,但不建议这样做。

相反,官方解决方案是工具在/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk中搜索标题。该路径可以从

xcrun --show-sdk-path

发行说明说

命令行工具包在 macOS SDK 中安装 macOS 系统标头。使用已安装的工具进行编译的软件将在 Xcode 提供的 macOS SDK 中搜索标头,网址为:

/Applications/Xcode.app/Content/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk

或命令行工具:

/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk

取决于使用 Xcode-Select 选择哪个。

如果您自己构建clang,则可以通过将-isysroot选项传递给 clang 来实现:

clang++ -isysroot "$(xcrun --show-sdk-path)" …

另请参阅:https://github.com/Homebrew/homebrew-core/issues/32765

在clang++ 命令行中添加一个 -I(减眼(选项对我有用,指向/Applications/Xcode.app/Content/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/usr/include

最新更新