# Encapsulating Link Arguments

**URL:** https://discourse.cmake.org/t/encapsulating-link-arguments/1171
**Category:** Usage
**Created:** [May 8, 2020, 5:01pm UTC](https://discourse.cmake.org/t/encapsulating-link-arguments/1171 "2020-05-08T17:01:48Z")
**Posts on this page:** 1
**Showing post:** 2

<div class="post-metadata">

### Author: ![marc.chevrier](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/ecb155/32.png) [@marc.chevrier](https://discourse.cmake.org/u/marc.chevrier)
#### Post date: [May 9, 2020, 1:23pm UTC](https://discourse.cmake.org/t/encapsulating-link-arguments/1171/2 "2020-05-09T13:23:03Z")

</div>

First, it is not a good practice to specify link options through `target_link_libraries()` command. It is preferable to use `target_link_options()`.  
And also, it is strongly recommended using the up-to-date syntax regarding `target_link_libraries()` by specifying one of following keyword `PUBLIC`, `PRIVATE` or `INTERFACE`.  
And to finish, to avoid unexpected de-duplicated link options, it is required to use the `SHELL:` prefix.

Here is possible approach:

```cmake
add_library(jni_component_helper STATIC ${jni_src})
target_link_libraries(jni_component_helper PRIVATE dep1 dep2)

add_library(jni_component STATIC "")
# add dependency of jni_component over jni_component_helper
target_link_libraries (jni_component PRIVATE jni_component_helper)
# ensure the required link option is specified for the helper library only
target_link_options(jni_component INTERFACE "SHELL:-Wl,-whole-archive $<TARGET_FILE:jni_component_helper> -Wl,-no-whole-archive")

```

And for the final step:

```cmake
add_subdirectory(jni_component_dir)
add_library(final_lib SHARED ${final_lib_src})
target_link_libraries(final_lib PRIVATE jni_component ... other libs...)

```

---

_[View the full topic](https://discourse.cmake.org/t/encapsulating-link-arguments/1171)._
