tags:

views:

35

answers:

1

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?

A: 

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)

RobertJMaynard
ok .. this could work, but i don't understand, why i can use the defined names in target_link_libraries and not in install commands!
Gizmomogwai
the Install command only looks at the current directory when figuring out if a target exists, unlike target_link_libraries which scans all targets.
RobertJMaynard
Ok ... thanks for the explanation, I thought something like this, but I was not sure (could you edit your install(FILES command, because install(FILES does not take the ARCHIVE parameter).
Gizmomogwai