7
votes

I have created a project with a large number of link_directories() commands. I'd now like to store the resulting string of directories into a variable. For include_directories() this is easy using

get_property( test_INCLUDE_DIRECTORIES TARGET test PROPERTY INCLUDE_DIRECTORIES )

however there seems to be no LINK_DIRECTORIES property to do

get_property( test_LINK_DIRECTORIES TARGET test PROPERTY LINK_DIRECTORIES )

Is there a way to get a list of link directories used for a target?

(Note: I realize I could manually track the link directories in a variable myself and then use a single link_directories() but it doesn't seem very clean)

1
Why do you need the list as a variable?steveire
I want to send these link directories "up" to a parent project so the parent project will link against the libraries specified in the child correctly. I will eventually get around to using absolute library paths and so completely avoid link_directories() but for now......user2746401

1 Answers

7
votes

Take a look at the LINK_DIRECTORIES directory property.

The point is that link_directories operates on a per-directory basis (the command affects all targets defined in the same CMakeLists, as well as targets from all of its subdirectories), unlike, for instance, target_include_directories which works on a per-target basis.

You can query the value of the property with:

get_property(test_LINK_DIRECTORIES DIRECTORY PROPERTY LINK_DIRECTORIES)

if called from the same directory as the link_directories call. Otherwise, you need to give the (full or relative) path as an additional argument after DIRECTORY. Unfortunately, I know of no way to obtain the matching directory for an existing target.

Note that in general the use of link_directories is discouraged in CMake, which is probably the main reason why it's not supported very well. Unless you have very good reasons not to, you should always stick with full library paths passed to target_link_libraries. It will save you lots of headaches in the long run.