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

时间:2019-05-13 21:59:13

标签: c cmake arm embedded nxp-microcontroller

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

系统信息

  • 系统:Linux Debian 10 4.19.0-4-amd64
  • CMake:3.13.4
  • ARM GNU工具链:8-2018-q4-major

文件

图书馆目录结构

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

运行此命令后,Driver_CAN.c.obj目录中没有CMakeFiles/kv11.dir/CMSIS/Driver/DriverTemplates文件。

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

1 个答案:

答案 0 :(得分:0)

我知道了。

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