Building a Static Library
A static library is a collection of compiled code that is copied directly into an executable at build time, making the program self-contained without requiring the library at runtime.
To create and use a static library in CMake, define it with add_library(... STATIC ...), then link it to your executable using target_link_libraries. Use target_include_directories to expose the library headers. CMake builds the library and links it automatically, allowing your program to use its functions.
main.c
src/hello.c
src/hello.h
CMakeLists.txt
123456#include "hello.h" int main() { say_hello(); return 0; }
When a static library is in another folder, include it with add_subdirectory, then link it using target_link_libraries. Use target_include_directories to expose the headers. This structure keeps projects organized and makes libraries easy to reuse.
app/main.c
libmath/math.c
libmath/math.h
CMakeLists.txt
libmath/CMakeLists.txt
12345678#include <stdio.h> #include "math.h" int main() { int result = add(3, 4); printf("3 + 4 = %d\n", result); return 0; }
A static library is a collection of object files bundled into a single archive. When linked, the library is copied directly into the executable, so no separate library file is needed at runtime. CMake handles this using add_library and target_link_libraries, resolving all dependencies during the build.
Merci pour vos commentaires !
Demandez à l'IA
Demandez à l'IA
Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion
Awesome!
Completion rate improved to 6.67
Building a Static Library
Glissez pour afficher le menu
A static library is a collection of compiled code that is copied directly into an executable at build time, making the program self-contained without requiring the library at runtime.
To create and use a static library in CMake, define it with add_library(... STATIC ...), then link it to your executable using target_link_libraries. Use target_include_directories to expose the library headers. CMake builds the library and links it automatically, allowing your program to use its functions.
main.c
src/hello.c
src/hello.h
CMakeLists.txt
123456#include "hello.h" int main() { say_hello(); return 0; }
When a static library is in another folder, include it with add_subdirectory, then link it using target_link_libraries. Use target_include_directories to expose the headers. This structure keeps projects organized and makes libraries easy to reuse.
app/main.c
libmath/math.c
libmath/math.h
CMakeLists.txt
libmath/CMakeLists.txt
12345678#include <stdio.h> #include "math.h" int main() { int result = add(3, 4); printf("3 + 4 = %d\n", result); return 0; }
A static library is a collection of object files bundled into a single archive. When linked, the library is copied directly into the executable, so no separate library file is needed at runtime. CMake handles this using add_library and target_link_libraries, resolving all dependencies during the build.
Merci pour vos commentaires !