vcpkg的配置文件

jk9hmnmh  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(115)

作为一个库的作者,我需要提供什么配置来使消费者能够轻松安装库。

CMakeLists.txt

cmake_minimum_required(VERSION 3.26)
project(mylib C)

set(CMAKE_C_STANDARD 11)

add_library(mylib src/foo.c src/bar.c)
target_compile_features(mylib PRIVATE c_std_11)

target_include_directories(mylib PUBLIC
        $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
        $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)

include(GNUInstallDirs)
include(CMakePackageConfigHelpers)

# Config
configure_package_config_file(build/config.cmake
        ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}-config.cmake # cmake-build-debug/
        INSTALL_DESTINATION ${CMAKE_INSTALL_DATADIR}/${PROJECT_NAME} NO_SET_AND_CHECK_MACRO) # share/

# Version
write_basic_package_version_file(
        ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}-config-version.cmake
        VERSION 1.0.0
        COMPATIBILITY SameMajorVersion)

# Install Config & Version
install(FILES
        ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}-config.cmake # cmake-build-debug/
        ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}-config-version.cmake # cmake_build-debug/
        DESTINATION
        ${CMAKE_INSTALL_DATADIR}/${PROJECT_NAME}) # share/

# Create export set to enable import in other CMake Projects
install(TARGETS ${PROJECT_NAME} EXPORT ${PROJECT_NAME}-targets # create export set
        LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} # lib/
        ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} # lib/
        INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} # include/
)
# Install Export Set
install(EXPORT ${PROJECT_NAME}-targets
        NAMESPACE mylib::
        DESTINATION ${CMAKE_INSTALL_DATADIR}/${PROJECT_NAME})

# Install Headers
install(DIRECTORY ${CMAKE_SOURCE_DIR}/include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}) # include/

字符串

我的config.cmake

@PACKAGE_INIT@

include(${CMAKE_CURRENT_LIST_DIR}/mylib-targets.cmake)

check_required_components(mylib)


请查看并建议需要更改配置或CMakeLists文件的内容,以获得无缝的消费者体验。

编辑消费者

cmake_minimum_required(VERSION 3.26)
project(consumer C)

set(CMAKE_C_STANDARD 11)

add_executable(consumer main.c)

find_package(mylib CONFIG REQUIRED)
target_link_libraries(consumer PRIVATE mylib::mylib)

目录结构

mylib/
|-- CMakeLists.txt (top-level)
|-- src/
|   |-- CMakeLists.txt
|   |-- foo.c
|   |-- bar.c
|-- example/
|   |-- CMakeLists.txt
|   |-- example1.c
|   |-- example2.c

example/CMakeLists.txt

add_executable(example1 example1.c)
target_link_libraries(example1 PRIVATE mylib)
add_test(NAME TestExample1 COMMAND example1)

add_executable(example2 example2.c)
target_link_libraries(example2 PRIVATE mylib)
add_test(NAME TestExample2 COMMAND example2)

41zrol4v

41zrol4v1#

看来你已经做得很好了。它保证是干净的和x平台。
有一件事你没有按照Daniel Pfeifer's awesome presentation做:
当您在命名空间Foo::中导出Foo时,还要创建别名Foo::Foo。
add_library(Foo::Foo ALIAS Foo)
这样,当你和你的消费者想直接从源代码使用你的库时,他们仍然可以使用规范形式的命名空间:

find_package(mylib REQUIRED)
target_link_library(... PRIVATE mylib::mylib)

字符串
假设它们覆盖find_package API,如上面pdf中的第34页,类似于以下内容:

### NOTE!!! Hand written && NOT tested
set(as_subproj mylib)

macro(find_package)
    if(NOT "${ARG0}" IN_LIST as_subproj)
        _find_package(${ARGV})
    else()
        add_subdirectory(${ARG0})
    endif()
endmacro()

相关问题