Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMake: How to set dependency from executable to custom target

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

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

What I need is a dependency between the conditioned version of the archive and the executable. Using add_dependencies doesn't work and I don't undestand why.

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

Here are the 2 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 the archive mylib. With add_custom_command I do the conditioning and add_custom_target provides a top level target for the conditioned archive. Since I need to update the input archive before the conditioned archive is built I added a dependency between the input archive top level target and the conditioned archive top level target. This works perfectly! When I touch mylib.c and run make conditioning the input archive is built and after that the conditioned archive.

Ok. Now I use the conditioned archive for linking. In order to have a dependency from the executable to the conditioned archive I used again add_dependencies. But touching the library source mylib.c and then running make will not update the executable as expected.

Where is the mistake in the CMakeLists.txt?

like image 550
user1995197 Avatar asked Dec 14 '25 20:12

user1995197


1 Answers

You can try to set the target property LINK_DEPENDS 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)
like image 164
guini Avatar answered Dec 17 '25 08:12

guini



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!