find_package(Boost)如何与静态库和共享库一起使用?

时间:2019-02-18 14:52:42

标签: c++ cmake shared-libraries static-libraries

我目前正在开发一个具有多个库和多个可执行文件的项目。一些可执行文件将静态链接某些库,而其他可执行文件将动态链接。我的问题是如何选择使用find_package静态或动态链接库。我注意到boost可以在调用Boost_USE_STATIC_LIBS之前设置find_package的选项,但是我不确定Boost如何使用它。 boost是否会同时建立共享/静态的,然后Boost_USE_STATIC_LIBS才选择目标?我是严格需要使用MODULE模式还是可以使用CONFIG模式,使用任何一种模式进行这种行为的收益/成本是多少?

我当前的方法使用配置模式,因为库所需要做的只是调用rn_lib(<target_name>),一切都已经为它们设置好了。我猜测选择静态vs共享目标的逻辑是在库的config / find文件中完成的。

期望的行为

set(<lib_name>_USE_STATIC_LIBS TRUE)
find_package(<lib_name> REQUIRED)

当前库的CMakeLists.txt

function(rn_lib)
cmake_parse_arguments(RN_LIB
    ""
    "NAME"
    "SRCS;HEADERS"
    ${ARGN}
)

if (RN_LIB_NAME)
    set(target_name ${RN_LIB_NAME})
else()
    set(target_name ${PROJECT_NAME})
endif()

set(static_target_option ${target_name}_STATIC)
option(${static_target_option} "Force build as static library" OFF)

set(_config_file ${target_name}_config.h)
configure_file("cmake/${_config_file}.in" ${CMAKE_CURRENT_BINARY_DIR}/${_config_file})

if (${PROJECT_VERSION})
    set(project_version ${PROJECT_VERSION})
else()
    set(project_version 1.0)
endif()

if (static_target_option)
    message("Build Static ${target_name}")
    add_library(${target_name} STATIC
        ${RN_LIB_SRCS}
        ${RN_LIB_HEADERS}
    )
else()
    message("Build Shared ${target_name}")
    # This could still produce a static library
    add_library(${target_name}
        ${RN_LIB_SRCS}
        ${RN_LIB_HEADERS}
    )
endif()

# setup lib variables
set(include_install_dir include)
set(include_install_abs ${RN_INSTALL_PATH}/${include_install_dir})
set(config_install_dir lib/cmake/${target_name})

set(generated_dir ${CMAKE_CURRENT_BINARY_DIR}/generated)
set(version_config ${generated_dir}/${target_name}ConfigVersion.cmake)
set(project_config ${generated_dir}/${target_name}Config.cmake)

set(package_namespace RustyNail::)
set(target_export_name ${target_name}Targets)

install(
        FILES ${RN_LIB_HEADERS}
        DESTINATION ${include_install_dir}/${base_name}
)

target_include_directories(
        ${target_name}
        PUBLIC
            $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
            $<INSTALL_INTERFACE:${include_install_dir}>
)

write_basic_package_version_file(
        ${version_config}
        VERSION ${project_version}
        COMPATIBILITY  AnyNewerVersion
)

configure_package_config_file(
        ${RN_CMAKE_PATH}/Config.cmake.in
        ${project_config}
        INSTALL_DESTINATION ${config_install_dir}
)

install(
        TARGETS ${target_name}
        EXPORT ${target_export_name}
        LIBRARY DESTINATION lib
        ARCHIVE DESTINATION lib
        RUNTIME DESTINATION bin
        INCLUDES DESTINATION ${include_install_dir}
)

install(
        FILES ${project_config} ${version_config}
        DESTINATION ${config_install_dir}
)

install(
        EXPORT ${target_export_name}
        NAMESPACE ${package_namespace}
        DESTINATION ${config_install_dir}
)
endfunction()

项目树

- < root>
  - output dir
    - lib
    - bin
    - build
  - src
    - lib
      - lib1
      - lib2
    - apps
      - app1
      - app2

0 个答案:

没有答案