【问题标题】:Clang on MacOS having problems with its includesMacOS 上的 Clang 包含问题
【发布时间】:2020-11-30 04:41:05
【问题描述】:

我在 MacOS 上从头开始构建 Clang,但遇到了问题。

使用以下配置 Clang 进行构建:

cmake -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX=/opt/clang-12 -DCMAKE_BUILD_TYPE=Release -DLLVM_TARGETS_TO_BUILD="AArch64;ARM;X86" -DLLVM_ENABLE_PROJECTS="clang;libcxx;libcxxabi;lldb" ../llvm

这是我的测试程序:

#include <ostream>
#include <algorithm>

int main(int argc, char** argv) {
return 0;
}
  1. 如果使用 XCode 附带的 Clang 11.0.3 编译则可以clang++ -c test.cc

  2. 如果使用新的 Clang 12(内置) /opt/clang-12/bin/clang++ -c test.cc,则会出现错误

In file included from test.cc:2:
In file included from /opt/clang-12/bin/../include/c++/v1/ostream:138:
In file included from /opt/clang-12/bin/../include/c++/v1/ios:214:
In file included from /opt/clang-12/bin/../include/c++/v1/iosfwd:95:
/opt/clang-12/bin/../include/c++/v1/wchar.h:119:15: fatal error: 'wchar.h' file not found
#include_next <wchar.h>
              ^~~~~~~~~
1 error generated.

根据我尝试包含的内容,我会收到不同的包含错误。

➜ tests /opt/clang-12/bin/clang++ -v -c test.cc
clang version 12.0.0 (https://github.com/llvm/llvm-project.git b529c5270c99e0ca18e3cbd9a5f50eb0970e560a)
Target: x86_64-apple-darwin19.6.0
Thread model: posix
InstalledDir: /opt/clang-12/bin
 (in-process)
 "/opt/clang-12/bin/clang-12" -cc1 -triple x86_64-apple-macosx10.15.0 -Wundef-prefix=TARGET_OS_ -Werror=undef-prefix -Wdeprecated-objc-isa-usage -Werror=deprecated-objc-isa-usage -emit-obj -mrelax-all --mrelax-relocations -disable-free -disable-llvm-verifier -discard-value-names -main-file-name test.cc -mrelocation-model pic -pic-level 2 -mframe-pointer=all -fno-rounding-math -munwind-tables -fcompatibility-qualified-id-block-type-checking -target-cpu penryn -debugger-tuning=lldb -target-linker-version 556.6 -v -resource-dir /opt/clang-12/lib/clang/12.0.0 -stdlib=libc++ -internal-isystem /opt/clang-12/bin/../include/c++/v1 -internal-isystem /usr/include/c++/v1 -internal-isystem /usr/local/include -internal-isystem /opt/clang-12/lib/clang/12.0.0/include -internal-externc-isystem /usr/include -fdeprecated-macro -fdebug-compilation-dir /Users/duddie/Projects/Audio/1voct/1voct_modular/tests -ferror-limit 19 -stack-protector 1 -fblocks -fencode-extended-block-signature -fregister-global-dtors-with-atexit -fgnuc-version=4.2.1 -fcxx-exceptions -fexceptions -fmax-type-align=16 -fcolor-diagnostics -o test.o -x c++ test.cc
clang -cc1 version 12.0.0 based upon LLVM 12.0.0git default target x86_64-apple-darwin19.6.0
ignoring nonexistent directory "/usr/include/c++/v1"
ignoring nonexistent directory "/usr/include"
#include "..." search starts here:
#include <...> search starts here:
 /opt/clang-12/bin/../include/c++/v1
 /usr/local/include
 /opt/clang-12/lib/clang/12.0.0/include
 /System/Library/Frameworks (framework directory)
 /Library/Frameworks (framework directory)
End of search list.
In file included from test.cc:2:
In file included from /opt/clang-12/bin/../include/c++/v1/ostream:138:
In file included from /opt/clang-12/bin/../include/c++/v1/ios:214:
In file included from /opt/clang-12/bin/../include/c++/v1/iosfwd:95:
/opt/clang-12/bin/../include/c++/v1/wchar.h:119:15: fatal error: 'wchar.h' file not found
#include_next <wchar.h>
              ^~~~~~~~~
1 error generated.
  1. 如果 Clang 11.0.3 (XCode) 或 Clang 12 但针对 ARM 则为错误 clang++ -target arm-none-eabi -c test.cc
test.cc:2:10: fatal error: 'ostream' file not found
#include <ostream>
         ^~~~~~~~~
1 error generated.

任何想法我做错了什么?

【问题讨论】:

    标签: c++ xcode macos sdk clang


    【解决方案1】:

    将以下内容添加到 shell 配置文件 ~/.bash_profile~/.zsh_profile 或以任何其他方式设置运行它的 shell 可访问的环境变量。

    export SDKROOT="/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk"
    

    在您的 macOS/Xcode 版本上验证 SDK 的正确路径。

    重新打开终端(获取文件可能无法可靠地工作)。 然后再次尝试编译命令。

    /opt/clang-12/bin/clang++ -c test.cc
    

    命令行工具包安装 macOS 系统头文件 在 macOS SDK 中。使用已安装工具编译的软件 将在 Xcode 提供的 macOS SDK 中搜索标头 在: /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk 或命令行工具: /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk 取决于哪个 使用 xcode-select 选择。命令行工具将搜索 默认情况下用于系统标头的 SDK。但是,有些软件可能无法 针对 SDK 正确构建并要求 macOS 标头是 安装在 /usr/include 下的基本系统中。如果你是 此类软件的维护者,我们鼓励您更新您的项目 使用 SDK 或针对以下问题提交错误报告 阻止你这样做。作为一种解决方法,一个额外的包是 提供它将把头安装到基本系统。在一个 未来版本,将不再提供此软件包。你可以找到 这个包在: /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg

    https://developer.apple.com/documentation/xcode-release-notes/xcode-10-release-notes

    【讨论】:

    • 虽然它有助于编译 macOS 目标,或者我应该说原生 x86_64,但它仍然没有解决 arm-none-eabi 的任何问题——这是我编译自己的 Clang 的主要原因。
    • 实际上发现是 -target arm-none-eabi 导致的。 -arch arm7 工作
    【解决方案2】:

    对于它的价值,我能够通过这样做来解决 MacOs Big Sur 的问题

    export SDKROOT='/Library/Developer/CommandLineTools/SDKs/MacOSX11.1.sdk'
    

    【讨论】:

      【解决方案3】:

      可能更便携(我在llvm-project/llvm/utils/lit/lit/util.py:399 找到了提示):

      export SDKROOT=$(xcrun --show-sdk-path --sdk macosx)
      

      此外,您还可以在 clang 驱动程序中添加 -isysroot 参数来更改默认系统根目录以进行标头搜索:

      clang++ a.cpp -isysroot $(xcrun --show-sdk-path --sdk macosx)
      

      更多细节和调查笔记here

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-01-04
        • 2020-03-12
        • 2020-08-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-29
        • 1970-01-01
        • 2010-10-15
        相关资源
        最近更新 更多