# Combining two third party libraries into a single imported target

**URL:** https://discourse.cmake.org/t/combining-two-third-party-libraries-into-a-single-imported-target/5695
**Category:** Usage
**Created:** [May 19, 2022, 8:49am UTC](https://discourse.cmake.org/t/combining-two-third-party-libraries-into-a-single-imported-target/5695 "2022-05-19T08:49:13Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![adaldev](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/9dc877/32.png) [@adaldev](https://discourse.cmake.org/u/adaldev)
#### Post date: [May 19, 2022, 8:49am UTC](https://discourse.cmake.org/t/combining-two-third-party-libraries-into-a-single-imported-target/5695/1 "2022-05-19T08:49:13Z")

</div>

Hi

My need as described in the topic subject is occurring with the GMP library (Gnu Multiple Precision).  
On my system it does not seem to be detectable by find\_package so I tried to make it detectable through a Findgmp.cmake.

The issue is that it is actually made fo two libraries gmp (C API) and gmpxx (C++ API, which depend on the first one).

I can link both using two different find modules, and generating two targets (gmp::gmp and gmpxx::gmpxx) but I’d like to have only one target (say gmp::gmp) that allows me to link both libraries.

So far I cannot write my findmodule accordingly (example attached).

Can someone tell me how to fix it?

By the way, I’m utterly confused with the difference between \_LIBRARY and \_LIBRARIES, \_INCLUDE\_DIR and \_INCLUDE\_DIRS. I’d appreciate some clarifications on this topic.

Regards  
A.  
[Findgmp.cmake](https://discourse.cmake.org/uploads/short-url/dM3qwRXy74Rmqy50nXyu3vAVeeq.cmake) (1.2 KB)

---

<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: [May 19, 2022, 11:25am UTC](https://discourse.cmake.org/t/combining-two-third-party-libraries-into-a-single-imported-target/5695/2 "2022-05-19T11:25:55Z")

</div>

> [@adaldev](#):
>
> By the way, I’m utterly confused with the difference between \_LIBRARY and \_LIBRARIES, \_INCLUDE\_DIR and \_INCLUDE\_DIRS. I’d appreciate some clarifications on this topic.

The singular ones tend to be `find_*` cache variables. Once the package is considered found, the plural ones are set as needed as local variables.

> [@adaldev](#):
>
> On my system it does not seem to be detectable by find\_package so I tried to make it detectable through a Findgmp.cmake.

Right, a find module would need to be written. You can write one that finds both targets. Something like:

```cmake
add_library(gmp::gmp UNKNOWN IMPORTED)
# skipping over location settings

# component conditional?
add_library(gmp::gmpxx UNKNOWN IMPORTED)
set_target_properties(gmp::gmpxx PROPERTIES LINK_INTERFACE_LIBRARIES gmp::gmp)
# skipping over location settings

add_library(gmp::gmpall INTERFACE)
set_target_properties(gmp::gmpxx PROPERTIES LINK_INTERFACE_LIBRARIES "gmp::gmp;gmp::gmpxx")

```

---

<div class="post-metadata">

### Author: ![adaldev](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/9dc877/32.png) [@adaldev](https://discourse.cmake.org/u/adaldev)
#### Post date: [May 20, 2022, 8:14am UTC](https://discourse.cmake.org/t/combining-two-third-party-libraries-into-a-single-imported-target/5695/3 "2022-05-20T08:14:55Z")

</div>

Thanks for your answer.

Here is my failed try  
[Findgmp.cmake](https://discourse.cmake.org/uploads/short-url/fSJuXwdO5vPAGiYUuyew5hAfIuq.cmake) (1.4 KB)

in my CMakeList:

```cmake
  IF(gmp_FOUND)
  	MESSAGE("gmp package found")
  	IF (NOT TARGET gmp::gmp)
  		MESSAGE(WARNING "gmp::gmp target not found")
  	ENDIF()
  ENDIF()

```

and the configuration output:

```auto
gmp package found
gmp::gmp target not found

```

What did I do wrong?

Thanks again  
A.

---

<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: [May 20, 2022, 12:59pm UTC](https://discourse.cmake.org/t/combining-two-third-party-libraries-into-a-single-imported-target/5695/4 "2022-05-20T12:59:32Z")

</div>

> `AND gmpxx_FOUND`

Nothing ever sets `gmpxx_FOUND`. Additionally, you should guard target creation with `if (NOT TARGET …)` because `find_package(gmp)` running twice within a scope is otherwise a hard error on the target re-creation.

---

<div class="post-metadata">

### Author: ![adaldev](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/9dc877/32.png) [@adaldev](https://discourse.cmake.org/u/adaldev)
#### Post date: [May 20, 2022, 2:17pm UTC](https://discourse.cmake.org/t/combining-two-third-party-libraries-into-a-single-imported-target/5695/5 "2022-05-20T14:17:29Z")

</div>

Thanks for having pinpointed a first error.

Here is the update

Now the configuration seems OK.

I find\_package(gmp) then target\_link\_libraries(MyTargetName PRIVATE gmp::gmp) but on link, only libgmpxx is passed to the linker whereas libgmp is also expected and needed.

where is the next mistake?  
[Findgmp.cmake](https://discourse.cmake.org/uploads/short-url/kXo1fNaDfQDyFeArhBvwL8FMp2J.cmake) (1.9 KB)

A.

---

<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: [May 21, 2022, 1:45pm UTC](https://discourse.cmake.org/t/combining-two-third-party-libraries-into-a-single-imported-target/5695/6 "2022-05-21T13:45:34Z")

</div>

You still need to wrap all of the post-FPHSA stuff in `if (gmp_FOUND)`. But the `gmp::gmp` target is completely wrong. It needs to be an `INTERFACE`, not an `UNKNOWN` library. You’re setting the same property twice which means that the first setting is completely ignored.

> [@ben.boeckel](#):
>
> ```auto
> add_library(gmp::gmpall INTERFACE)
> set_target_properties(gmp::gmpxx PROPERTIES LINK_INTERFACE_LIBRARIES "gmp::gmp;gmp::gmpxx")
> 
> ```

---

<div class="post-metadata">

### Author: ![adaldev](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/9dc877/32.png) [@adaldev](https://discourse.cmake.org/u/adaldev)
#### Post date: [May 24, 2022, 4:33pm UTC](https://discourse.cmake.org/t/combining-two-third-party-libraries-into-a-single-imported-target/5695/7 "2022-05-24T16:33:34Z")

</div>

Sorry but I’m still lost.

I fixed the INTERFACE issue but I have the same behaviour, nothing is linked, as if gmp::gmp would be empty.

[GMPTest.7z](https://discourse.cmake.org/uploads/short-url/g4cqiqiXEBLNLICIEUUac2WjMo2.7z) (1.4 KB)

Here is a minimal example to reproduce

on the other hand targets gmp::gmpc and gmp::gmpxx as, if I replace

```cmake
target_link_libraries(${TARGET_NAME} PRIVATE gmp::gmp)

```

by

```cmake
target_link_libraries(${TARGET_NAME} PRIVATE gmp::gmpc)
target_link_libraries(${TARGET_NAME} PRIVATE gmp::gmpxx)

```

it compiles and runs fine

another fix is to replace

````cmake
set_target_properties(gmp::gmp PROPERTIES LINK_INTERFACE_LIBRARIES "gmp::gmpc;gmp::gmpxx")```
by
```cmake
target_link_libraries(gmp::gmp INTERFACE gmp::gmpc gmp::gmpxx)

````

whereas both commands seem equivalent to me…  
then

```cmake
target_link_libraries(${TARGET_NAME} PRIVATE gmp::gmp)

```

works properly  
But anyway, whatever the commands used, variables gmp\_LIBRARY and gmp\_INCLUDE\_DIR with incorrect values are created. How to set them correctly?

thanks again for the help

---

<div class="post-metadata">

### Author: ![adaldev](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/9dc877/32.png) [@adaldev](https://discourse.cmake.org/u/adaldev)
#### Post date: [January 20, 2023, 3:06pm UTC](https://discourse.cmake.org/t/combining-two-third-party-libraries-into-a-single-imported-target/5695/8 "2023-01-20T15:06:52Z")

</div>

I have another issue with my Findgmp module.  
on some random machine, I seem to find a gmp install as \_INCLUDE\_DIR and \_LIBRARY paths are set

```auto
/usr/lib/x86_64-linux-gnu/libgmp.so
/usr/include/x86_64-linux-gnu
/usr/lib/x86_64-linux-gnu/libgmpxx.so
/usr/include

```

yet, when running CMake, I have the following error:  
CMake Error at …/Findgmp.cmake:50 (target\_link\_libraries):  
Cannot specify link libraries for target “gmp::gmp” which is not built by this project.

What does it mean? How can I fix it.  
Thanks  
[Findgmp.cmake](https://discourse.cmake.org/uploads/short-url/3E9G88h4W84jyLlRKnCegMdjJ3U.cmake) (1.6 KB)

---

<div class="post-metadata">

### Author: ![adaldev](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/9dc877/32.png) [@adaldev](https://discourse.cmake.org/u/adaldev)
#### Post date: [January 25, 2023, 2:04pm UTC](https://discourse.cmake.org/t/combining-two-third-party-libraries-into-a-single-imported-target/5695/9 "2023-01-25T14:04:14Z")

</div>

Issue on cmake 3.10.2, no issue on 3.19.2

mere curiosity, what has changed between both version that may explained the error.  
from what version should I consider that my Findgmp is OK?

I will just issue an error message if client cmake version is too old.

Thx,  
A.

---

<div class="post-metadata">

### Author: ![jtxa](https://discourse.cmake.org/user_avatar/discourse.cmake.org/jtxa/32/1535_2.png) [@jtxa](https://discourse.cmake.org/u/jtxa)
#### Post date: [January 26, 2023, 12:26am UTC](https://discourse.cmake.org/t/combining-two-third-party-libraries-into-a-single-imported-target/5695/10 "2023-01-26T00:26:10Z")

</div>

Probably [CMP0079](https://cmake.org/cmake/help/latest/policy/CMP0079.html) in v3.13:  
[`target_link_libraries()`](https://cmake.org/cmake/help/latest/command/target_link_libraries.html#command:target_link_libraries) allows use with targets in other directories.

If that’s not the one: You can play around with different `cmake_minimum_required` until the problem also occurs in your newer version.

---

<div class="post-metadata">

### Author: ![adaldev](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/9dc877/32.png) [@adaldev](https://discourse.cmake.org/u/adaldev)
#### Post date: [January 26, 2023, 3:49pm UTC](https://discourse.cmake.org/t/combining-two-third-party-libraries-into-a-single-imported-target/5695/11 "2023-01-26T15:49:12Z")

</div>

Thx, it sounds correct.  
Regards  
A.
