Cmake:将子项目目标导出到主项目 [英] Cmake: Exporting subproject targets to main project
问题描述
我目前有一个名为 LIBS 的项目,其结构如下:
I currently have a project called LIBS with a structure like this:
├── Lib1
│ ├── CMakeLists.txt
│ ├── lib1-class.cpp
│ └── lib1-class.h
├── lib2
│ └── CMakeLists.txt
│ ├── lib2-class.cpp
│ ├── lib2-class.h
├── cmake
│ └── LIBSConfig.cmake.in
├── CMakeLists.txt
在主cmake文件中,我有:
in the main cmake file, I have:
install(
TARGETS
lib1
lib2
DESTINATION
${PROJECT_DIRNAME_lib}
EXPORT
${PROJECT_NAME}Exports
)
install(
EXPORT
${PROJECT_NAME}Exports
DESTINATION
${PROJECT_DIRNAME_lib}
)
我想将它们导出到find_package()可发现的包中.
as I want to export these in a package that is discoverable by find_package().
我的问题是我在各自的目录中生成了lib1
和lib2
,并且在安装它们时,Cmake告诉我
My problem is that I generate lib1
and lib2
in their respective directories and when installing them, Cmake tells me that
Error:install TARGETS given target "lib1" which does not exist in this directory.
根据建议此处,我的理解是我应该使用Export()
在lib1和lib2中,格式如下:
As suggested here, My understanding is that I should use Export()
and in lib1 and lib2, have something of the form:
export(TARGETS lib1 FILE lib1Exports.cmake)
在 LIBS 项目中,具有以下内容:
and in the LIBS project, have something like this:
ADD_LIBRARY(lib1 UNKNOWN IMPORTED)
set_property(TARGET lib1 PROPERTY IMPORTED_LOCATION lib1)
但是,它与我不喜欢使用从父项目添加的该库相同的名称.它告诉我:
However it does not like me using the same name for this library that is being added from the parent project. It tells me:
Error:add_library cannot create imported target "lib1" because another target with the same name already exists.
因此该库可用,并且可以链接到它,依此类推.如果要在父目录中创建另一个目标,但是我无法安装它.
so the library is available and I can link to it, etc. if I were to create another target in the parent directory, but I can't install it.
我在错误报告此处中发现了完全相同的问题,但我相信cmake现在处理事情的方式有所不同,而我只是做得不好. 那我做错了吗?我希望尽可能避免使用外部软件包.
I have found the exact same problem in a bug report here but I believe cmake handles things differently now and I am just not doing it correctly. So am I doing it wrong? I would like to avoid using external packages if possible.
更新:接受的解决方案仅在lib1,lib2之间没有依赖关系的情况下有效.在这种情况下,应使用此问题.
Update: the accepted solution works only for cases where there is no dependency between lib1, lib2. In that case one should use the solution provided to this question.
推荐答案
如您引用install()
命令的错误报告应该从创建目标的目录下发出.当您在不同目录中创建了库目标时,您需要为其分配不同的导出名称,并因此分配不同的导出文件.
As noted in the bugreport you refer to install()
command should be issued from the same directory where target is created. As you have libraries target created in different directories, you need to assign different export names for them, and, consequently, different export files.
但是您可以自由地将两个导出文件都包含在LIBSConfig.cmake
脚本中:
But you are free to include both export files into the LIBSConfig.cmake
script:
cmake/LIBSConfig.cmake :
get_filename_component(SELF_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH)
include(${SELF_DIR}/LIBS-lib1.cmake)
include(${SELF_DIR}/LIBS-lib2.cmake)
lib1/CMakeLists.txt :
add_library(lib1 ...)
install(TARGET lib1 EXPORT lib1-export ...)
lib2/CMakeLists.txt :
add_library(lib2 ...)
install(TARGET lib2 EXPORT lib2-export ...)
CMakeLists.txt :
add_subdirectory(lib1)
add_subdirectory(lib2)
install(EXPORT lib1-export FILENAME LIBS-lib1.cmake DESTINATION lib/LIBS)
install(EXPORT lib2-export FILENAME LIBS-lib2.cmake DESTINATION lib/LIBS)
install(FILES cmake/LIBSConfig.cmake DESTINATION lib/LIBS)
请注意, export 命令将导出 build 树.通常不适用于find_package
,通常用于查找已安装的文件.
Note, that export command exports build tree. It is usually not suitable for find_package
, which is normally used for find installed files.
这篇关于Cmake:将子项目目标导出到主项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!