Link shared cmake target with prebuilt static libraries (PIC)


In my CMakeLists.txt I declare a shared library target (for Android/JNI):

add_library(native-lib SHARED native-lib.cpp)

That depends on an external static library, which then depends upon a bunch of other static libraries (discoverable through pkg-config). I have been able to make this work by manually declaring all the dependencies, direct + transitive through a series of:

add_library(libA STATIC IMPORTED)
set_target_properties(libA PROPERTIES IMPORTED_LOCATION


target_include_directories(native-lib PRIVATE

But this is becoming pretty tedious so I’d like to take advantage of pkg-config discovery and keep CMakeLists simple. I have come up with a FindLibA.cmake script that uses pkg-config to discover all dependencies:

pkg_check_modules(LibA REQUIRED IMPORTED_TARGET libA)

And then in CMakeLists.txt I have:

target_link_libraries(native-lib PkgConfig::LibA)

(I know using ::PkgConfig here is not the best practice but I wanted to keep this as simple as possible)

When I build I notice from the output of the compile step that the header paths are properly populated with the help of pkg-config, but the link step fails to use proper -L... and -l... for the dependencies and breaks. Seems that cmake is trying to use the shared versions of the target link attributes (i.e. LibA_LDFLAGS) instead of the static (i.e. LibA_STATIC_LDFLAGS), as I can verify from the CMakeCache.txt

Is there a way to fix this and instruct cmake to use static attributes instead?

Thanks in advance

There are variables for static information, but I’m not seeing anything that makes a PkgConfig::LibA-static target or the like. That should probably be added. Please file an issue requesting this to be implemented.

In the meantime, using the _STATIC variables rather than the imported target from pkg_check_modules would be the way to go.

Thanks for the prompt reply and insights, let me check usage of _STATIC variables. In the meantime I opened the issue as suggested. Please let me know if you need further details

Best regards