Garphunkle
Garphunkle

Reputation: 21

Cannot find header files when cross compiling with clang and specifying target/sysroot

I am attempting to cross-compile a C++ object file using the GNU ARM Embedded Toolchain libraries, and the Clang compiler. I cannot understand what is missing to successfully compile. Could someone help me to understand why clang cannot find the header files?

Here is my example program:

#include <algorithm>

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

I invoke clang++ by specifying the target and sysroot:

$ 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.

I would suspect a linker error (clang may not know how to find the correct libraries within the sysroot), however I am surprised clang cannot infer the location of the algorithm header given a sysroot.

Any suggestions?

Both clang and arm-none-eabi-gcc are in my path:

$ 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.

Upvotes: 2

Views: 2805

Answers (1)

Jeremy
Jeremy

Reputation: 5321

I've found that -sysroot doesn't solve the problem on my system and so - as suggested by the relevant Clang documentation - I've explicitly specified include paths using -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

Since the GCC header files use #include_next directives, the order of these is crucial.

For the time being I'm using gcc/g++ to link, so I'm yet to have to deal with any linking issues.

Upvotes: 0

Related Questions