Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get target filename without extension with CMake

Tags:

cmake

How can I get the basename of a target library with CMake?

I'm creating a library with the CMake variable name ${lib} as in

set(lib lib)
add_library(${lib} ...

I need the basename of the generated library file.

CMake will create a library with the filename <prefix>lib.<extension> where both <prefix> and <extension> are platform specific.

I've tried the generator $<TARGET_FILE_NAME:${lib}> which gives me the full name, which works because I'm needing this in a COMMAND, which is one of the few places where CMake allows generators.

To remove the extension using CMake I need to use GET_FILENAME_COMPONENT as in

GET_FILENAME_COMPONENT(<var> <filename> NAME_WE)

but this is used to set the variable <var> (in normal CMake fashion) and generators don't work there.

(The CMake language is an irregular abomination, if you ask me. CMake, as a concept, is pretty cool.)

like image 754
thoni56 Avatar asked Sep 20 '15 05:09

thoni56


2 Answers

I used get_property with get_filename_component and this worked for me:

get_property(lib_loc TARGET lib PROPERTY LOCATION)
get_filename_component(lib_we ${lib_loc} NAME_WE)
like image 189
a.v. Avatar answered Oct 10 '22 17:10

a.v.


Query the target's OUTPUT_NAME property:

get_target_property(_baseName lib OUTPUT_NAME)
like image 36
sakra Avatar answered Oct 10 '22 16:10

sakra