[CMake] Installing and exporting multiple configurations of the same library

Domen Vrankar domen.vrankar at gmail.com
Sat Dec 16 14:56:11 EST 2017


2017-12-16 0:18 GMT+01:00 Saad Khattak <saadrustam at gmail.com>:

> Hi,
>
> I have 4 configurations (2 for Debug and 2 for Release) and I would like
> to install the libraries such that they are installed in the correct
> directories.
>
> Installing without worrying about configurations looks like this:
>
>     install(TARGETS ${LIB_NAME}
>         EXPORT ${LIB_NAME}Config
>         PUBLIC_HEADER DESTINATION "include/${LIB_NAME}"
>         LIBRARY DESTINATION "bin/${LIB_NAME}/"
>         ARCHIVE DESTINATION "lib/${LIB_NAME}/"
>         )
>
> However, different configurations overwrite the binaries. So instead, I
> did something like this (I'm going over all my configurations in a foreach):
>
>     install(TARGETS ${LIB_NAME}
>         CONFIGURATIONS DEBUG
>         EXPORT ${LIB_NAME}Config
>         PUBLIC_HEADER DESTINATION "include/${LIB_NAME}"
>         LIBRARY DESTINATION "bin/${LIB_NAME}/debug/"
>         ARCHIVE DESTINATION "lib/${LIB_NAME}/debug"
>         )
>
>     install(TARGETS ${LIB_NAME}
>         CONFIGURATIONS RELEASE
>         EXPORT ${LIB_NAME}Config
>         PUBLIC_HEADER DESTINATION "include/${LIB_NAME}"
>         LIBRARY DESTINATION "bin/${LIB_NAME}/release/"
>         ARCHIVE DESTINATION "lib/${LIB_NAME}/release/"
>         )
>
> however, that results in the error:
> CMake Error: INSTALL(EXPORT ...) includes target "MyLibrary" more than
> once in the export set.
>
> The error makes sense, in that I cannot have multiple exports in the same
> export set, in this case ${LIB_NAME}Config. However, I would like CMake to
> choose a different directory based on the configuration.
>

A simple solution would be to write

    install(TARGETS ${LIB_NAME}
        CONFIGURATIONS DEBUG
        EXPORT ${LIB_NAME}Config-d
        PUBLIC_HEADER DESTINATION "include/${LIB_NAME}"
        LIBRARY DESTINATION "bin/${LIB_NAME}/debug/"
        ARCHIVE DESTINATION "lib/${LIB_NAME}/debug"
        )

    install(TARGETS ${LIB_NAME}
        CONFIGURATIONS RELEASE
        EXPORT ${LIB_NAME}Config
        PUBLIC_HEADER DESTINATION "include/${LIB_NAME}"
        LIBRARY DESTINATION "bin/${LIB_NAME}/release/"
        ARCHIVE DESTINATION "lib/${LIB_NAME}/release/"
        )

and then use FILE parameter for install(EXPORT ... FILE
${LIB_NAME}Config.cmake ...) command to rename the file from
${LIB_NAME}Config-d.cmake to ${LIB_NAME}Config.cmake.

See here for details:
https://cmake.org/cmake/help/latest/command/install.html#installing-exports.

Also one thing that you can do is set:

set(CMAKE_DEBUG_POSTFIX "-debug")

so that your libraries when built in Debug mode will automatically get
-debug suffix (example: my_lib.so for Release and my_lib-debug.so for
debug). This way you don't need to install the library in two separate sub
directories (named debug/ and release/ in your case).


> Now there is a workaround... sort of. I could name the binaries based on
> the configuration but that doesn't work with our existing build systems. We
> want the following:
>
> lib/${LIB_NAME}/${CONFIG}/libname
>
> Any way to get CMake to install the libraries this way?
>

Not certain what the intended way of doing this debug/releas export on
Linux is as you can only build one build type at a time but the
install(EXPORT ...) command already generates:
${LIB_NAME}Config.cmake and ${LIB_NAME}Config-debug.cmake or
${LIB_NAME}Config-release.cmake

If you install the files in two separate directories and diff them you'll
notice that ${LIB_NAME}Config.cmake files (and Version files if you
generate one) are the same and -debug.cmake and -release.cmake differ so
possibly it's intended to simply override the two files and don't care
about it since the build specific files are exported under different names
and included automatically in ${LIB_NAME}Config.cmake file anyway.

I hope somebody else will shed some light on this part.

Regards,
Domen
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://cmake.org/pipermail/cmake/attachments/20171216/aa323f6e/attachment.html>


More information about the CMake mailing list