[CMake] Determining a library name at configure time

Brad King brad.king at kitware.com
Tue Sep 22 14:57:03 EDT 2015


On 09/18/2015 02:42 PM, Bill Newcomb wrote:
> Suppose I add a library in my project:
> 
> add_library(foo STATIC foo.c bar.c)
> 
> Is there a way I can get the name of the generated library file at 
> configure time?

On multi-configuration generators like VS and Xcode there may be
more than one library file, one for each configuration like Debug
and Release.  For this reason CMake does not always know the
path to the final file at configure time.  Instead one typically
needs to use a placeholder of some kind that will be substituted
for each build configuration at generate time.

What do you actually want to do with the library file name?  There
are several common and supported use cases.  In add_custom_command
COMMAND lines for example one can use the $<TARGET_FILE:foo>
generator expression:

 http://www.cmake.org/cmake/help/v3.3/manual/cmake-generator-expressions.7.html
 http://www.cmake.org/cmake/help/v3.3/command/add_custom_command.html

> get_target_property(libname foo LIBRARY_OUTPUT_NAME)

That property is only for setting a non-default name.  It contributes
to the computation of the actual file name which may be specific to
the platform.

-Brad



More information about the CMake mailing list