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 简单配置文件示例
基础教程推荐
- 使用从字符串中提取的参数调用函数 2022-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 从 std::cin 读取密码 2021-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01