22
votes

Is it possible to use install(TARGETS ...) with targets that are defined in directories added with add_subdirectory?

My use case is, that I want to build e.gg an rpm for gtest. the gtest project happens to have a CMakeLists.txt without any install statements. I want to build the package without adding those statements to the CMakeLists.txt of gtest.

I have this resulting directory structure:

+ gtest-1.5.0/...
+ CMakeLists.txt 

The CMakeLists of gtest-1.5.0 defines libraries like this:

cxx_static_library(gtest "${cxx_strict}" src/gtest-all.cc)
cxx_static_library(gtest_main "${cxx_strict}" src/gtest_main.cc)
target_link_libraries(gtest_main gtest)

now i want to add something like this to my CMakeLists.txt:

add_subdirectory(gtest-1.5.0)
install(TARGETS gtest gtest_main ARCHIVE DESTINATION lib)

but cmake correctly states:

CMake Error at CMakeLists.txt:10 (install):
  install TARGETS given target "gtest" which does not exist in this
  directory.

Is there a way to do this without patching gtest-1.5.0?

1
It seems this limitation has been lifted, see stackoverflow.com/a/34444949/227322villintehaspam

1 Answers

7
votes

You could try using file install rather than install targets. The downside is that you will have to handle shared and static builds.

install(FILES gtest-1.5.0/gtest_main.so DESTINATION lib)