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

时间:2018-10-03 14:16:59

标签: c++ macos llvm homebrew clang++

使用以下命令安装clang ++(尝试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
 }

是否可以使用官方的clang代替Apple的版本?
对于苹果的版本,我们甚至都不知道它真正是哪个版本的LLVM ...

2 个答案:

答案 0 :(得分:1)

看来,从Mojave(10.14)开始,Xcode不再在/usr/include中安装系统头文件。有一个兼容程序包可以,但是不推荐。

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

获得
xcrun --show-sdk-path

release notes

  

“命令行工具”程序包将macOS系统标头安装在macOS SDK中。使用已安装工具编译的软件将在以下任一位置的Xcode提供的macOS SDK中搜索标头:

     

/Applications/Xcode.app/Contents/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

答案 1 :(得分:0)

对我来说,在clang ++命令行中添加-I(负眼)选项,指向/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14。 sdk / usr / include