Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Building a Shared Library | Libraries, Includes, and Build Types
Introduction to CMake

bookBuilding a Shared Library

Note
Definition

A shared library is a collection of reusable code that is loaded into a program at runtime instead of being embedded into the executable, allowing multiple programs to use the same library file.

A shared library is created in CMake by using the SHARED keyword with the add_library command. Unlike a static library, a shared library is not embedded into the executable. Instead, it is loaded dynamically at runtime and can be used by multiple programs at the same time.

CMakeLists.txt

CMakeLists.txt

copy
1234567891011
cmake_minimum_required(VERSION 3.10) project(MySharedLibraryProject C) # Create the shared library add_library(myshared SHARED myshared.c) # Create the executable add_executable(main main.c) # Link the library to the executable target_link_libraries(main PRIVATE myshared)

In this example, myshared is built as a shared library, and the executable main is linked against it. When the program runs, the operating system loads the library dynamically instead of copying the code into the executable.

Note
Note

Shared libraries must be available at runtime. They are usually placed in the same directory as the executable or in a system library path so the operating system can find them.

Static vs Shared Libraries

When choosing between static and shared libraries, consider how your program will be used and deployed.

question mark

Which of the following is a characteristic of shared libraries?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 4. Kapitel 2

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

Suggested prompts:

Can you explain the main differences between static and shared libraries?

When should I use a static library instead of a shared library?

What are the advantages and disadvantages of using shared libraries?

bookBuilding a Shared Library

Swipe um das Menü anzuzeigen

Note
Definition

A shared library is a collection of reusable code that is loaded into a program at runtime instead of being embedded into the executable, allowing multiple programs to use the same library file.

A shared library is created in CMake by using the SHARED keyword with the add_library command. Unlike a static library, a shared library is not embedded into the executable. Instead, it is loaded dynamically at runtime and can be used by multiple programs at the same time.

CMakeLists.txt

CMakeLists.txt

copy
1234567891011
cmake_minimum_required(VERSION 3.10) project(MySharedLibraryProject C) # Create the shared library add_library(myshared SHARED myshared.c) # Create the executable add_executable(main main.c) # Link the library to the executable target_link_libraries(main PRIVATE myshared)

In this example, myshared is built as a shared library, and the executable main is linked against it. When the program runs, the operating system loads the library dynamically instead of copying the code into the executable.

Note
Note

Shared libraries must be available at runtime. They are usually placed in the same directory as the executable or in a system library path so the operating system can find them.

Static vs Shared Libraries

When choosing between static and shared libraries, consider how your program will be used and deployed.

question mark

Which of the following is a characteristic of shared libraries?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 4. Kapitel 2
some-alt