0

I am cross compiling with cmake from mac to arm linux, and instead of a toolchain file, I have a custom simple cmake file.

I can build and run fine with add_executable, but when using add_library, cmake starts adding host machine flags to compiler. It thinks I am compiling for the host machine.

Any idea?

my CMakeLists:

cmake_minimum_required(VERSION 3.19)
project ( rpi-zero )

# set(CMAKE_SYSTEM_NAME "Generic" CACHE STRING "Arm")
SET(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_PROCESSOR arm)


set(CMAKE_THREAD_LIBS_INIT "-lpthread")
set(CMAKE_HAVE_THREADS_LIBRARY 1)
set(CMAKE_USE_WIN32_THREADS_INIT 0)
set(CMAKE_USE_PTHREADS_INIT 1)
set(THREADS_PREFER_PTHREAD_FLAG ON)


set( CMAKE_BUILD_TYPE Debug )
# set( CMAKE_BUILD_TYPE release )


set(SYS_ROOT $ENV{HOME}/Projects/rpi-zero/sysroot)
set(CMAKE_SYSROOT ${SYS_ROOT})
set(CMAKE_OSX_SYSROOT "")
# set(CMAKE_OSX_SYSROOT ${SYS_ROOT})
# set(CMAKE_CXX_COMPILER_EXTERNAL_TOOLCHAIN /opt/homebrew/opt/arm-linux-gnueabihf-binutils)

set(CMAKE_CXX_COMPILER "clang++" )
set(CMAKE_CXX_FLAGS " -std=gnu++2a   -fcolor-diagnostics -fdiagnostics-color=always -Wno-ignored-attributes -Werror=return-type -Wfatal-errors" )
set(CMAKE_CXX_FLAGS  "${CMAKE_CXX_FLAGS} -Wno-deprecated-anon-enum-enum-conversion")
set(CMAKE_CXX_FLAGS  "${CMAKE_CXX_FLAGS} -Wno-macro-redefined")
set(CMAKE_CXX_FLAGS  "${CMAKE_CXX_FLAGS} -Wno-deprecated-volatile")
set(CMAKE_CXX_FLAGS  "${CMAKE_CXX_FLAGS} -Wno-format-security")
set(CMAKE_CXX_FLAGS  "${CMAKE_CXX_FLAGS}  --target=arm-linux-gnueabihf ")
set(CMAKE_CXX_FLAGS  "${CMAKE_CXX_FLAGS}  --gcc-toolchain=`brew --prefix arm-linux-gnueabihf-binutils`")
set(CMAKE_CXX_FLAGS  "${CMAKE_CXX_FLAGS}  -B${SYS_ROOT}/usr/lib/gcc/arm-linux-gnueabihf/10 ")
set(CMAKE_CXX_FLAGS  "${CMAKE_CXX_FLAGS}  -mfpu=vfpv2 -march=armv6 -mfloat-abi=hard -std=c++20")


# libs & includes...
somebody4
  • 505
  • 4
  • 14
  • 3
    **All of that should be in a toolchain file, period**. Almost none of it does anything after the `project()` command has been called. – Alex Reinking Dec 01 '21 at 06:42
  • 5
    "instead of a toolchain file, I have a custom simple cmake file." - This is normally not what one should do. `CMakeLists.txt` is for what executables and libraries should be built, and a toolchain file is for a platform description. Do not mix them. Placing toolchain commands **after** the `project()` call is simply **wrong**. See that [my answer](https://stackoverflow.com/a/63944545/3440745) for details. – Tsyvarev Dec 01 '21 at 06:44
  • 1
    Since you require CMake version 3.19 (without failing fatally, if an older version is used), you may want to put a `CMakePresets.json` file next to the toplevel `CMakeLists.txt` file, if your intention is to deliver the project in combination with the toolchain info. This would allow you to use `cmake --preset=default -S ... -B ...` to setup the project without the need to pass the toolchain file via command line option. Btw: using a relatively recent version (>= 3.0) of cmake you should replace you should use `add_compile_definitions` instead of `set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ...")` – fabian Dec 01 '21 at 19:18

0 Answers0