Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele 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

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 4. Luku 2

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

bookBuilding a Shared Library

Pyyhkäise näyttääksesi valikon

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

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 4. Luku 2
some-alt