乐闻世界logo
搜索文章和话题

How to create a static library with CMake?

2个答案

1
2

Creating a static library is a common requirement when building projects with CMake. A static library is a collection of compiled code that can be linked to the program during compilation, rather than being dynamically loaded at runtime. Below, I will provide a detailed explanation of how to create a static library in CMake, along with a practical example.

Step 1: Prepare Source Code

First, prepare the source code intended for compilation into a static library. Assume we have a simple project containing two files: library.cpp and library.h.

library.h

cpp
#ifndef LIBRARY_H #define LIBRARY_H void print_hello(); #endif

library.cpp

cpp
#include "library.h" #include <iostream> void print_hello() { std::cout << "Hello, Static Library!" << std::endl; }

Step 2: Write the CMakeLists.txt File

Next, you need to write a CMakeLists.txt file to instruct CMake on how to compile these source files and create a static library.

CMakeLists.txt

cmake
cmake_minimum_required(VERSION 3.10) # Specify the minimum CMake version requirement project(MyStaticLibrary) # Define the project name add_library(mylib STATIC # Create the static library `mylib` library.cpp library.h )

Here, the add_library command is used to create a new library. mylib is the name of the library, STATIC specifies that we are creating a static library, followed by the source files to be compiled into the library.

Step 3: Compile the Project

To compile this library, execute the following commands:

  1. Create a build directory and enter it:
bash
mkdir build cd build
  1. Run CMake to configure the project and generate the build system:
bash
cmake ..
  1. Compile the code:
bash
cmake --build .

After executing these commands, you will find the compiled static library file (e.g., libmylib.a, which may vary by platform) in the build directory.

Summary

Through the above steps, we successfully created a static library using CMake. This method is widely used in practical development as it helps modularize code, improve code reusability, and simplify the management of large projects.

2024年6月29日 12:07 回复

When using CMake to create a static library, you first need to write a CMakeLists.txt file to define the project's build configuration. The following outlines the steps and example for creating a simple static library.

1. Project Structure

Assume the following simple project structure:

shell
/my_library |-- include | |-- mylib.h |-- src | |-- mylib.cpp |-- CMakeLists.txt

In this structure:

  • The include directory contains all header files.
  • The src directory contains all source code files.
  • The CMakeLists.txt file is the CMake configuration file we need to write.

2. Writing CMakeLists.txt

In the CMakeLists.txt file, we need to specify the project name, the minimum required CMake version, the header file directory, and the source files to be compiled into a static library.

cmake
cmake_minimum_required(VERSION 3.10) # Specifies the minimum required CMake version project(MyLibrary) # Defines the project name # Specifies the header file directory include_directories(include) # Compiles source files in the src directory into a static library named mylib file(GLOB_RECURSE SOURCES "src/*.cpp") add_library(mylib STATIC ${SOURCES})

3. Compiling the Project

In the project root directory (where the CMakeLists.txt file is located), run the following commands to generate the Makefile and compile the project:

bash
mkdir build cd build cmake .. make

These commands first create a build directory to store the compiled files, then use cmake .. to generate the Makefile, and finally use make to compile the project.

4. Using the Static Library

The generated static library is typically named libmylib.a (on Unix-like systems) and located in the build directory. To use this library in another project, add the header file directory and the library file directory to the project, then link the library.

Summary

Through this example, we can see that creating a static library with CMake is a straightforward process, primarily involving writing an appropriate CMakeLists.txt file to specify how to compile source files into a static library. This method is highly flexible and can be easily extended to larger projects.

2024年6月29日 12:07 回复

你的答案