CMake子目录安装/卸载目标

时间:2016-09-07 04:13:55

标签: cmake

我有一个包含两个子目录项目的项目(添加了add_subdirectory),每个项目都有自己的库,二进制文件和安装/卸载目标。所以:

main_project
|
|--CMakeLists.txt
|--src/
   |--CMakeLists.txt (with binary target, install()
|
|--project_a
   |--CMakeLists.txt
   |--src/
      |--CMakeLists.txt (with library, install())
|
|--project_b
   |--CMakeLists.txt
   |--src/
      |--CMakeLists.txt (with library, install())

我希望顶级项目(main_project)自动安装库a和b(包含在target_link_libraries()中的main_project中)。所以,我希望能够去:

cd main_project/build
cmake ..
make
sudo make install

并自动安装main_project二进制文件和project_a / b库。我试过这个:

main_project/src/CMakeLists.txt
...
install(FILES main project_a project_b DESTINATION bin
        LIBRARY DESTINATION lib)

cmake ..导致

install TARGETS given target "project_a" which does not exist in this directory.

正如所料。

我也尝试过指定路径:

main_project/src/CMakeLists.txt
...
install(FILES main ${CMAKE_SOURCE_DIR}/project_a/ ${CMAKE_SOURCE_DIR}/project_b DESTINATION bin
        LIBRARY DESTINATION lib)

还抱怨project_a / b不在此目录中(我猜也是如此?)

我还尝试使用install()中的FILES选项“手动”安装库,这样就可以了,但考虑到子项目中存在非常好的install(),这看起来非常糟糕。

另外一个问题:由于project_a和project_b也有uninstall()自定义目标,因此我无法在没有CMake抱怨已存在的自定义目标的情况下向main_project添加卸载目标。当我尝试将卸载指令添加到顶部目录CMakeLists时:

add_custom_target(uninstall
    COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake)

但是,由于我的project_a有一个卸载指令,我得到:

CMake Error at CMakeLists.txt:37 (add_custom_target):
  add_custom_target cannot create target "uninstall" because another
  target with the same name already exists.  The existing target is a
  custom target created in source directory "/main_project/project_a".
  See documentation for policy CMP0002 for more details.

那么,如何在我的main_project旁边的子项目中安装和卸载必要的库文件?

1 个答案:

答案 0 :(得分:1)

我发现了问题。我正在使用EXCLUDE_FROM_ALL添加我想要安装的子目录,这样它就不会在子目录中构建所有内容,只需要构建我需要的库。该标志似乎阻止了子目录install()的发生。也许ExternalProject_Add确实是去这里的最佳方式......

此外,RE覆盖自定义目标,这对我有用:http://public.kitware.com/pipermail/cmake/2011-July/045269.html