Skip to main content
Associate II
January 7, 2026
Solved

How to add external libraries in my project using Vscode and CMake

  • January 7, 2026
  • 2 replies
  • 457 views

Hi for all,

I use Cube MX to generate code with CMake and then use Vscode with needed extension to compile my code.

My question is : i want to use library that contain multiple folders to drive stepper motor, how to add this library to my project.

best regard for all 

thanks for any reply 

Best answer by unsigned_char_array

./CMakeLists.txt:

...

# 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
STM32Step/src/stepper/GPIOStepper.cpp
# etc.
)

# Add include paths
target_include_directories(${CMAKE_PROJECT_NAME} PRIVATE
# Add user defined include paths
STM32Step/src
STM32Step/src/timer
# etc.
)

target_compile_definitions(${CMAKE_PROJECT_NAME} PRIVATE
# Add user compile definitions here
)

# Add linked libraries
target_link_libraries(${CMAKE_PROJECT_NAME}
stm32cubemx
# Add user defined libraries
)

...

 

I wrote a guide for more advanced modifications: https://community.st.com/t5/stm32cubemx-mcus/how-to-customize-your-stm32cubemx-generated-cmake-project/td-p/857290

2 replies

Andrew Neil
Super User
January 7, 2026

Is it a source code library (.c and .h files), or a precompiled binary library (.a) ?

From your post, it sounds like the former?

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
EliteAuthor
Associate II
January 7, 2026

Thanks for reply

it is source code but there are multiple .h and .c files in multiple folders   

Andrew Neil
Super User
January 7, 2026

So the question is really just, "how to add multiple .h and .c files in multiple folders?"

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
Lead II
January 7, 2026

./CMakeLists.txt:

...

# 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
STM32Step/src/stepper/GPIOStepper.cpp
# etc.
)

# Add include paths
target_include_directories(${CMAKE_PROJECT_NAME} PRIVATE
# Add user defined include paths
STM32Step/src
STM32Step/src/timer
# etc.
)

target_compile_definitions(${CMAKE_PROJECT_NAME} PRIVATE
# Add user compile definitions here
)

# Add linked libraries
target_link_libraries(${CMAKE_PROJECT_NAME}
stm32cubemx
# Add user defined libraries
)

...

 

I wrote a guide for more advanced modifications: https://community.st.com/t5/stm32cubemx-mcus/how-to-customize-your-stm32cubemx-generated-cmake-project/td-p/857290

"Kudo posts if you have the same problem and kudo replies if the solution works.Click ""Accept as Solution"" if a reply solved your problem. If no solution was posted please answer with your own."