Displaying a target's list of linked libraries in cmake

get_target_property(OUT Target LINK_LIBRARIES)
message(STATUS ${OUT})

Actually not(*).

However, you can use a cmake variable to collect the name of the libraries that you want to link (using the set( ... or the list(APPEND ... command), and then use this variable in your target_link_libraries command:

target_link_libraries(<targetname> ${YOUR_CMAKE_VARIABLE})

The same variable can also be used to create your copy commands (for example using this custom target)


(*) A similar question was asked here, and it got no definitive answer.


I realise this doesn't fully answer the question with regards doing it within cmake, but I faced a similar problem and thought I should share my solution.

First, in your source directory ("project"):

$ mkdir build && cd build
$ cmake ..

Then, use graphviz to create a dot file, as in this answer:

$ cmake --graphviz=graph.dot .

Then, strip out the dependencies from the graph for your target (let's call it "foo"):

$ sed -n 's/.*label="\(.*\)"\s.*/\1/p' graph.dot.foo > foo_dependencies.txt

Now, remove the clutter:

$ rm graph.dot*

Tags:

Cmake