-
Notifications
You must be signed in to change notification settings - Fork 18
Creating and using static libraries
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)Note When creating targeted libraries using
add_library(), CMake will automatically detect the corresponding IAR Archive Tool (<target>/bin/iarchive) during the configuration phase.
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.txtperform 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
- IAR Compiler options in a CMake project
- IAR ILINK options in a CMake project
- Language-specific target options
- Selecting build types
- Using Ninja Multi-Config
- Filing a build log
- Multi-file compilation
- Invoking IAR binary utilities
- Use the IAR ELF Tool to convert executable targets to their binary formats
- Using IAR Build Tools with CMake Presets