cmake simple config file example(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}libx86debuglib-files
${MY_LIB_PATH}libx86
eleaselib-files
${MY_LIB_PATH}libx64debuglib-files
${MY_LIB_PATH}libx64
eleaselib-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)
这会自动处理 IMPORTED
目标,还允许您嵌入编译器标志、包含路径、库依赖项,甚至哪些文件是您的界面的一部分(基本上,任何属于 IMPORTED
>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 简单配置文件示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:cmake 简单配置文件示例


基础教程推荐
- 常量变量在标题中不起作用 2021-01-01
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 我有静态或动态 boost 库吗? 2021-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 这个宏可以转换成函数吗? 2022-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01