2025-05-29 2:45 PM - edited 2025-05-30 8:04 AM
I'm trying to get the u8g2 library for OLED-displays to work, but I can't get it included properly.
When I'm adding source and lib files (e.g. test_io.c and test_io.h) to the appropriate core folders,
I usually do include the paths for every *.c file separately in the makelists.txt like shown here, which works fine:
# Add sources to executable
target_sources(${CMAKE_PROJECT_NAME} PRIVATE
# Add user sources here
../../Core/Src/test_io.c
)
But what is the right way to go with vscode, when adding a complete library, like u8g2 with a dozen files?
I found an example, where the complete u8g2 library folder (../csrc) is copied into the driver path and, for MXcube,
included via compiler setting.
Like this, in vscode I also copied the complete library to the drivers folder and added the include path to makelists.txt like this:
# Add include paths
target_include_directories(${CMAKE_PROJECT_NAME} PRIVATE
# Add user defined include paths
../../Drivers/csrc/ # No error while compiling, but doesn't seem to work
)
# Add linked libraries
target_link_libraries(${CMAKE_PROJECT_NAME}
stm32cubemx
# ../../Drivers/csrc/ # Failed with error while compiling
# Add user defined libraries
)
But that doesn't work at all.
I got errors on #include "u8g2.h" in my main.c file and vscode doesn't seem to recognize any of the files included in the ../../Drivers/csrc/ path.
Any ideas how to do this the right way?
EDIT:
I figured out something more:
Using STM32 extension in vscode, there are TWO CMakeLists.txt files.
First one created by CubeMX, created new every time you update your project (path: ${PROJ_PATH}/cmake/stm32cubemx/) and
the second created only once by CubeMX when starting a new project, to be edited by the user (path: ${PROJ_PATH}).
Adding the include path to the first CMakeLists.txt file like this works fine:
target_include_directories(stm32cubemx INTERFACE
../../Core/Inc
../../Drivers/STM32G0xx_HAL_Driver/Inc
../../Drivers/STM32G0xx_HAL_Driver/Inc/Legacy
../../Drivers/CMSIS/Device/ST/STM32G0xx/Include
../../Drivers/CMSIS/Include
../../Drivers/csrc # works fine
)
But the problem is, it gets deleted every time you update with CubeMX.
Adding the include path to the second CMakeLists.txt file like this doesn't work at all:
target_include_directories(stm32cubemx INTERFACE
../../Drivers/csrc # doesn't work
)
OR
target_include_directories(${CMAKE_PROJECT_NAME} PRIVATE
# Add user defined include paths
../../Drivers/csrc # doesn't work either
)
Now, to update my question:
How to get included directories in the second, user editable, CMakeLists.txt file work correctly?
Solved! Go to Solution.
2025-05-30 9:37 AM
# Add include paths
target_include_directories(${CMAKE_PROJECT_NAME} PRIVATE
# Add user defined include paths
${CMAKE_SOURCE_DIR}/Drivers/csrc
)
2025-05-30 9:37 AM
# Add include paths
target_include_directories(${CMAKE_PROJECT_NAME} PRIVATE
# Add user defined include paths
${CMAKE_SOURCE_DIR}/Drivers/csrc
)