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


因為要用 openmp庫,用 clang++ 編譯 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-select install

沒有用。

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

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

可以看到這些

 /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/include

因為當前的 clang++是用 brew 安裝的 llvm 自帶的。
嘗試了卸載 llvm

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。就又安裝回來了

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即可。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM