问题描述
现在我有一个我自己制作的库,我想在另一个 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 简单配置文件示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!