从macOS Catalina上的源代码编译LLVM(如何解决未找到"stdio.h")
Compile LLVM from sources on macOS Catalina (How to solve "stdio.h" not found)
我提请您注意一个我花了几天时间解决的问题,没有任何问题可以为我解决,希望它能节省其他人的时间。
我尝试从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.
我尝试了很多解决方案,包括设置CPATH
、CFLAGS
、CXXFLAGS
、LDFLAGS
等,但没有一个奏效。
系统标头新路径
从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
应该就是这样了。
- 运行同一解决方案的另一个项目的项目
- Project Euler问题4的错误解决方案
- Ardunio UNO解决了多个重叠的定时器循环
- 如何解决gcc编译器优化导致的centos双编译器设置中的分段错误
- 两个文件使用彼此的功能-如何解决
- 计算每个节点的树高,帮助我解释这个代码解决方案
- 如何解决"invalid conversion from 'char' to 'const char*'"
- 在java中解决这段代码时面临循环中的问题
- C++:Application.cpp中抛出了未解析的外部符号(解决方案在问题的末尾,供未来的读者参考)
- 难以理解某些人解决IOI问题的源代码
- visual c++,如何获取解决方案目录中的代码
- 如何解决错误:SCIP C++中的 SCIP 阶段无效 <10>
- 节俭并发:未解决的外部问题
- IpOpt拒绝解决不受约束的问题
- 如何解决这个超硬恒星的创造问题
- 循环无限运行C++解决骑士之旅问题
- 有没有办法在远程设备上打开和编辑visual Studio 2017解决方案
- C++Matching Brackets 2解决方案不起作用
- 在 ubuntu3 上C++ goto 定义有什么解决方案吗16.04?
- 从macOS Catalina上的源代码编译LLVM(如何解决未找到"stdio.h")