8

I have

ExternalProjectAdd(googletest ......)
...
add_library(gtest_main UNKNOWN IMPORTED)
set_target_properties(gtest_main PROPERTIES
    "IMPORTED_LOCATION" ${binary_dir}/googlemock/gtest/libgtest_main.a
)
...
add_executable(sometest somefile.cpp)
target_link_library(sometest gtest_main)
add_dependencies(sometest googletest)

But evidently stating the dependency of the executable sometest on googletest does not actually require the ExternalProject_Add's build command to be invoked prior to the build command for sometest.

If I run ninja then it complains that it can't find libgtest_main.a. But if you call ninja googletest && ninja sometest it works just fine.

So, how do you tell cmake that sometest depends on googletest's build command being invoked?

lanza
  • 1,512
  • 2
  • 13
  • 26
  • Call `add_dependencies(sometest googletest)` which you currently use should do the trick. Show more details about `ExternalProject_Add` call. (E.g. check that build directory for external project isn't `googletest/...`: not sure about ninja, but makefile definitely doesn't like a directory and a target with the same name.) – Tsyvarev Jan 07 '18 at 22:40
  • personally I prefer `add_dependencies(gtest_main googletest_project)` and since you executable depend on `gtest_main` .... – Mizux Jan 08 '18 at 10:08
  • I have the exact same problem. It seems to me like the problem only occurs with ninja. Simply switching to make runs the build command for the ExternalProject before attempting to link. I don't know if that's a ninja problem then or a cmake problem. – paulgessinger May 09 '18 at 08:00
  • Hit the same with ninja, solved with make or building the dependency first. – phcerdan Oct 10 '18 at 23:19

1 Answers1

2

As mentioned in the comments, the issue is related to Ninja generator and its mechanism of scanning dependencies (more details).

For solve the issue need to add additional Cmake option BUILD_BYPRODUCTS with expected path to library that will be compiled:

ExternalProject_add(
    ...
    BUILD_BYPRODUCTS ${CMAKE_BINARY_DIR}/googlemock/gtest/libgtest_main.a
)

This should help to get rid of error like this:

ninja: error: 'gtest/libgtest_main.a', needed by 'sometest.so', missing and no known rule to make it

I understand that answer is quite late, but hope that it will help someone safe few hours.

Pavel K.
  • 983
  • 9
  • 21