How to include external (shared) library with several dlls/dylibs

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

How to include external (shared) library with several dlls/dylibs

Kai Wolf

I need to integrate an externally built (shared) library as a logical
build target into my CMake configuration which consists of several
shared library files (dlls/dylibs).
Usually I'd do the following:

    find_path(MyLib_INCLUDE_DIR mylib.h HINTS ${MyLib_PATH}/include)
    find_library(MyLib_LIBRARY NAMES MyLib HINTS ${MyLib_PATH}/bin)
    find_library(MyLib_LIBRARY_DEBUG NAMES MyLib_d ${MyLib_PATH}/bin)

    find_package_handle_standard_args(MyLib DEFAULT_MSG

    if(MyLib_FOUND AND NOT TARGET MyLib::MyLib)
      set(MyLib_LIBRARIES ${MyLib_LIBRARY})
      set(MyLib_INCLUDE_DIRS ${MyLib_INCLUDE_DIR})

      add_library(MyLib::MyLib UNKNOWN IMPORTED)
      set_target_properties(MyLib::MyLib PROPERTIES

        mark_as_advanced(MyLib_INCLUDE_DIR MyLib_LIBRARY MyLib_LIBRARY_DEBUG)

My understanding is that add_library(... UNKOWN IMPORTED) will CMake
figure out shared/static libraries by itself (and based on the option
BUILD_SHARED_LIBS probably, but this shouldn't be necessary). However,
CMake doesn't allow to add a list of libraries to the property
IMPORTED_LOCATION. For Instance, in case I want to import MyLib,
MyLibFoo and MyLibBar both as debug and release builds, this is not
possible using the listed approach.

I'm aware of add_library(MyLib::MyLib INTERFACE IMPORTED) and set a
list of libraries to the property INTERFACE_LINK_LIBRARIES, but this
is problematic in the case of creating relocatable packages (as
mentioned in the CMake documentation). Furthermore, there's no such
property as INTERFACE_LINK_LIBRARIES_DEBUG, thus it would only be
possible to refer to either the release or debug builds of my
externally imported library.

So, how am I supposed to import my externally built library as an
relocatable-ready logical CMake target which actually includes several
either debug or release build shared library files?

Thanks in advance.

Powered by

Please keep messages on-topic and check the CMake FAQ at:

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support:
CMake Consulting:
CMake Training Courses:

Visit other Kitware open-source projects at

Follow this link to subscribe/unsubscribe: