arm-none-eabi-gcc

unrecognized command-line option '-arch'; did you mean '-march='?


I'm trying to build a STM32 project on MacBook M1 with CMake and arm-none-eabi-gcc. These are my CMake codes:

cmake_minimum_required(VERSION 3.0.0)
project(Embedded VERSION 0.1.0)


set(CMAKE_C_COMPILER "arm-none-eabi-gcc") 

add_compile_options(-g -mcpu=cortex-m4 -O2 -Wall -Werror  -Wmissing-prototypes  -mlittle-endian   -fsingle-precision-constant -Wdouble-promotion)

add_executable(Embedded main.c)

set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)

How can I make CMake generate -march instead if -arch?


Solution

  • TL; DR

    Have a try at setting SET(APPLE FALSE).


    I've encountered a similar problem when cross-compiling a RISC-V project on my MacBook with macOS Sonoma. I found that CMake will set -arch arm64 by default, and there is an option controlling the argument to -arch called CMAKE_OSX_ARCHITECTURES. However, setting this argument to false does not help since CMake still applies a default value of arm64 which is the architecture of the build host.

    I'm rather curious about why there's not much information about this question on the Internet; both on StackOverflow and GitHub are some questions/issues on this problem but no one gave a sharp and clear answer, at least none solved my issue.

    So, I dived into the source code of CMake and found that if the target is set as Apple platforms, there will be a hardcoded appending to the compilation flags, which caused this problem. Finally, we can avoid this by setting APPLE variable to false, effectively simulating building on other platforms if you do not need to build a universal package which is almost always the case today.