且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在 Windows 上使用 cmake + arm-none-eabi 进行交叉编译?

更新时间:2023-10-22 23:00:04

将我的评论转化为答案

我已经尝试了您示例的简化版本.我可以重现您的初始错误,并可以通过设置 CMAKE_TRY_COMPILE_TARGET_TYPESTATIC_LIBRARY.但随后构建可执行文件"仍然会失败.

I've given a reduced version of your an example a try. I could reproduce your initial error and could fix the compiler detection by setting CMAKE_TRY_COMPILE_TARGET_TYPE to STATIC_LIBRARY. But then building an "executable" would still fail.

所以你可以设置CMAKE_EXE_LINKER_FLAGS_INIT--specs=nosys.specs (因为这些是链接器标志).如果您使用 ..._INIT 标志,只需确保从空的二进制输出目录开始,因为结果 CMAKE_EXE_LINKER_FLAGS 已被缓存.

So you can set CMAKE_EXE_LINKER_FLAGS_INIT to --specs=nosys.specs (since those are linker flags). Just make sure to start from an empty binary output directory if you are using ..._INIT flags, since the resulting CMAKE_EXE_LINKER_FLAGS is cached.

我更喜欢强制结果 CMAKE_EXE_LINKER_FLAGS 变量(INTERNAL 确实暗示 FORCE).

I prefer to force the result CMAKE_EXE_LINKER_FLAGS variable (INTERNAL does imply FORCE).

CMakeLists.txt

cmake_minimum_required(VERSION 3.6)

project(testApp)

file(WRITE main.cpp "int main(void) { return 0; }")

set(testApp "testApp")

list(
    APPEND testApp_sources
        main.cpp
)

add_executable(
    ${testApp}
    ${testApp_sources}
)

toolchain.cmake

set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_PROCESSOR arm)

set(CMAKE_C_COMPILER "arm-none-eabi-gcc.exe")
set(CMAKE_CXX_COMPILER "arm-none-eabi-g++.exe")

set(CMAKE_EXE_LINKER_FLAGS "--specs=nosys.specs" CACHE INTERNAL "")

set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)

是否:

build> cmake -G"MinGW Makefiles" -DCMAKE_BUILD_TYPE=Release -DCMAKE_TOOLCHAIN_FILE:PATH="..\toolchain.cmake"  ..
-- The C compiler identification is GNU 6.3.1
-- The CXX compiler identification is GNU 6.3.1
-- Check for working C compiler: C:/Program Files (x86)/GNU Tools ARM Embedded/6 2017-q1-update/bin/arm-none-eabi-gcc.exe
-- Check for working C compiler: C:/Program Files (x86)/GNU Tools ARM Embedded/6 2017-q1-update/bin/arm-none-eabi-gcc.exe -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: C:/Program Files (x86)/GNU Tools ARM Embedded/6 2017-q1-update/bin/arm-none-eabi-g++.exe
-- Check for working CXX compiler: C:/Program Files (x86)/GNU Tools ARM Embedded/6 2017-q1-update/bin/arm-none-eabi-g++.exe -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: build

build> cmake --build .
Scanning dependencies of target testApp
[ 50%] Building CXX object CMakeFiles/testApp.dir/main.obj
[100%] Linking CXX executable testApp
[100%] Built target testApp

参考资料