baryonics
baryonics

Reputation: 11

How do I include a library in a generated CMakeLists.txt (CubeMX)?

I'm currently trying to make the jump from Arduino to STM HAL and have just started writing libraries for some modules I have lying around. I'm new to this and confused. I am using the STM32 VSCode plugin and have it generate the CMakeLists.txt for me.

I am having trouble including the libraries:

This is the main CMakeLists.txt

cmake_minimum_required(VERSION 3.22)

#
# This file is generated only once,
# and is not re-generated if converter is called multiple times.
#
# User is free to modify the file as much as necessary
#

# Setup compiler settings
set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_C_EXTENSIONS ON)


# Define the build type
if(NOT CMAKE_BUILD_TYPE)
    set(CMAKE_BUILD_TYPE "Debug")
endif()

# Set the project name
set(CMAKE_PROJECT_NAME stm32f4-sensor-demo)

# Include toolchain file
include("cmake/gcc-arm-none-eabi.cmake")

# Enable compile command to ease indexing with e.g. clangd
set(CMAKE_EXPORT_COMPILE_COMMANDS TRUE)

# Enable CMake support for ASM and C languages
enable_language(C ASM)

# Core project settings
project(${CMAKE_PROJECT_NAME})
message("Build type: " ${CMAKE_BUILD_TYPE})

# Create an executable object type
add_executable(${CMAKE_PROJECT_NAME})

# Add STM32CubeMX generated sources
add_subdirectory(cmake/stm32cubemx)

add_subdirectory(Libs/hmc5883l-stm32f4)

add_subdirectory(Libs/dht11-stm32f4)

# Link directories setup
target_link_directories(${CMAKE_PROJECT_NAME} PRIVATE
    # Add user defined library search paths
)

# Add sources to executable
target_sources(${CMAKE_PROJECT_NAME} PRIVATE
    # Add user sources here
)

# Add include paths
target_include_directories(${CMAKE_PROJECT_NAME} PRIVATE
    # Add user defined include paths
)

# Add project symbols (macros)
target_compile_definitions(${CMAKE_PROJECT_NAME} PRIVATE
    # Add user defined symbols
)

# Add linked libraries
target_link_libraries(${CMAKE_PROJECT_NAME}
    stm32cubemx
    hmc5883l-stm32f4
    dht11-stm32f4
    # Add user defined libraries
)

CMakeLists.txt of DHT11:

cmake_minimum_required(VERSION 3.22)

add_library(dht11-stm32f4 STATIC
    Src/dht11-stm32f4.c
)

# Include directories for this library
target_include_directories(dht11-stm32f4 PUBLIC
    ${CMAKE_CURRENT_SOURCE_DIR}/Inc
)

# Link dependencies for this library
target_link_libraries(dht11-stm32f4 PRIVATE stm32cubemx)

CMakeLists.txt of HMC5883l:

cmake_minimum_required(VERSION 3.22)

add_library(hmc5883l-stm32f4 STATIC
    Src/hmc5883l-stm32f4.c
)

# Include directories for this library
target_include_directories(hmc5883l-stm32f4 PUBLIC
    ${CMAKE_CURRENT_SOURCE_DIR}/Inc
)

# Link dependencies for this library
target_link_libraries(hmc5883l-stm32f4 PRIVATE stm32cubemx)

Libs folder Structure:

Libs
 ┣ dht11-stm32f4
 ┃ ┣ Inc
 ┃ ┃ ┗ dht11-stm32f4.h
 ┃ ┣ Src
 ┃ ┃ ┗ dht11-stm32f4.c
 ┃ ┣ .git
 ┃ ┣ CMakeLists.txt
 ┃ ┣ LICENSE
 ┃ ┗ README.md
 ┗ hmc5883l-stm32f4
 ┃ ┣ Inc
 ┃ ┃ ┗ hmc5883l-stm32f4.h
 ┃ ┣ Src
 ┃ ┃ ┗ hmc5883l-stm32f4.c
 ┃ ┣ .git
 ┃ ┣ CMakeLists.txt
 ┃ ┣ LICENSE
 ┃ ┗ README.md

If I remove all the entries for one of the two libraries in my main CMake file, everthing works fine, but as soon as I add the other everything breaks...I Am definitely missing something here. I like C, but CMake is pain.

As mentioned, if I add just one of the two libraries, everything works perfectly. I have already tried finding a solution with ChatGPT, but it always comes up with the exact structure I have already tested.

EDIT:

[build] FAILED: Libs/hmc5883l-stm32f4/CMakeFiles/hmc5883l-stm32f4.dir/__/__/Core/Src/main.c.obj 
....
[build] /opt/st/stm32cubeclt_1.17.0/GNU-tools-for-STM32/bin/arm-none-eabi-gcc -DDEBUG -DSTM32F401xE 
: fatal error: dht11-stm32f4.h: No such file or directory
[build]    24 | #include "dht11-stm32f4.h"
[build]       |          ^~~~~~~~~~~~~~~~~
[build] compilation terminated.
[build] ninja: build stopped: subcommand failed.

If stm32cubemx is not linked in Libs/hmc5883l-stm32f4 (by removing stm32cubemx from target_link_libraries(hmc5883l-stm32f4 PRIVATE stm32cubemx)) , it strangely works. I have already generated a new project with test libraries lib_a and lib_b in the same configuration, but without any code, just for testing purposes. And with that, it works as well, even when linking stm32cubemx. I think I just have a small stupid mistake somewhere. But I can't find it.

Upvotes: 1

Views: 88

Answers (0)

Related Questions