了解了安裝需要的基本概念和指令後, 今天就來實際安裝看看
CMakeLists.txt
cmake_minimum_required(VERSION 3.25)
project(Day21Sample CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
add_subdirectory(src)
src/lib/CMakeLists.txt
set(CMAKE_CXX_VISIBILITY_PRESET hidden)
set(CMAKE_VISIBILITY_INLINES_HIDDEN TRUE)
add_library(StaticLib STATIC
static_lib.cpp
)
add_library(SharedLib SHARED
shared_lib.cpp
)
set_target_properties(SharedLib PROPERTIES
VERSION 1.0.0
SOVERSION 1
)
target_sources(StaticLib PRIVATE
FILE_SET HEADERS
FILES ${PROJECT_SOURCE_DIR}/src/include/static_lib.h
BASE_DIRS ${PROJECT_SOURCE_DIR}/src/include
)
target_sources(SharedLib PRIVATE
FILE_SET HEADERS
FILES ${PROJECT_SOURCE_DIR}/src/include/shared_lib.h
BASE_DIRS ${PROJECT_SOURCE_DIR}/src/include
)
include(GNUInstallDirs)
install(TARGETS StaticLib SharedLib
EXPORT MyLibs
FILE_SET HEADERS
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)
install(EXPORT MyLibs
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}
NAMESPACE Day21Sample::
)
include(CMakePackageConfigHelpers)
write_basic_package_version_file(${PROJECT_NAME}ConfigVersion.cmake
VERSION 1.0.0
COMPATIBILITY AnyNewerVersion
)
configure_package_config_file(
${PROJECT_NAME}Config.cmake.in
${PROJECT_NAME}Config.cmake
INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}
)
install(
FILES
${PROJECT_BINARY_DIR}/src/lib/${PROJECT_NAME}ConfigVersion.cmake
${PROJECT_BINARY_DIR}/src/lib/${PROJECT_NAME}Config.cmake
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}
)
src/lib/Day21SampleConfig.cmake.in
@PACKAGE_INIT@
Configure, build 之後, 我們可以用下面的指令將 targets 或 libraries 安裝到 install
directory
cmake --install build --prefix include
且可以發現, 該 script 有用 write_basic_package_version()
來產生 Day21SampleConfig.cmake
也有用 configure_package_config_file()
產生 config 檔
可以參考今天的 Colab
下一篇, 我們就可以來打包我們的專案了~