使cmake库自动访问其他cmake包 [英] Making cmake library accessible by other cmake packages automatically

查看:180
本文介绍了使cmake库自动访问其他cmake包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目生成一个库:

 项目(myCoolLibrary)
ADD_LIBRARY(my_cool_library SHARED $ { mysources_SRC})

另一个应该使用此库的项目:



find_package(myCoolLibrary REQUIRED)
INCLUDE_DIRECTORIES($ {myCoolLibrary_INCLUDE_DIRS})
add_executable(myCoolExe $ {my_sources_SRC})
TARGET_LINK_LIBRARIES(myCoolExe $ {myCoolLibrary_LIBRARIES})

有什么方法可以更改第一个文件第二个文件自动工作?通过在第一个文件上运行cmake然后在输出上运行make,然后在第二个文件上运行cmake,cmake能够找到包吗?



解决方案

将我的第一个项目建立的地址评论成答案



@ daniperez - 在CMake项目(III)中使用CMake启用的库 - 我想出了以下最基本的解决方案:



myCoolLibrary / CMakeLists.txt

  cmake_minimum_required(VERSION 3.3)

project(myCoolLibrary)

函数(my_export_target _target _include_dir)
文件(
WRITE$ {CMAKE_CURRENT_BINARY_DIR} / $ {_ target} Config.cmake

include \ $ \ {CMAKE_CURRENT_LIST_DIR\} / $ {_ target} Targets.cmake\
set_property(
TARGET $ {_ target}
APPEND PROPERTY
INTERFACE_INCLUDE_DIRECTORIES \\ \\$ {_ include_dir} \




export(TARGETS $ {_ target} FILE$ {CMAKE_CURRENT_BINARY_DIR} / $ {_ target}注意:以下调用可能污染您的电脑的CMake包注册表
#查看以下注释/替代
export(PACKAGE $ {_ target})
endfunction(my_export_target)

...

add_library($ {PROJECT_NAME} SHARED $ {mysources_SRC})
my_export_target($ {PROJECT_NAME}$ { CMAKE_CURRENT_SOURCE_DIR})

myCoolExe / CMakeLists.txt b
$ b

  cmake_minimum_required(VERSION 3.3)

project(myCoolExe)

find_package(myCoolLibrary REQUIRED)

...

add_executable($ {PROJECT_NAME} $ {my_sources_SRC})
target_link_libraries($ {PROJECT_NAME} myCoolLibrary)

为了使其可重用,我将所有内容打包到 my_export_target()。我是 等自我宣传属性的朋友, INTERFACE_INCLUDE_DIRECTORIES



EDIT :由@ruslo使用导出(PACKAGE ...)可能会污染您的软件包注册表。您也可以:


  1. 将目标配置文件直接写入特定工具链的专用位置



    参见例如如何安装自定义CMake-Find模块 0003659:FIND_PACKAGE命令改进


  2. 设置 CMAKE_MODULE_PATH 通过第二个项目的CMake命令行(从外部注入搜索路径)。如果你正在用构建脚本构建两个项目,那么这是传播模块搜索路径的最直接的方式。


其他参考




I have one project that produces a library:

project (myCoolLibrary)
ADD_LIBRARY(my_cool_library SHARED ${mysources_SRC})

And another project that should be using this library:

find_package (myCoolLibrary REQUIRED)
INCLUDE_DIRECTORIES("${myCoolLibrary_INCLUDE_DIRS}" )
add_executable(myCoolExe ${my_sources_SRC} )
TARGET_LINK_LIBRARIES(myCoolExe ${myCoolLibrary_LIBRARIES} )

Is there a way that I can change the first file so that the second file works automatically? That by running cmake on the first file and then running make on the output, then running cmake on the second file, cmake is able to find the package?

An answer where I just give the address of where the first project is built to the second package is also acceptable.

解决方案

Turning my comment into an answer

Taking the code found in a blog post by @daniperez - Use CMake-enabled libraries in your CMake project (III) - I've come up with the following minimal solution:

myCoolLibrary/CMakeLists.txt

cmake_minimum_required(VERSION 3.3)

project(myCoolLibrary)

function(my_export_target _target _include_dir)
    file(
        WRITE "${CMAKE_CURRENT_BINARY_DIR}/${_target}Config.cmake"
        "
            include(\"\$\{CMAKE_CURRENT_LIST_DIR\}/${_target}Targets.cmake\")
            set_property(
                TARGET ${_target} 
                APPEND PROPERTY 
                    INTERFACE_INCLUDE_DIRECTORIES \"${_include_dir}\"
            )
        "
    )

    export(TARGETS ${_target} FILE "${CMAKE_CURRENT_BINARY_DIR}/${_target}Targets.cmake")

    # NOTE: The following call can pollute your PC's CMake package registry
    #       See comments/alternatives below 
    export(PACKAGE ${_target}) 
endfunction(my_export_target)

...

add_library(${PROJECT_NAME} SHARED ${mysources_SRC})
my_export_target(${PROJECT_NAME} "${CMAKE_CURRENT_SOURCE_DIR}")

myCoolExe/CMakeLists.txt

cmake_minimum_required(VERSION 3.3)

project(myCoolExe)

find_package(myCoolLibrary REQUIRED)

...

add_executable(${PROJECT_NAME} ${my_sources_SRC})
target_link_libraries(${PROJECT_NAME} myCoolLibrary)

To make it reusable I have packed everything into my_export_target(). And I'm friend of self-propagating properties like INTERFACE_INCLUDE_DIRECTORIES.

EDIT: As commented by @ruslo using export(PACKAGE ...) can pollute your package registry. So alternatively you can:

  1. Write the target config files directly to some dedicated place specific for a certain toolchain

    See e.g. How to install your custom CMake-Find module and 0003659: FIND_PACKAGE command improvements

  2. Set CMAKE_MODULE_PATH via the second project's CMake command line (injecting the search path(s) from the outside). If you are building the two projects anyway with a build script, then this is the most direct way to propagate the module search path(s).

Additional References

这篇关于使cmake库自动访问其他cmake包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆