iT邦幫忙

2023 iThome 鐵人賽

DAY 21
0
Software Development

30 天 CMake 跨平台之旅系列 第 21

[Day 21] Relocatable project

  • 分享至 

  • xImage
  •  

本日內容

  • 來安裝吧!
  • 預告

Day21 - Colab

了解了安裝需要的基本概念和指令後, 今天就來實際安裝看看

來安裝吧!

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

預告

下一篇, 我們就可以來打包我們的專案了~


上一篇
[Day 20] 安裝 Project
下一篇
[Day 22] 打包我的 Project
系列文
30 天 CMake 跨平台之旅30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言