CMake: how to set an executable dependency to a custom target

I am using CMake on linux environment using cmake 2.8.1. I have also tried cmake 2.8.7 with the same results.

I need to do some special settings in an archive (static library). This is done as cmake custom_command. The result is a new archive to be used in the link of the executable. The input archive for conditioning is also the target of cmake.

I need a dependency between the conditional version of the archive and the executable. Using add_dependencies doesn't work and I don't understand why.

I created an example that shows the behavior of cmake. The example contains 2 C files, one for the archive and one containing main (). As a simple archive, I just make a copy of the previously created archive.

Here are the two source files:

main.c:

int myPrint(void);

int main(void)
{
  myPrint();
}

      

mylib.c:

#include <stdio.h>

int myPrint(void)
{
  printf("Hello World\n");
}

      

This is the CMakeLists.txt I created:

cmake_minimum_required(VERSION 2.8)

project(cmake_dependency_test)

add_library(mylib STATIC mylib.c)

add_custom_command(OUTPUT libmylib_conditioned.a
                   COMMAND cp libmylib.a libmylib_conditioned.a
                   DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/libmylib.a
                   COMMENT "Conditioning library")
add_custom_target(conditioning DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/libmylib_conditioned.a)
add_dependencies(conditioning mylib)

add_executable(cmake_dependency_test main.c)
add_dependencies(cmake_dependency_test conditioning)
target_link_libraries(cmake_dependency_test -L. mylib_conditioned)

      

First, I create an archive mylib. With add_custom_command I create the air conditioning system and add_custom_target provides the top level target for the conditional archive. Since I need to update the input archive before creating the conditional archive, I added a dependency between the top-level target of the input archive and the top-level target of the conditioned archive. This works great! When I touch mylib.c and run the setup, an input archive is created and after that a conditional archive is created.

Ok. Now I am using a conditional archive to bind. To have a dependency from the executable to the conditional archive, I used add_dependencies again. But touching the library source mylib.c and then running make will not update the executable as expected.

Where is the error in CMakeLists.txt?

+3


source to share


1 answer


You can try setting the target LINK_DEPENDS property for the target cmake_dependency_test .



...
add_executable(cmake_dependency_test main.c )  
set_property(TARGET cmake_dependency_test PROPERTY LINK_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/libmylib_conditioned.a)  
add_dependencies(cmake_dependency_test conditioning mylib)
target_link_libraries(cmake_dependency_test -L. mylib_conditioned)

      

+2


source







All Articles