Search code examples
ccmakelinkerarmclang

How to specify stdlib path for ARM cross compiler in clang with cmake?


I am using cmake with clang to compile a C project for an ARM cortex-m4 MCU (armv7e-m, softfp).

Currently I am using the GCC Toolchain includes and libraries so I specify them as follows in the Toolchain cmake file:

set(CMAKE_SYSROOT /opt/gcc-arm-none-eabi-10-2020-q4-major/arm-none-eabi)

This works as expected and there isnt any problem with the includes. As the linker requires the standard libraries I noticed it looks bby default for libm and libc in

/opt/gcc-arm-none-eabi-10-2020-q4-major/arm-none-eabi/lib.

However, I would like to use the std libraries from another path, for this case:

/opt/gcc-arm-none-eabi-10-2020-q4-major/arm-none-eabi/lib/thumb/v7e-m+fp/softfp

I tried using -L/opt/gcc-arm-none-eabi-10-2020-q4-major/arm-none-eabi/lib/thumb/v7e-m+fp/softfp but I noticed that the other path takes priority.

So my question is how can I tell clang to look for the standard libraries in this path? My current solution is not setting CMAKE_SYSROOT at all, then I add -nostdinc to the compiler flags and include the std includes from the GCC toolchain manually

-I${ARM_TOOLCHAIN_PATH}/arm-none-eabi/include
-I${ARM_TOOLCHAIN_PATH}/lib/gcc/arm-none-eabi/10.2.1/include

That allows me to the tell the linker the paths for the lib I want:

-L${ARM_TOOLCHAIN_PATH}/lib/thumb/v7e-m+fp/softfp

As I am not experience with clang and cmake I am not sure if this is the best way to override a library path or tell clang to look first in another path.


Solution

  • The comment from Tsyvarev worked out. I added the following to my toolchain cmake file

    set(CMAKE_C_STANDARD_INCLUDE_DIRECTORIES 
        ${ARM_TOOLCHAIN_BIN_DIR}/../arm-none-eabi/include
        )
    

    Afterwards I specify the library paths where cmake should look for the libm libc.

    target_link_directories(${EXECUTABLE} PRIVATE
        ${PROJECT_SOURCE_DIR}/my_custom_libs
        )
    

    I did not use CMAKE_C_STANDARD_LIBRARIES as the library names are the same as the standard naming. Just in case you want to add your own std libs with different names you just have to add -nostdlib flag and then its up to you to provide all the std defs that your project uses for the specific cpu.