Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Building a Static Library | Libraries, Includes, and Build Types
Quizzes & Challenges
Quizzes
Challenges
/
Introduction to CMake

bookBuilding a Static Library

Note
Definition

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

main.c

src/hello.c

src/hello.c

src/hello.h

src/hello.h

CMakeLists.txt

CMakeLists.txt

copy
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

app/main.c

libmath/math.c

libmath/math.c

libmath/math.h

libmath/math.h

CMakeLists.txt

CMakeLists.txt

libmath/CMakeLists.txt

libmath/CMakeLists.txt

copy
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.

question mark

What command is used to link a static library to an executable in CMake?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 4. Kapittel 1

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

Suggested prompts:

Can you show me an example CMakeLists.txt for using a static library?

How do I organize my project folders for static libraries?

What is the difference between a static and a shared library in CMake?

bookBuilding a Static Library

Sveip for å vise menyen

Note
Definition

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

main.c

src/hello.c

src/hello.c

src/hello.h

src/hello.h

CMakeLists.txt

CMakeLists.txt

copy
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

app/main.c

libmath/math.c

libmath/math.c

libmath/math.h

libmath/math.h

CMakeLists.txt

CMakeLists.txt

libmath/CMakeLists.txt

libmath/CMakeLists.txt

copy
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.

question mark

What command is used to link a static library to an executable in CMake?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 4. Kapittel 1
some-alt