CMake Basics: Compiling Static and Dynamic Libraries, Customizing Library Output Paths

CMake Basics: Compiling Static and Dynamic Libraries, Customizing Library Output Paths1. Generate a static library for third-party useIn Linux, the name of a static library is composed of: lib + library name + .a, where the library name needs to be specified, and lib and .a are generated automatically. The Windows system is similar.Project file structure:CMake Basics: Compiling Static and Dynamic Libraries, Customizing Library Output PathsCMakeLists.txt

# Set the minimum required CMake version to 3.15
cmake_minimum_required(VERSION 3.15)
# Set the project name to MyProject, version number to 1.0
project(MyProject VERSION 1.0)
# Set compilation options, set C++ standard to C++17
set(CMAKE_CXX_STANDARD 17)
# Force the compiler to support the specified C++ standard (C++17 here), error if not supported.
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# Set header file directory
include_directories(${PROJECT_SOURCE_DIR}/include)
# Get all source files in the current directory
# Note: ${CMAKE_CURRENT_SOURCE_DIR} refers to the current directory, i.e., the directory where CMakeLists.txt is located
# GLOB pattern, search for all files in the current directory
# GLOB_RECURSE means recursive search, i.e., search all subdirectories under the current directory
file(GLOB SRC_LIST ${CMAKE_CURRENT_SOURCE_DIR}/src/*.c)
# Add static library
add_library(my_static_lib STATIC ${SRC_LIST})

CMake Basics: Compiling Static and Dynamic Libraries, Customizing Library Output PathsCMake Basics: Compiling Static and Dynamic Libraries, Customizing Library Output Paths2. Generate a dynamic library for third-party useIn Linux, the name of a dynamic library is composed of: lib + library name + .so, where the library name needs to be specified, and lib and .so are generated automatically. The Windows system is similar.

# Set the minimum required CMake version to 3.15
cmake_minimum_required(VERSION 3.15)
# Set the project name to MyProject, version number to 1.0
project(MyProject VERSION 1.0)
# Set compilation options, set C++ standard to C++17
set(CMAKE_CXX_STANDARD 17)
# Force the compiler to support the specified C++ standard (C++17 here), error if not supported.
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# Set header file directory
include_directories(${PROJECT_SOURCE_DIR}/include)
# Get all source files in the current directory
# Note: ${CMAKE_CURRENT_SOURCE_DIR} refers to the current directory, i.e., the directory where CMakeLists.txt is located
# GLOB pattern, search for all files in the current directory
# GLOB_RECURSE means recursive search, i.e., search all subdirectories under the current directory
file(GLOB SRC_LIST ${CMAKE_CURRENT_SOURCE_DIR}/src/*.c)
# Add dynamic library
add_library(my_shared_lib SHARED ${SRC_LIST})

CMake Basics: Compiling Static and Dynamic Libraries, Customizing Library Output PathsCMake Basics: Compiling Static and Dynamic Libraries, Customizing Library Output Paths3. Set library output paths

# Set the minimum required CMake version to 3.15
cmake_minimum_required(VERSION 3.15)
# Set the project name to MyProject, version number to 1.0
project(MyProject VERSION 1.0)
# Set compilation options, set C++ standard to C++17
set(CMAKE_CXX_STANDARD 17)
# Force the compiler to support the specified C++ standard (C++17 here), error if not supported.
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# Set header file directory
include_directories(${PROJECT_SOURCE_DIR}/include)
# Get all source files in the current directory
# Note: ${CMAKE_CURRENT_SOURCE_DIR} refers to the current directory, i.e., the directory where CMakeLists.txt is located
# GLOB pattern, search for all files in the current directory
# GLOB_RECURSE means recursive search, i.e., search all subdirectories under the current directory
file(GLOB SRC_LIST ${CMAKE_CURRENT_SOURCE_DIR}/src/*.c)
# Set library file output path
set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)
# Add static library
add_library(my_static_lib STATIC ${SRC_LIST})
# Add dynamic library
add_library(my_shared_lib SHARED ${SRC_LIST})

CMake Basics: Compiling Static and Dynamic Libraries, Customizing Library Output PathsCMake Basics: Compiling Static and Dynamic Libraries, Customizing Library Output Paths

Leave a Comment