我的项目结构如下:
── src
├── app
│ ├── first_app
│ │ ├── include
│ │ └── src
│ │ └── CMakeLists.txt
│ ├── second_app
│ │ └── ...
├── lib
│ ├── first_lib
│ | ├── include
│ | └── src
│ | └── CMakeLists.txt
│ ├── second_lib
│ | ├── include
│ | └── src
│ | └── CMakeLists.txt
├── CMakeLists.txt
first_app 同时使用 first_lib 和 second_lib 。我想强迫CMake使用
查找所有依赖项(库和包含标头) target_link_libraries(first_app first_lib::first_lib second_lib::second_lib)
。
我想问两个问题:
答案 0 :(得分:0)
假设您使用add_library(first_lib ...)
1)我会使用:
include(GNUInstallDirs)
install(TARGETS first_lib
EXPORT FirstLibTargets
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/first_lib
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
)
src:https://github.com/Mizux/cmake-cpp/blob/master/Foo/CMakeLists.txt#L24 然后您可以使用以下命令安装导出文件:
install(EXPORT FirstLibTargets
NAMESPACE first_lib::
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/FirstLib
COMPONENT Devel)
src:https://github.com/Mizux/cmake-cpp/blob/master/CMakeLists.txt
注意:我不知道您是否可以安装目标别名(即install(TARGETS first_lib::first_lib
)和/或具有不同名称空间的导出文件(即first_lib::
second_lib::
)
2)只需在first_lib / CMakeLists.txt中使用:
add_library(first_lib::first_lib ALIAS first_lib)
就在您的add_library之后。
因此,无论您使用Meta CMake(即first_lib::first-lib
)还是add_subdirectory()
,first_app始终可以使用find_package()
来检索此目标依赖项。
例如https://github.com/Mizux/cmake-cpp/blob/master/Foo/CMakeLists.txt