CMake: How to add a dependency that is not a "link" dependency

Viewed 527

I have a project configured by CMake. It has a program and a some shared libraries.

  • Some shared libraries are linked by the program (using target_link_libraries statement).
  • Some other shared libraries are not linked by the program, kind of plugins: they are loaded at runtime through LoadLibrary Win32 API.

We use Visual Studio 2015 as CMake target compiler. But from this IDE, when I start my program (press F5) after I modified some code, only the program and linked shared libraries are compiled. The "plugins" to be loaded at runtime are not compiled and so the code do not match the binary.

Is there a way to add a "build dependency", saying that some libraries should be compiled if out-of-date before a program is executed, even if this last one does not link them?

1 Answers

There is a CMake command exactly for this purpose: add_dependencies. It should do what you are looking for. Example:

add_executable(mainTarget SomeSource.cpp)
add_library(linkedLib SomeOtherSource.cpp)
add_library(libToBeLoaded MODULE MoreSource.cpp)

target_link_libraries(mainTarget PRIVATE linkedLib)

# This is it:
add_dependencies(mainTarget libToBeLoaded)
Related