mac 下 clang++ 找不到頭文件 stdlib.h

由於要用 openmp庫,用 clang++ 編譯 c++程序,出現了以下報錯:c++

clang++ xx.cpp -o xx -fopenmp
/usr/local/Cellar/llvm/7.0.0/include/c++/v1/stdlib.h:94:15:
 fatal error:      'stdlib.h' file not found
#include_next <stdlib.h>
                      ^

探究緣由:
include_next是在當前文件所在的路徑後面的路徑(通常有多個搜索路徑)裏搜索頭文件。
報錯說明 clang++的 include 搜索路徑裏/usr/local/Cellar/llvm/7.0.0/include/c++/v1/ 後面的路徑中不存在stdlib.h文件。xcode

網上的解決方案app

xcode-select install

沒有用。工具

使用命令查看 clang++的 include 搜索路徑(#include <...> search starts here: 後面)。code

clang++ -E -x c++ - -v < /dev/null

能夠看到這些orm

/usr/local/Cellar/llvm/7.0.0/include/c++/v1
 /usr/include/c++/v1
 /usr/local/include
 /usr/local/Cellar/llvm/7.0.0/lib/clang/7.0.0/include
 /System/Library/Frameworks (framework directory)
 /Library/Frameworks (framework directory)

發現忽略了不存在的/usr/includerem

由於當前的 clang++是用 brew 安裝的 llvm 自帶的。
嘗試了卸載 llvmio

brew remove llvm

這時系統裏還有 clang++,看起來是 Xcode 的工具鏈裏的。
這時再執行clang++ -E -x c++ - -v < /dev/null能夠看到編譯

/usr/local/include
 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1
 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/10.0.0/include
 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include
 /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/usr/include
 /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/System/Library/Frameworks (framework directory)

由於要用 openmp,因此還得用 brew 安裝的 llvm。就又安裝回來了form

brew install llvm

而後經過添加軟連接的方式解決問題。

ln -s /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include /usr/include/c++/v1

由於MacOSX10.14.sdk其實是指向MacOSX.sdk的軟連接,因此直接用MacOSX.sdk便可。

相關文章
相關標籤/搜索