-
Notifications
You must be signed in to change notification settings - Fork 16
Creating and using static libraries
Felipe Torrezan edited this page Mar 23, 2024
·
2 revisions
This interactive example describes the configuration details for when, in a CMake project, an executable target make use of an user library.
When a project complexity increases, it might make sense to break down the complexity into multiple targets such as an executable along one or more user libraries. In such cases, organizing libraries in subdirectories is a good practice. These subdirectories usually contain their own CMakeLists.txt
which are later consumed by an executable target. A CMake project example like this is provided at examples/libs:
Project files |
---|
lib/inc/crc32.h |
lib/src/crc32.c |
lib/CMakeLists.txt |
CMakeLists.txt |
main.c |
The main()
function uses the library's crc32()
function.
- Prepare the library by filling the missing information in
lib/CMakeLists.txt
(click to show/hide answers):
TODO 1: Add the lib
library target
add_library(lib)
TODO 2: Configure the lib
library sources
target_sources(lib PRIVATE src/crc32.c)
TODO 3: Using the PUBLIC
scope, expose the lib
headers (inc) to other targets
target_include_directories(lib PUBLIC inc)
- In the application's
CMakeLists.txt
perform the following tasks (click to show/hide answers):
TODO 4: Add the lib
subdirectory (contains the lib
target)
add_subdirectory(lib)
TODO 5: Link the lib
library target against the libs
executable target
target_link_libraries(libs lib)
- Finally, build and test the project. Refer to the tutorial for more information.
This is the cmake-tutorial wiki. Back to Wiki Home
- Setting language-specific target options
- Selecting build types
- Using Ninja Multi-Config
- Filing a build log
- Multi-file compilation
- Invoking IAR binary utilities