Alex Norell
Alex Norell

Reputation: 21

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

The intermediate .obj files aren't being created when building a static C library using CMake and the ARM GNU Toolchain.

System Information

Files

Library Directory Structure

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

Top Level 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)

From here, there are just single line CMakeLists.txt in the directories with child directories, like in bsp/CMakeLists.txt, unless they have sources in them.

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

This is how the CMakeLists.txt file in directories with source files look.

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})

Running CMake

The build is invoked using the following commands:

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

which nets the following output:

-- 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

Taking a closer look at the compile commands

GCC call created by CMake for one of the objects: (formated)

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

After this is run, there is no Driver_CAN.c.obj file in the CMakeFiles/kv11.dir/CMSIS/Driver/DriverTemplates directory.

What is wrong with the CMake Configuration that is causing the intermediate .obj files to not be created, which causes the library to not link properly.

Upvotes: 1

Views: 325

Answers (1)

Alex Norell
Alex Norell

Reputation: 21

I figured it out.

I turned on -fsyntax-only in the toolchain when debugging some initial files and forgot to turn it off.

Upvotes: 1

Related Questions