从macOS Catalina上的源代码编译LLVM(如何解决未找到"stdio.h")

Compile LLVM from sources on macOS Catalina (How to solve "stdio.h" not found)

本文关键字:解决 stdio Catalina macOS 源代码 LLVM 编译 何解决      更新时间:2023-10-16

我提请您注意一个我花了几天时间解决的问题,没有任何问题可以为我解决,希望它能节省其他人的时间。

我尝试从macOS Catalina(10.15(上的源代码编译LLVM。

为此,我使用了以下命令(遵循 LLVM 构建手册后(:

git clone https://github.com/llvm/llvm-project
cd llvm-project
mkdir build && cd build
cmake -G "Ninja" -DLLVM_ENABLE_PROJECTS="clang;clang-tools-extra" -DCMAKE_BUILD_TYPE="Debug" ../llvm
ninja

现在你应该在build/bin下有一个有效的clang编译器。 但是,鉴于/tmp/program.c以下内容:

#include <stdio.h>
int main() {
printf("Hello, world!n");
return 0;
}

我得到了以下输出:

/tmp/program.c:1:10: fatal error: 'stdio.h' file not found
#include <stdio.h>
^~~~~~~~~
1 error generated.

我尝试了很多解决方案,包括设置CPATHCFLAGSCXXFLAGSLDFLAGS等,但没有一个奏效。

系统标头新路径

从macOS Catalina开始,/usr文件夹被挂载为只读目录,因此即使作为root目录 - 也没有人有权修改此文件夹(好吧,除非您在SIP模式下重新启动,这显然不是一个相当大的解决方案(。 默认情况下,整个工具链保存在/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk下。您可以通过执行xcrun --show-sdk-path来获取路径。我假设您的答案中确实有默认路径,但请根据需要进行调整。

收集信息

使用-v标志运行clang给了我以下内容:

./clang /tmp/program.c -v --    
clang version 11.0.0 (https://github.com/llvm/llvm-project 01641197ee0bd3895c756c925723c8c8e03bcb09)
Target: x86_64-apple-darwin19.5.0
Thread model: posix
InstalledDir: ~/llvm/llvm-project/build/bin/.
"~/llvm/llvm-project/build/bin/clang-11" -cc1 -triple x86_64-apple-macosx10.15.0 -Wdeprecated-objc-isa-usage -Werror=deprecated-objc-isa-usage -emit-obj -mrelax-all -disable-free -main-file-name program.c -mrelocation-model pic -pic-level 2 -mframe-pointer=all -fno-rounding-math -munwind-tables -fcompatibility-qualified-id-block-type-checking -target-cpu penryn -dwarf-column-info -debugger-tuning=lldb -target-linker-version 556.6 -v -resource-dir ~/llvm/llvm-project/build/lib/clang/11.0.0 -internal-isystem /usr/local/include -internal-isystem ~/llvm/llvm-project/build/lib/clang/11.0.0/include -internal-externc-isystem /usr/include -fdebug-compilation-dir ~/llvm/llvm-project/build/bin -ferror-limit 19 -stack-protector 1 -fblocks -fencode-extended-block-signature -fregister-global-dtors-with-atexit -fgnuc-version=4.2.1 -fmax-type-align=16 -fcolor-diagnostics -o /var/folders/64/6bf82c_52ws1n4cwc376znvr0000gn/T/program-d111a4.o -x c /tmp/program.c
clang -cc1 version 11.0.0 based upon LLVM 11.0.0git default target x86_64-apple-darwin19.5.0
ignoring nonexistent directory "/usr/include"
#include "..." search starts here:
#include <...> search starts here:
/usr/local/include
~/llvm/llvm-project/build/lib/clang/11.0.0/include
/System/Library/Frameworks (framework directory)
/Library/Frameworks (framework directory)
End of search list.
/tmp/program.c:1:10: fatal error: 'stdio.h' file not found
#include <stdio.h>
^~~~~~~~~
1 error generated.

这可以告诉我们,系统标头的包含路径是:

/usr/local/include
~/llvm/llvm-project/build/lib/clang/11.0.0/include
/System/Library/Frameworks (framework directory)
/Library/Frameworks (framework directory)

因此,我们可以看到新标头的位置(/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk(没有出现在这里,因此错误。

我们可以做一个从/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/local/include的符号链接,但这会产生冲突的标题并产生不好的副作用(是的,我试过了。是的,这很愚蠢...

溶液

我们可以看到默认的搜索标题路径之一出现在构建目录下(~/llvm/llvm-project/build/lib/clang/11.0.0/include(。以符号方式将标准标头链接到此目录不会产生任何危险的冲突。

因此,要解决此问题,您需要做的就是:

ln -s /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/* ~/llvm/llvm-project/build/lib/clang/11.0

我希望我的回答能帮助那些在这个问题上挣扎的人。

为了能够在 macOS 上使用从 LLVM 源编译clang,您可以执行以下操作:

  • 设置PATH以包含新建clang所在的bin目录。
  • clang需要知道XCode SDK的位置。您可以通过设置环境变量来执行此操作SDKROOT。例如:
SDKROOT=`xcrun --show-sdk-path` clang test.c

应该就是这样了。