摘要

一个库生成后然后find_package来引用。下边的示例代码涉及到

dome代码下载

testLib.zip
GIT项目地址

库导出cmake说明

cmake_minimum_required(VERSION 3.5)
project(testLib VERSION 0.1.1 LANGUAGES C CXX)

set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_INSTALL_PREFIX ${CMAKE_BINARY_DIR}/../install)    # 指定安装目录

OPTION(BUILD_MODULE_test "test" OFF)                # 设置编译子模块
if (NOT BUILD_LIBRARY_TYPE)                         # 判定是否配置生成类型下拉项
    set (BUILD_LIBRARY_TYPE "Static" CACHE STRING "${BUILD_LIBRARY_TYPE_DESCR}" FORCE)  # 设置默认类型为静态库
    SET_PROPERTY(CACHE                              # 配置下拉选项
        BUILD_LIBRARY_TYPE PROPERTY STRINGS Shared Static   # 配置生成选择类型下拉项
    )
endif()

if ("${BUILD_LIBRARY_TYPE}" STREQUAL "Shared")
    set (BUILD_SHARED_LIBS ON)                      # 配置生成动态库(OFF/默认为静态库)
endif()

include_directories(include)                        # 添加头文件路径
file(GLOB FILES_SRC "src/*.cpp")                    # 添加当前目录下的源文件至FILES_SRC

add_library(${PROJECT_NAME} ${FILES_SRC})

ADD_CUSTOM_COMMAND(                                 # 用于将自定义构建规则添加到生成的构建系统
    TARGET ${PROJECT_NAME} POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_LIST_DIR}/config $<TARGET_FILE_DIR:${PROJECT_NAME}>/config   # 复制配置目录到可执行目录下
)

############################## 安装部分 ##############################

TARGET_INCLUDE_DIRECTORIES(                         # 目标头文件搜索路径
    ${PROJECT_NAME} PUBLIC                          
    $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>  # 指定构建接口头文件路径, 可以add_subdirectory后直接头文件
    $<INSTALL_INTERFACE:include>                    # install会自动引用include头文件
)

SET_TARGET_PROPERTIES(${PROJECT_NAME} PROPERTIES    # 设置目标的属性
    # DEBUG_POSTFIX d                                 # debug声称是添加d后缀
    RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}    # 二进制执行文件目录
    ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}    # 静态库目录
    LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}    # 动态库目录
)

INSTALL(
    TARGETS ${PROJECT_NAME}                         # 安装目标名称
    EXPORT ${PROJECT_NAME}Targets                   # 导出项目名
    PUBLIC_HEADER DESTINATION include               # 指定头文件目录
    RUNTIME DESTINATION "bin/$<$<CONFIG:Release>:Release>$<$<CONFIG:Debug>:Debug>"  # 二进制执行文件目录
    ARCHIVE DESTINATION "lib/$<$<CONFIG:Release>:Release>$<$<CONFIG:Debug>:Debug>"  # 静态库目录
    LIBRARY DESTINATION "lib/$<$<CONFIG:Release>:Release>$<$<CONFIG:Debug>:Debug>"  # 动态库目录
)

INSTALL(
    DIRECTORY                                       # 安装目录
    include DESTINATION ${CMAKE_INSTALL_PREFIX}     # 指定头文件安装目录
)

INSTALL(
    EXPORT ${PROJECT_NAME}Targets                   # 导出项目名
    FILE ${PROJECT_NAME}Targets.cmake               # 生成xxxTargets.cmake文件
    # NAMESPACE Ftest::                               # 添加命名空间
    DESTINATION cmake                               # 指定相对路径cmake目录
)


include (CMakePackageConfigHelpers)
configure_package_config_file (                     # 生成 xxxConfig.cmake文件
    cmake/Config.cmake.in ${PROJECT_NAME}Config.cmake   # 根据.make.in 生成对应Config.cmake文件
    INSTALL_DESTINATION cmake                       # 安装目录
    # PATH_VARS INCLUDE_DIRS LIB_DIR
    # INSTALL_PREFIX ${CMAKE_INSTALL_PREFIX}
)

write_basic_package_version_file(                   # 生成 xxxConfigVersion.cmake文件
    ${PROJECT_NAME}ConfigVersion.cmake              # 指定文件名
    VERSION ${PACKAGE_VERSION}                      # 指定版本号
    COMPATIBILITY SameMajorVersion                  # 指定兼容性
)

INSTALL(
    FILES
    ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake         # 复制xxxConfig.cmake文件到安装目录的cmake
    ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake  # 复制xxxConfigVersion.cmake文件到安装目录的cmake
    DESTINATION cmake                               # 指定相对路径
)

INSTALL(
    DIRECTORY
    config/                                         # 复制配置文件夹
    DESTINATION static                              # 指定目录
)

引入

直接find_package就可以直接将lib和头文件引入。

find_package(testLib REQUIRED)
target_link_libraries(${PROJECT_NAME} testLib::testLib)

使用

#include <iostream>
#include "testLib.h"

using namespace std;

int main(int, char**){
    std::cout << "Hello, from testMain!\n";
    auto pTestLib = make_shared<testLib>();
    pTestLib->say();
}

最终调用成功!
屏幕截图 2023-06-17 090831.jpg


本文由 管理员 创作,采用 知识共享署名 3.0,可自由转载、引用,但需署名作者且注明文章出处。

还不快抢沙发

添加新评论