问题描述
原本mac电脑上装有Xcode,在终端下gcc命令可以使用Xcode自带的clang编译代码。安装完CommandLineTools后,编译代码会报一堆缺少.h文件的错误。
问题分析
装CommandLineTools之前,使用gcc --version命令,得到输出如下:
1 2 3 4 5 6 |
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/ Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/4.2.1 Apple clang version 12.0.0 (clang-1200.0.32.28) Target: x86_64-apple-darwin19.6.0 Thread model: posix InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin |
装CommandLineTools之后,使用gcc --version命令,得到输出如下:
1 2 3 4 5 6 |
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Library/Developer/ CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include/c++/4.2.1 Apple clang version 12.0.0 (clang-1200.0.32.28) Target: x86_64-apple-darwin19.6.0 Thread model: posix InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin |
可见,安装完CommandLineTools之后,gcc默认的include目录发生了改变,而由于未知原因gcc找不到默认的include目录,这导致了编译时缺少.h文件。
问题解决
在查阅了
https://stackoverflow.com/questions/58278260/cant-compile-a-c-program-on-a-mac-after-upgrading-to-catalina-10-15/58278392#58278392之后得知,在macOS Catalina 10.15系统中,移除了/usr/include目录。因此,需要指定新的include目录。解决方案如下:
1 2 3 4 5 6 |
// 依次执行以下命令 1. xcode-select --install 2. sudo ln -s /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/* /usr/local/include/ 3. export SDKROOT=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk |
文章评论