【问题标题】:Cannot find header files when cross compiling with clang and specifying target/sysroot使用 clang 交叉编译并指定 target/sysroot 时找不到头文件
【发布时间】:2021-12-03 21:35:29
【问题描述】:

我正在尝试使用 GNU ARM Embedded Toolchain 库和 Clang 编译器交叉编译 C++ 目标文件。我无法理解成功编译缺少什么。谁能帮我理解一下为什么clang找不到头文件?

这是我的示例程序:

#include <algorithm>

extern "C" uint32_t * copy(
    uint32_t const * first, uint32_t const * last, uint32_t * dest)
{
    return std::copy(first, last, dest);
}

我通过指定目标和 sysroot 来调用 clang++:

$ clang++ --target=thumbv7m-none-eabi --sysroot "C:\Program Files (x86)\GNU Arm Embedded Toolchain\10 2021.07\arm-none-eabi" -c example.cpp -v
clang version 12.0.1
Target: thumbv7m-none-unknown-eabi
Thread model: posix
InstalledDir: C:\Program Files\LLVM\bin
 (in-process)
 "C:\\Program Files\\LLVM\\bin\\clang++.exe" -cc1 -triple thumbv7m-none-unknown-eabi -emit-obj -mrelax-all --mrelax-relocations -disable-free -disable-llvm-verifier -discard-value-names -main-file-name example.cpp -mrelocation-model static -mframe-pointer=all -fmath-errno -fno-rounding-math -fno-verbose-asm -mconstructor-aliases -nostdsysteminc -target-cpu cortex-m3 -target-feature +soft-float-abi -target-feature +strict-align -target-abi aapcs -mfloat-abi soft -fallow-half-arguments-and-returns -fno-split-dwarf-inlining -debugger-tuning=gdb -v -resource-dir "C:\\Program Files\\LLVM\\lib\\clang\\12.0.1" -isysroot "C:\\Program Files (x86)\\GNU Arm Embedded Toolchain\\10 2021.07\\arm-none-eabi" -internal-isystem "C:\\Program Files (x86)\\GNU Arm Embedded Toolchain\\10 2021.07\\arm-none-eabi\\include\\c++\\v1" -internal-isystem "C:\\Program Files\\LLVM\\lib\\clang\\12.0.1\\include" -internal-isystem "C:\\Program Files (x86)\\GNU Arm Embedded Toolchain\\10 2021.07\\arm-none-eabi\\include" -fdeprecated-macro -fdebug-compilation-dir "C:\\source\\espresso\\components" -ferror-limit 19 -fmessage-length=226 -fno-signed-char -fgnuc-version=4.2.1 -fcxx-exceptions -fexceptions -fcolor-diagnostics -faddrsig -o example.o -x c++ example.cpp
clang -cc1 version 12.0.1 based upon LLVM 12.0.1 default target x86_64-pc-windows-msvc
ignoring nonexistent directory "C:\Program Files (x86)\GNU Arm Embedded Toolchain\10 2021.07\arm-none-eabi\include\c++\v1"
ignoring duplicate directory "C:\Program Files\LLVM\lib\clang\12.0.1\include"
#include "..." search starts here:
#include <...> search starts here:
 C:\Program Files\LLVM\lib\clang\12.0.1\include
 C:\Program Files (x86)\GNU Arm Embedded Toolchain\10 2021.07\arm-none-eabi\include
End of search list.
example.cpp:3:10: fatal error: 'algorithm' file not found
#include <algorithm>
         ^~~~~~~~~~~
1 error generated.

我怀疑是链接器错误(clang 可能不知道如何在 sysroot 中找到正确的库),但令我惊讶的是,clang 无法在给定 sysroot 的情况下推断算法头的位置。

有什么建议吗?

clang 和 arm-none-eabi-gcc 都在我的路径中:

$ clang++ --version
clang version 12.0.1
Target: x86_64-pc-windows-msvc
Thread model: posix
InstalledDir: C:\Program Files\LLVM\bin

$ arm-none-eabi-gcc --version
arm-none-eabi-gcc (GNU Arm Embedded Toolchain 10.3-2021.07) 10.3.1 20210621 (release)
Copyright (C) 2020 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

【问题讨论】:

    标签: c++ c arm clang cross-compiling


    【解决方案1】:

    我发现 -sysroot 不能解决我系统上的问题,因此 - 正如 the relevant Clang documentation 所建议的那样 - 我已使用 -isystem 明确指定包含路径:

    ifeq ($(toolchain),clang)
        INCLUDE  += -isystem "$(GCC_TOOLCHAIN_DIR)/arm-none-eabi/include/c++/$(GCC_TOOLCHAIN_VERSION)/arm-none-eabi"
        INCLUDE  += -isystem "$(GCC_TOOLCHAIN_DIR)/arm-none-eabi/include/c++/$(GCC_TOOLCHAIN_VERSION)"
        INCLUDE  += -isystem "$(GCC_TOOLCHAIN_DIR)/arm-none-eabi/include"
    endif
    

    由于 GCC 头文件使用 #include_next 指令,这些指令的顺序至关重要

    目前我使用 gcc/g++ 链接,所以我还没有处理任何链接问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-11
      • 2014-03-10
      • 2021-04-15
      • 2013-07-10
      • 2017-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多