如何在cmake中使用COMPONENTS配置项目

问题描述 投票:1回答:1

我想使用cmake创建一个项目项目,可以通过与Poco使用的方式类似的方式访问。我发现使用Poco作为一个例子是拥挤的,很难遵循,所以我试图创建一个最小的版本,没有宏,以便我可以看到发生了什么。我在这里为这个例子构建了一个存储库。

https://github.com/markeastwood82/nomnoms

这个以及下面的内容是我最好的猜测,在阅读/解决“现代CMake”几天之后如何解决这个问题,除了它不太有效。基本上我有一个库noms与组件fruitveg我想从应用程序munch动态链接。我可以安装noms库,但无法用munch找到它。有人可以帮我把这个东西放在一起吗?

这两个项目的结构如下:

noms
|---- CMakeLists.txt
+---- fruit
|     |---- CMakeLists.txt
|     |---- fruit-config.cmake.in
|     +---- src
|     |     |----apple.cpp
|     |
|     +---- include/noms/fruit
|           |----apple.h
|
+---- veg
      |---- CMakeLists.txt
      |---- veg-config.cmake.in
      +---- src
      |     |---- asparagus.cpp
      |
      +---- include/noms/veg
            |---- asparagus.h
munch
|---- CmakeLists.txt
+---- src
      |---- main.cpp

文件noms/CMakeLists.txt包含以下内容。

cmake_minimum_required(VERSION 3.0)
set(project noms)
set(version 1.0.0)

project(${project})

add_subdirectory(fruit)
add_subdirectory(veg)

# UPDATE: implement advice from Tsyvarev

configure_file(${project}-config.cmake
  "${CMAKE_BINARY_DIR}/${project}-config.cmake"
  @ONLY
)

include(CMakePackageConfigHelpers)
write_basic_package_version_file(
  "${CMAKE_BINARY_DIR}/${project}-config-version.cmake"
  VERSION ${version}
  COMPATIBILITY AnyNewerVersion
)

install(
  FILES
    "${CMAKE_BINARY_DIR}/${project}-config.cmake"
  DESTINATION lib/cmake/${project}
)

文件noms/fruit/CMakeLists.txt(和noms/veg/CMakeLists.txt几乎相同)是

set(component fruit)

add_library(${component} SHARED src/apple.cpp)

# namespaced alias
add_library(${project}::${component} ALIAS ${component})

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

install(TARGETS ${component} EXPORT ${component}-targets
  COMPONENT ${component}
  LIBRARY DESTINATION lib
  ARCHIVE DESTINATION lib
  RUNTIME DESTINATION bin
  INCLUDES DESTINATION include
)

install(EXPORT ${component}-targets
  FILE "${project}-${component}-targets.cmake"
  NAMESPACE ${project}::
  DESTINATION lib/cmake/${project}
  COMPONENT ${component}
)

# This seems like a kludge, but it does place the file in the correct location
# on my machine (Ubuntu 18.04). Idea taken from Poco
configure_file("${component}-config.cmake.in"
  "${CMAKE_BINARY_DIR}/${project}-${component}-config.cmake"
  @ONLY
)

include(CMakePackageConfigHelpers)
write_basic_package_version_file(
  "${CMAKE_BINARY_DIR}/${project}-${component}-config-version.cmake"
  VERSION ${version}
  COMPATIBILITY AnyNewerVersion
)

install(
  FILES
    "${CMAKE_BINARY_DIR}/${project}-${component}-config.cmake"
    "${CMAKE_BINARY_DIR}/${project}-${component}-config-version.cmake"
  DESTINATION lib/cmake/${project}
  COMPONENT ${component}
)

# DESTINATION will be automatically prefixed by ${CMAKE_INSTALL_PREFIX}
install(
  DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include
  COMPONENT ${component}
  DESTINATION ${CMAKE_INSTALL_PREFIX}
)

最后,noms/fruit/fruit-config.cmake.in的文件(和noms/veg/veg-config.cmake几乎完全相同)是

include(${CMAKE_CURRENT_LIST_DIR}/noms-fruit-config-version.cmake)
include(${CMAKE_CURRENT_LIST_DIR}/noms-fruit-config-targets.cmake)

对于项目munch,文件munch/CMakeLists.txt是简单的

cmake_minimum_required(VERSION 3.0)
set(project munch)

find_package(noms REQUIRED COMPONENTS fruit)

add_executable(${project} src/main.cpp)

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

target_link_libraries(${project}
  PUBLIC
    noms::fruit
)

安装noms之后我尝试构建munch,但是cmake无法找到noms-config.cmake文件。我不知道它应该包含什么,也不知道我应该把它放在哪里。我也不确定在configure_file中使用noms/<food-type>/CMakeLists.txt的方法是否合适,因为这里的例子似乎并不需要

https://github.com/boostcon/cppnow_presentations_2017/blob/master/05-19-2017_friday/effective_cmake__daniel_pfeifer__cppnow_05-19-2017.pdf

我的问题与此处提出的问题基本相同,目前尚未得到解答(接受的答案是错误的,正如我在该主题中所评论的那样)。实际上是公平的,因为该线程已有6年历史,在编写本文时可能是正确的,但似乎没有使用现在推荐的xxx-config.cmake方法。

How to export libraries with components in CMAKE?

如果我犯了任何错误,或者任何可以做得更好的事情,请随时为我解读。谢谢

*更新*

除了上面更新的noms/CMakeLists.txt文件,我根据以下@Tsyvarev的建议实现了以下noms/noms-config.cmake文件...

foreach(component ${noms_FIND_COMPONENTS})
    include(${CMAKE_CURRENT_LIST_DIR}/noms-${component}-config.cmake)
endforeach()

结果代码现在有效,谢谢!


c++ cmake components
1个回答
2
投票

CMake不会自动处理COMPONENTS列表。它将该任务留给noms-config.cmake脚本,当发出命令时搜索并执行该脚本

find_package(noms COMPONENTS fruit veg)

来自find_package文档:

在Config模式下,find_package会自动处理REQUIREDQUIET[version]选项,但会将其留给包配置文件,以便以对包有意义的方式处理组件。

在此配置文件中,您可以从COMPONENTS变量中提取所请求组件的列表(通过find_package()noms_FIND_COMPONENTS选项),并执行适当的操作。例如:

NOMS-config.cmake:

foreach(component ${noms_FIND_COMPONENTS})
  # For requested component, execute its "config" script
  include(${CMAKE_CURRENT_LIST_DIR}/${component}-config.cmake)
endforeach()

这意味着,您将脚本noms-config.cmake安装到lib/cmake/noms子目录中,靠近您已在项目中安装的其他.cmake脚本。

© www.soinside.com 2019 - 2024. All rights reserved.