用 cmake 构建 STM32 工程问题【已解决】
环境信息
- os: ubuntu22.04
- gcc: arm-none-eabi-gcc (Arm GNU Toolchain 13.2.rel1 (Build arm-13.7)) 13.2.1 20231009
- cmake: cmake version 3.22.1
- ninja: 1.10.1
问题 log
[main] 正在配置项目: Olidy
[driver] 删除 /home/pomegranate/workspace/Olidy/build/CMakeCache.txt
[driver] 删除 /home/pomegranate/workspace/Olidy/build
[main] 在 CMakeLists.txt 中检测到更改,但由于另一操作已在进行中,我们无法重新配置项目。
[proc] 执行命令: /usr/bin/cmake -DCMAKE_BUILD_TYPE:STRING=Debug -DCMAKE_C_FLAGS:STRING=--specs=nosys.specs -DCMAKE_CXX_FLAGS:STRING=--specs=nosys.specs -DCMAKE_SYSTEM_NAME:STRING=Generic -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE -DCMAKE_C_COMPILER:FILEPATH=/home/pomegranate/software/arm-gnu-toolchain-13.2.Rel1-x86_64-arm-none-eabi/bin/arm-none-eabi-gcc -DCMAKE_CXX_COMPILER:FILEPATH=/home/pomegranate/software/arm-gnu-toolchain-13.2.Rel1-x86_64-arm-none-eabi/bin/arm-none-eabi-g++ --no-warn-unused-cli -S/home/pomegranate/workspace/Olidy -B/home/pomegranate/workspace/Olidy/build -G Ninja
[cmake] Not searching for unused variables given on the command line.
[cmake] -- The C compiler identification is GNU 13.2.1
[cmake] -- The CXX compiler identification is GNU 13.2.1
[cmake] -- The ASM compiler identification is GNU
[cmake] -- Found assembler: /home/pomegranate/software/arm-gnu-toolchain-13.2.Rel1-x86_64-arm-none-eabi/bin/arm-none-eabi-gcc
[cmake] -- Detecting C compiler ABI info
[cmake] -- Detecting C compiler ABI info - failed
[cmake] -- Check for working C compiler: /home/pomegranate/software/arm-gnu-toolchain-13.2.Rel1-x86_64-arm-none-eabi/bin/arm-none-eabi-gcc
[cmake] -- Check for working C compiler: /home/pomegranate/software/arm-gnu-toolchain-13.2.Rel1-x86_64-arm-none-eabi/bin/arm-none-eabi-gcc - broken
[cmake] CMake Error at /usr/share/cmake-3.22/Modules/CMakeTestCCompiler.cmake:69 (message):
[cmake] The C compiler
[cmake]
[cmake] "/home/pomegranate/software/arm-gnu-toolchain-13.2.Rel1-x86_64-arm-none-eabi/bin/arm-none-eabi-gcc"
[cmake]
[cmake] is not able to compile a simple test program.
[cmake]
[cmake] It fails with the following output:
[cmake]
[cmake] Change Dir: /home/pomegranate/workspace/Olidy/build/CMakeFiles/CMakeTmp
[cmake]
[cmake] Run Build Command(s):/usr/bin/ninja cmTC_77608 && [1/2] Building C object CMakeFiles/cmTC_77608.dir/testCCompiler.c.obj
[cmake] [2/2] Linking C executable cmTC_77608
[cmake] FAILED: cmTC_77608
[cmake] : && /home/pomegranate/software/arm-gnu-toolchain-13.2.Rel1-x86_64-arm-none-eabi/bin/arm-none-eabi-gcc -mcpu=cortex-m4 -mthumb -mfpu=fpv4-sp-d16 -mfloat-abi=hard -std=gnu99 -Wall -fdata-sections -ffunction-sections -mcpu=cortex-m4 -mthumb -mfpu=fpv4-sp-d16 -mfloat-abi=hard -Wl,--gc-sections CMakeFiles/cmTC_77608.dir/testCCompiler.c.obj -o cmTC_77608 && :
[cmake] /home/pomegranate/software/arm-gnu-toolchain-13.2.Rel1-x86_64-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/13.2.1/../../../../arm-none-eabi/bin/ld: /home/pomegranate/software/arm-gnu-toolchain-13.2.Rel1-x86_64-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/13.2.1/../../../../arm-none-eabi/lib/thumb/v7e-m+fp/hard/libc.a(libc_a-exit.o): in function `exit':
[cmake] exit.c:(.text.exit+0x14): undefined reference to `_exit'
[cmake] collect2: error: ld returned 1 exit status
[cmake] ninja: build stopped: subcommand failed.
[cmake]
[cmake]
[cmake]
[cmake]
[cmake]
[cmake] CMake will not be able to correctly generate this project.
[cmake] Call Stack (most recent call first):
[cmake] CMakeLists.txt:23 (project)
[cmake]
[cmake]
[cmake] -- Configuring incomplete, errors occurred!
[cmake] See also "/home/pomegranate/workspace/Olidy/build/CMakeFiles/CMakeOutput.log".
[cmake] See also "/home/pomegranate/workspace/Olidy/build/CMakeFiles/CMakeError.log".
[proc] 命令“/usr/bin/cmake -DCMAKE_BUILD_TYPE:STRING=Debug -DCMAKE_C_FLAGS:STRING=--specs=nosys.specs -DCMAKE_CXX_FLAGS:STRING=--specs=nosys.specs -DCMAKE_SYSTEM_NAME:STRING=Generic -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE -DCMAKE_C_COMPILER:FILEPATH=/home/pomegranate/software/arm-gnu-toolchain-13.2.Rel1-x86_64-arm-none-eabi/bin/arm-none-eabi-gcc -DCMAKE_CXX_COMPILER:FILEPATH=/home/pomegranate/software/arm-gnu-toolchain-13.2.Rel1-x86_64-arm-none-eabi/bin/arm-none-eabi-g++ --no-warn-unused-cli -S/home/pomegranate/workspace/Olidy -B/home/pomegranate/workspace/Olidy/build -G Ninja”已退出,代码为 1
问题定位
arm-gnu-toolchain 配置出错,导致 cmake 执行了错误的操作,如测试编译器能否生存可执行文件
验证
使用官方生成的 arm-none-eabi.cmake,问题解决【确凿】
解决方案:参考 cmake try-compiler
在 CMakeLists.txt 中加入
# 其它设置
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY) # 规范 try-compiler 行为