# Link shared cmake target with prebuilt static libraries (PIC)

**URL:** <https://discourse.cmake.org/t/link-shared-cmake-target-with-prebuilt-static-libraries-pic/2504>\
**Category:** Code\
**Created:** [January 10, 2021, 5:53pm UTC](https://discourse.cmake.org/t/link-shared-cmake-target-with-prebuilt-static-libraries-pic/2504 "2021-01-10T17:53:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jazu.dev](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/j/dec6dc/32.png) [@jazu.dev](https://discourse.cmake.org/u/jazu.dev)\
**Post date:** [January 10, 2021, 5:53pm UTC](https://discourse.cmake.org/t/link-shared-cmake-target-with-prebuilt-static-libraries-pic/2504/1 "2021-01-10T17:53:32Z")

</div>

Greetings,

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
        ${INSTALL_PREFIX}/libA/${ANDROID_ABI}/lib/libA.a)

```

And:

```
target_include_directories(native-lib PRIVATE
        ${INSTALL_PREFIX}/libA/${ANDROID_ABI}/include
        ...
        )

```

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:

```
find_package(PkgConfig)
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

---

<div class="post-metadata">

**Author:** ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)\
**Post date:** [January 11, 2021, 2:44pm UTC](https://discourse.cmake.org/t/link-shared-cmake-target-with-prebuilt-static-libraries-pic/2504/2 "2021-01-11T14:44:02Z")

</div>

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](https://gitlab.kitware.com/cmake/cmake/issues) 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.

---

<div class="post-metadata">

**Author:** ![jazu.dev](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/j/dec6dc/32.png) [@jazu.dev](https://discourse.cmake.org/u/jazu.dev)\
**Post date:** [January 17, 2021, 4:46pm UTC](https://discourse.cmake.org/t/link-shared-cmake-target-with-prebuilt-static-libraries-pic/2504/3 "2021-01-17T16:46:13Z")

</div>

Thanks for the prompt reply and insights, let me check usage of \_STATIC variables. In the meantime I opened the [issue](https://gitlab.kitware.com/cmake/cmake/-/issues/21714) as suggested. Please let me know if you need further details

Best regards
