Skip to main content
Associate II
Jan 7, 2026 11:57 AM UTC
Solved

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

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 

unsigned_char_array
Lead III
Jan 7, 2026 12:44 PM UTC

./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."

5 replies

Andrew Neil
Super User
Jan 7, 2026 12:01 PM UTC

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
Jan 7, 2026 12:13 PM UTC

Thanks for reply

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

Andrew Neil
Super User
Jan 7, 2026 12:30 PM UTC

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.
unsigned_char_array
Lead III
Jan 7, 2026 12:44 PM UTC

./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."