在 CMake 中编译静态 C 库时未生成中间 .obj 文件

Intermediate .obj files not getting generated when compiling a static C library in CMake

使用 CMake 和 ARM GNU 工具链构建静态 C 库时,未创建中间 .obj 文件。

系统信息

文件

库目录结构

bsp
└── SDK_2.5.0_FRDM-KV11Z
    ├── CMSIS
    │   ├── Driver
    │   │   ├── DriverTemplates
    │   │   └── Include
    │   └── Include
    ├── components
    │   ├── fxos8700cq
    │   ├── lists
    │   ├── serial_manager
    │   └── uart
    └── devices
        └── MKV11Z7
            ├── arm
            ├── cmsis_drivers
            ├── drivers
            ├── mcuxpresso
            ├── project_template
            ├── template
            └── utilities
                ├── debug_console
                └── str

顶级CMakeLists.txt

cmake_minimum_required(VERSION 3.12)
project(rtos-project-structure C)
ENABLE_LANGUAGE(ASM)
SET(CMAKE_STATIC_LIBRARY_PREFIX)
SET(CMAKE_STATIC_LIBRARY_SUFFIX)
SET(CMAKE_EXECUTABLE_LIBRARY_PREFIX)
SET(CMAKE_EXECUTABLE_LIBRARY_SUFFIX)

add_subdirectory(bsp)

bsp/CMakeLists.txt

add_subdirectory(SDK_2.5.0_FRDM-KV11Z)

bsp/SDK_2.5.0_FRDM-KV11Z/CMakeLists.txt

cmake_policy(SET CMP0076 NEW)

add_library(kv11 STATIC)
set_target_properties(kv11 PROPERTIES LINKER_LANGUAGE C)

add_subdirectory(CMSIS)
add_subdirectory(components)
add_subdirectory(devices)

从这里开始,包含子目录的目录中只有一行 CMakeLists.txt,例如 bsp/CMakeLists.txt,除非它们中有源代码。

bsp/SDK_2.5.0_FRDM-KV11Z/CMSIS/Drivers/DriverTemplates/CMakeLists.txt

这是包含源文件的目录中的 CMakeLists.txt 文件的样子。

set(SOURCE_FILES
    Driver_CAN.c;
    Driver_ETH_MAC.c;
    Driver_ETH_PHY.c;
    Driver_Flash.c;
    Driver_I2C.c;
    Driver_MCI.c;
    Driver_SAI.c;
    Driver_SPI.c;
    Driver_USART.c;
    Driver_USBD.c;
    Driver_USBH.c;
)

target_sources(kv11 PUBLIC ${SOURCE_FILES})
target_include_directories(kv11 PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})

运行 CMake

使用以下命令调用构建:

rm -rf build
mkdir -p build
cd build
cmake -D CMAKE_BUILD_TYPE=Debug \
      -D CMAKE_TOOLCHAIN_FILE="arm-gcc-toolchain.cmake" \
      -D CMAKE_C_FLAGS="-fdiagnostics-color=always" \
      --verbose \
      ..
make

得到以下输出:

-- The C compiler identification is GNU 8.2.1
-- Check for working C compiler: /home/vagrant/toolchain/bin/arm-none-eabi-gcc
-- Check for working C compiler: /home/vagrant/toolchain/bin/arm-none-eabi-gcc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- The ASM compiler identification is GNU
-- Found assembler: /home/vagrant/toolchain/bin/arm-none-eabi-gcc
-- Configuring done
-- Generating done
-- Build files have been written to: /home/vagrant/nxp/build
make[1]: Entering directory '/home/vagrant/nxp/build'
make[2]: Entering directory '/home/vagrant/nxp/build'
make[3]: Entering directory '/home/vagrant/nxp/build'
Scanning dependencies of target kv11
make[3]: Leaving directory '/home/vagrant/nxp/build'
make[3]: Entering directory '/home/vagrant/nxp/build'
[  1%] Building C object bsp/SDK_2.5.0_FRDM-KV11Z/CMakeFiles/kv11.dir/CMSIS/Driver/DriverTemplates/Driver_CAN.c.obj

...

[100%] Linking C static library kv11
/home/vagrant/toolchain/bin/arm-none-eabi-ar: CMakeFiles/kv11.dir/CMSIS/Driver/DriverTemplates/Driver_CAN.c.obj: No such file or directory
make[3]: *** [bsp/SDK_2.5.0_FRDM-KV11Z/CMakeFiles/kv11.dir/build.make:895: bsp/SDK_2.5.0_FRDM-KV11Z/kv11] Error 1
make[3]: Leaving directory '/home/vagrant/nxp/build'
make[2]: *** [CMakeFiles/Makefile2:1013: bsp/SDK_2.5.0_FRDM-KV11Z/CMakeFiles/kv11.dir/all] Error 2
make[2]: Leaving directory '/home/vagrant/nxp/build'
make[1]: *** [Makefile:95: all] Error 2
make[1]: Leaving directory '/home/vagrant/nxp/build'
make: *** [Makefile:20: build2] Error 2

仔细查看编译命令

CMake 为对象之一创建的 GCC 调用:(已格式化)

cd /home/vagrant/nxp/build/bsp/SDK_2.5.0_FRDM-KV11Z && 
/home/vagrant/toolchain/bin/arm-none-eabi-gcc 
    --sysroot=/home/vagrant/toolchain/bin/../arm-none-eabi
    -I/home/vagrant/nxp/bsp/SDK_2.5.0_FRDM-KV11Z/CMSIS/Driver/DriverTemplates
    -I/home/vagrant/nxp/bsp/SDK_2.5.0_FRDM-KV11Z/CMSIS/Driver/Include
    -I/home/vagrant/nxp/bsp/SDK_2.5.0_FRDM-KV11Z/CMSIS/Include
    -I/home/vagrant/nxp/bsp/SDK_2.5.0_FRDM-KV11Z/components/lists
    -I/home/vagrant/nxp/bsp/SDK_2.5.0_FRDM-KV11Z/components/serial_manager
    -I/home/vagrant/nxp/bsp/SDK_2.5.0_FRDM-KV11Z/components/uart
    -I/home/vagrant/nxp/bsp/SDK_2.5.0_FRDM-KV11Z/devices/MKV11Z7
    -I/home/vagrant/nxp/bsp/SDK_2.5.0_FRDM-KV11Z/devices/MKV11Z7/arm
    -I/home/vagrant/nxp/bsp/SDK_2.5.0_FRDM-KV11Z/devices/MKV11Z7/cmsis_drivers
    -I/home/vagrant/nxp/bsp/SDK_2.5.0_FRDM-KV11Z/devices/MKV11Z7/drivers
    -I/home/vagrant/nxp/bsp/SDK_2.5.0_FRDM-KV11Z/devices/MKV11Z7/template
    -I/home/vagrant/nxp/bsp/SDK_2.5.0_FRDM-KV11Z/devices/MKV11Z7/utilities
    -I/home/vagrant/nxp/bsp/SDK_2.5.0_FRDM-KV11Z/devices/MKV11Z7/utilities/debug_console
    -I/home/vagrant/nxp/bsp/SDK_2.5.0_FRDM-KV11Z/devices/MKV11Z7/utilities/str
    -fdiagnostics-color=always
    -fsyntax-only
    -fno-common
    -ffunction-sections
    -fdata-sections
    -ffreestanding
    -fno-builtin
    -mthumb
    -mapcs
    -mcpu=cortex-m0plus
    -mfloat-abi=soft
    -std=gnu99
    -DCPU_MKV11Z128VLH7
    -DFRDM_KV11Z
    -DFREEDOM
    -MMD
    -MP
    -DDEBUG
    -g
    -O0
    -o CMakeFiles/kv11.dir/CMSIS/Driver/DriverTemplates/Driver_CAN.c.obj
    -c /home/vagrant/nxp/bsp/SDK_2.5.0_FRDM-KV11Z/CMSIS/Driver/DriverTemplates/Driver_CAN.c

这是运行之后,CMakeFiles/kv11.dir/CMSIS/Driver/DriverTemplates目录下没有Driver_CAN.c.obj文件。

导致无法创建中间 .obj 文件的 CMake 配置有什么问题,这导致库无法 link 正确。

我明白了。

我在调试一些初始文件时在工具链中打开了 -fsyntax-only,但忘记将其关闭。