Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele Creating Static and Shared Libraries with CMake | Building and Packaging Libraries
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
C++ Library Development

bookCreating Static and Shared Libraries with CMake

To build robust C++ libraries, you need to understand how to use CMake to generate both static and shared libraries from your source code. CMake is a cross-platform build system generator that simplifies the process of managing complex builds. When you build a static library, you generate an archive file with a .a (on Unix-like systems) or .lib (on Windows) extension. A shared library, on the other hand, is a dynamically linked file with a .so (on Unix-like systems) or .dll (on Windows) extension.

Begin by creating a CMakeLists.txt file in your project root. This file describes your project to CMake and tells it how to build your code. To declare a project that will build both static and shared libraries, use the add_library command. The add_library command takes a target name, the library type (STATIC or SHARED), and a list of source files. For example, to build libmath, you might write:

add_library(math_static STATIC math.cpp)
add_library(math_shared SHARED math.cpp)

This creates two targets: one static and one shared. You can set properties such as the version, output directory, and whether to use position independent code (important for shared libraries). For shared libraries, you often need to ensure that symbols are exported correctly. On Windows, this usually requires using __declspec(dllexport) and __declspec(dllimport), but on Unix-like systems, symbols are exported by default unless specified otherwise.

To instruct CMake to build both library types, you can use multiple add_library commands, each with its own target and type. You can then install these libraries or link them to your applications as needed. The typical workflow involves:

  1. Creating your source and header files;
  2. Writing a CMakeLists.txt that defines both static and shared library targets;
  3. Running cmake to generate the build system;
  4. Building the project with your chosen build tool (such as make or ninja);
  5. Using or installing the resulting .a and .so/.dll files.

Common CMake commands for this process include project(), add_library(), target_include_directories(), and install(). Always ensure you specify the correct target type (STATIC or SHARED) and manage symbol visibility appropriately for cross-platform compatibility.

math.cpp

math.cpp

math.h

math.h

CMakeLists.txt

CMakeLists.txt

copy
123456
#include "math.h" int add(int a, int b) { return a + b; }
question mark

Which CMake command is used to create a shared library from math.cpp?

Select the correct answer

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 2. Luku 2

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

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

bookCreating Static and Shared Libraries with CMake

Pyyhkäise näyttääksesi valikon

To build robust C++ libraries, you need to understand how to use CMake to generate both static and shared libraries from your source code. CMake is a cross-platform build system generator that simplifies the process of managing complex builds. When you build a static library, you generate an archive file with a .a (on Unix-like systems) or .lib (on Windows) extension. A shared library, on the other hand, is a dynamically linked file with a .so (on Unix-like systems) or .dll (on Windows) extension.

Begin by creating a CMakeLists.txt file in your project root. This file describes your project to CMake and tells it how to build your code. To declare a project that will build both static and shared libraries, use the add_library command. The add_library command takes a target name, the library type (STATIC or SHARED), and a list of source files. For example, to build libmath, you might write:

add_library(math_static STATIC math.cpp)
add_library(math_shared SHARED math.cpp)

This creates two targets: one static and one shared. You can set properties such as the version, output directory, and whether to use position independent code (important for shared libraries). For shared libraries, you often need to ensure that symbols are exported correctly. On Windows, this usually requires using __declspec(dllexport) and __declspec(dllimport), but on Unix-like systems, symbols are exported by default unless specified otherwise.

To instruct CMake to build both library types, you can use multiple add_library commands, each with its own target and type. You can then install these libraries or link them to your applications as needed. The typical workflow involves:

  1. Creating your source and header files;
  2. Writing a CMakeLists.txt that defines both static and shared library targets;
  3. Running cmake to generate the build system;
  4. Building the project with your chosen build tool (such as make or ninja);
  5. Using or installing the resulting .a and .so/.dll files.

Common CMake commands for this process include project(), add_library(), target_include_directories(), and install(). Always ensure you specify the correct target type (STATIC or SHARED) and manage symbol visibility appropriately for cross-platform compatibility.

math.cpp

math.cpp

math.h

math.h

CMakeLists.txt

CMakeLists.txt

copy
123456
#include "math.h" int add(int a, int b) { return a + b; }
question mark

Which CMake command is used to create a shared library from math.cpp?

Select the correct answer

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 2. Luku 2
some-alt