Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Building a Shared Library | Libraries, Includes, and Build Types
Quizzes & Challenges
Quizzes
Challenges
/
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

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 4. Розділ 2

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

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

Свайпніть щоб показати меню

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

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 4. Розділ 2
some-alt