# Usage of INTERFACE\_LINK\_LIBRARIES with namespaced targets

**URL:** https://discourse.cmake.org/t/usage-of-interface-link-libraries-with-namespaced-targets/330
**Category:** Code
**Created:** [December 6, 2019, 1:29pm UTC](https://discourse.cmake.org/t/usage-of-interface-link-libraries-with-namespaced-targets/330 "2019-12-06T13:29:07Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Martin\_Stolpe](https://discourse.cmake.org/user_avatar/discourse.cmake.org/martin_stolpe/32/188_2.png) [@Martin\_Stolpe](https://discourse.cmake.org/u/Martin_Stolpe)
#### Post date: [December 6, 2019, 1:29pm UTC](https://discourse.cmake.org/t/usage-of-interface-link-libraries-with-namespaced-targets/330/1 "2019-12-06T13:29:07Z")

</div>

I try to use the Intel MKL, which has some pkg-config configuration files, with CMake. When I try to link the MKL statically in my project some linker options will be stored in the `INTERFACE_LINK_LIBRARIES` property. When I use this namespaced target in the` target_link_libraries` command the linker option will be set, which causes the compilation to fail.

Is there a way to tell CMake that the linker options should be used in my project when using the namespaced target?

The CMakeLists.txt currently looks like this:

```
cmake_minimum_required(VERSION 3.16)
project(intel-fft
    LANGUAGES C CXX
    )

set(source_files
    "main.c"
    )
set(executable_name test_fft)
 
# the `pkg_check_modules` function is created with this call
find_package(PkgConfig REQUIRED)

# these calls create special `PkgConfig::<MODULE>` variables
pkg_check_modules(INTEL_MKL
    REQUIRED
    IMPORTED_TARGET
    mkl-static-ilp64-iomp
    )
    
add_executable(${executable_name}
    ${source_files}
    )

set_target_properties(${executable_name}
    PROPERTIES
        C_STANDARD 11
        CXX_STANDARD 14
        CXX_STANDARD_REQUIRED ON
        CXX_EXTENSIONS OFF
    )

get_property(INTERFACE_LINK_OPTIONS
    TARGET PkgConfig::INTEL_MKL
    PROPERTY INTERFACE_LINK_OPTIONS
    )

target_link_libraries(${executable_name}
    PUBLIC
        PkgConfig::INTEL_MKL
        ${INTERFACE_LINK_OPTIONS} # This option is needed because it sets some linker options
        # ${INTEL_MKL_LDFLAGS_OTHER} <- This would also work
    )

```

These are the options which are stored in `INTERFACE_LINK_OPTIONS`:

```
-Wl,--start-group;/opt/intel/compilers_and_libraries_2019.5.281/linux/mkl/lib/intel64_lin/libmkl_intel_ilp64.a;/opt/intel/compilers_and_libraries_2019.5.281/linux/mkl/lib/intel64_lin/libmkl_intel_thread.a;/opt/intel/compilers_and_libraries_2019.5.281/linux/mkl/lib/intel64_lin/libmkl_core.a;-Wl,--end-group
```

---

<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: [December 6, 2019, 2:41pm UTC](https://discourse.cmake.org/t/usage-of-interface-link-libraries-with-namespaced-targets/330/2 "2019-12-06T14:41:49Z")

</div>

I don’t understand your problem.

Why do you extract the `INTERFACE_LINK_OPTIONS` property from target `PkgConfig::INTEL_MKL` to re-inject them in the command `target_link_directories`?

The command `target_link_libraries(${executable_name} PUBLIC PkgConfig::INTEL_MKL)` is enough to propagate interface link options from target `PkgConfig::INTEL_MKL` to your target.

---

<div class="post-metadata">

### Author: ![Martin\_Stolpe](https://discourse.cmake.org/user_avatar/discourse.cmake.org/martin_stolpe/32/188_2.png) [@Martin\_Stolpe](https://discourse.cmake.org/u/Martin_Stolpe)
#### Post date: [December 7, 2019, 3:47pm UTC](https://discourse.cmake.org/t/usage-of-interface-link-libraries-with-namespaced-targets/330/3 "2019-12-07T15:47:53Z")

</div>

Unfortunately it’s not enough to just add `PkgConfig::INTEL_MKL` when I use the static library. I have to explicitly add `${INTEL_MKL_LDFLAGS_OTHER}` (which is equal to the `INTERFACE_LINK_OPTIONS` property) to the `target_link_libraries` command, otherwise the required linker flags would not be set.

The question is if this behavior is expected or if this is perhaps a CMake bug?

---

<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: [December 7, 2019, 6:53pm UTC](https://discourse.cmake.org/t/usage-of-interface-link-libraries-with-namespaced-targets/330/4 "2019-12-07T18:53:23Z")

</div>

I cannot reproduce your problem. Using exactly same `CMakeLists.txt` I get the expected result: no need to manually specify the link options.

What is the exact version of the `cmake` tool ?

---

<div class="post-metadata">

### Author: ![erk](https://discourse.cmake.org/user_avatar/discourse.cmake.org/erk/32/114_2.png) [@erk](https://discourse.cmake.org/u/erk)
#### Post date: [December 7, 2019, 7:00pm UTC](https://discourse.cmake.org/t/usage-of-interface-link-libraries-with-namespaced-targets/330/5 "2019-12-07T19:00:10Z")

</div>

As stated by @marc.chevrier it should be enough to:  
`target_link_libraries(${executable_name} PUBLIC PkgConfig::INTEL_MKL)`  
to drag in the necesserary `INTERFACE_LINK_OPTIONS` from `PkgConfig::INTEL_MKL`.  
but you said:  
_…when I use the static library. I have to explicitly add…_  
does this mean that when you use shared lib it works?

The `INTERFACE_LINK_OPTIONS` you have shown only refer to static lib.

---

<div class="post-metadata">

### Author: ![Martin\_Stolpe](https://discourse.cmake.org/user_avatar/discourse.cmake.org/martin_stolpe/32/188_2.png) [@Martin\_Stolpe](https://discourse.cmake.org/u/Martin_Stolpe)
#### Post date: [December 9, 2019, 4:54pm UTC](https://discourse.cmake.org/t/usage-of-interface-link-libraries-with-namespaced-targets/330/6 "2019-12-09T16:54:15Z")

</div>

Yes it works for the shared library version because the linker flags will be set in the INTERFACE\_LINK\_LIBRARIES property only. The property INTERFACE\_LINK\_OPTIONS is empty when I use the shared library.

---

<div class="post-metadata">

### Author: ![erk](https://discourse.cmake.org/user_avatar/discourse.cmake.org/erk/32/114_2.png) [@erk](https://discourse.cmake.org/u/erk)
#### Post date: [December 9, 2019, 6:33pm UTC](https://discourse.cmake.org/t/usage-of-interface-link-libraries-with-namespaced-targets/330/7 "2019-12-09T18:33:47Z")

</div>

@marc.chevrier did reproduce your use case (see [Usage of INTERFACE\_LINK\_LIBRARIES with namespaced targets](https://discourse.cmake.org/t/usage-of-interface-link-libraries-with-namespaced-targets/330/4)) may be you could share a non-working stripped down example with us.
