Path to target output file

Cmake

Cmake Problem Overview


I have a .so library target created by add_library, and need to pass an absolute path to this library to an external script. Now I have ${LIBRARY_OUTPUT_PATH}/${CMAKE_SHARED_LIBRARY_PREFIX}LangShared${CMAKE_SHARED_LIBRARY_SUFFIX} for that (LIBRARY_OUTPUT_PATH is defined in my CMakeLists.txt). This looks like hard-coding to me, because it will break as soon as the target is renamed or some of its properties are changed. Is there a way to get an absolute path to add_library's output?

Cmake Solutions


Solution 1 - Cmake

You should use a [generator expression][0] for this.

From the docs for [add_custom_command][1] and the docs for generator expressions:

> Arguments to COMMAND may use "generator expressions"...

> Generator expressions are evaluted during build system generation to produce information specific to each build configuration.

In this case, assuming your library target is called "MyLib", the generator expression representing the full path to the built library would be:

$<TARGET_FILE:MyLib>

[0]: http://www.cmake.org/cmake/help/v3.3/manual/cmake-generator-expressions.7.html "CMake v3.3 documentation for generator expressions" [1]: http://www.cmake.org/cmake/help/v3.3/command/add_custom_command.html "CMake v3.3 documentation for "add_custom_command" command"

Solution 2 - Cmake

Try:

get_property(fancy_lib_location TARGET fancy_lib PROPERTY LOCATION)
message (STATUS "fancy_lib_location == ${fancy_lib_location}")

Where fancy_lib is the target created with add_library (fancy_lib SHARED ...).

I found that works directly with Makefile generators, but there is more work to be done for Visual Studio generators since the value of fancy_lib_location is not what you would expect:

  1. fancy_lib_location will contain an embedded reference to a Visual-Studio-specific $(OutDir) reference that you will have to replace with the value of the CMAKE_BUILD_TYPE CMake variable (which resolves to something like Debug, or Release).
  2. At least for CMake 2.8.1, and at least on Visual Studio targets, and if you have set the CMAKE_DEBUG_POSTFIX variable, then it will not be included in the value (which may or may not be a bug, I don't know).

Solution 3 - Cmake

To expand on the answer by @bgooddr, here is a CMake function to get the location of a target:

function(get_fancy_lib_location)
	set(options)
	set(multiValueArgs LIB)
	set(oneValueArgs LOCATION)
	cmake_parse_arguments(get_fancy_lib_location "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN} )
	message (STATUS "fancy_lib  == ${get_fancy_lib_location_LIB}")

	get_property(fancy_lib_location TARGET "${get_fancy_lib_location_LIB}" PROPERTY LOCATION)
	message (STATUS "fancy_lib_location == ${fancy_lib_location}")

	set(${get_fancy_lib_location_LOCATION} ${fancy_lib_location})
endfunction()

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDmitry RisenbergView Question on Stackoverflow
Solution 1 - CmakeFraserView Answer on Stackoverflow
Solution 2 - CmakebgoodrView Answer on Stackoverflow
Solution 3 - CmakeBlue7View Answer on Stackoverflow