cmake简单的配置文件示例 [英] cmake simple config file example

查看:154
本文介绍了cmake简单的配置文件示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我有了一个自己的图书馆,我想在另一个CMake c ++项目中使用它。像这样在我的计算机中存在。

Now I have a lib I made my self that I want to use in another CMake c++ project. It exists in my computer like this.

${MY_LIB_PATH}\include
${MY_LIB_PATH}\lib\x86\debug\lib-files
${MY_LIB_PATH}\lib\x86\release\lib-files
${MY_LIB_PATH}\lib\x64\debug\lib-files
${MY_LIB_PATH}\lib\x64\release\lib-files

一个基本的配置文件会是什么样,它使CMake find_package 知道这些?我希望它会非常简单,因为它没有太多信息可提供。 但是此页面只会伤到我的头。

What would a basic config file be like which makes CMake find_package know those? I expected it would be very simple because it just doesn't have much information to provide. But this page just make my head hurt.

对不起,我决定复制源代码,所以我真的不知道应该接受哪个答案。

Sorry, I decided to copy the source code around so I don't really know which answer should be accepted.

推荐答案

不要自己编写配置;使用CMake的 export 命令。范围太广,无法完整介绍,但这是我的一个项目的修改示例:

Don't write a config yourself; use CMake's export command. It's too broad to cover here in its entirety, but here's a modified example from one of my projects:

install(TARGETS
        your_target
    EXPORT YourPackageConfig
    ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
export(TARGETS
        your_target
    NAMESPACE YourPackage::
    FILE "${CMAKE_CURRENT_BINARY_DIR}/YourPackageConfig.cmake"
)
install(EXPORT
        YourPackageConfig
    DESTINATION "${CMAKE_INSTALL_DATADIR}/YourPackage/cmake"
    NAMESPACE YourPackage::
)

这将为您创建配置文件,以便其他项目可以通过 find_package 使用它。

This will create the config file for you, so other projects can use it via find_package.

find_package(YourPackage REQUIRED)
target_link_libraries(foo YouprPackage::your_target)

这处理了进口自动定位,并且还允许您嵌入编译器标志,包括路径,库依赖项,甚至是接口中包含哪些文件(基本上,属于 INTERFACE 属性)。

This handles the IMPORTED targets automatically, and also lets you embed compiler flags, include paths, library dependencies, and even which files are part of your interface (basically, anything that falls under the INTERFACE properties).

这篇关于cmake简单的配置文件示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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