# How to use find\_library correctly?

**URL:** <https://discourse.cmake.org/t/how-to-use-find-library-correctly/280>\
**Category:** Code\
**Created:** [November 28, 2019, 12:24pm UTC](https://discourse.cmake.org/t/how-to-use-find-library-correctly/280 "2019-11-28T12:24:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![matwey](https://discourse.cmake.org/user_avatar/discourse.cmake.org/matwey/32/198_2.png) [@matwey](https://discourse.cmake.org/u/matwey)\
**Post date:** [November 28, 2019, 12:24pm UTC](https://discourse.cmake.org/t/how-to-use-find-library-correctly/280/1 "2019-11-28T12:24:55Z")

</div>

Hello,

I am trying to reuse existing [FindCheck.cmake](https://github.com/libcheck/check/blob/master/doc/example/cmake/FindCheck.cmake) module in my project. However, it appears that Ubuntu, for instance, provides only static libcheck.a library in the system package.  
`FindCheck.cmake` uses `find_library` to locate the library, the library path is successfully found:

`-- Found CHECK: /usr/lib/x86_64-linux-gnu/libcheck.a`

but finally I get linker error due to missed `-pthread` (I don’t use pthread in my application, but `libcheck` does):

```
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/libcheck.a(check_pack.o): undefined reference to symbol '__pthread_unregister_cancel@@GLIBC_2.3.3'
//lib/x86_64-linux-gnu/libpthread.so.0: error adding symbols: DSO missing from command line 

```

So what should be a proper way for generic `FingXXX.cmake` to locate and specify library linker dependencies? As far as I understand there is no `pthread` dependency on Windows platform, for instance.

---

<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:** [November 28, 2019, 1:51pm UTC](https://discourse.cmake.org/t/how-to-use-find-library-correctly/280/2 "2019-11-28T13:51:05Z")

</div>

FindCheck should define an imported target not a bare list of libraries.  
That imported target should define [https://cmake.org/cmake/help/v3.15/prop\_tgt/INTERFACE\_LINK\_LIBRARIES.html#prop\_tgt:INTERFACE\_LINK\_LIBRARIES](https://cmake.org/cmake/help/v3.15/prop_tgt/INTERFACE_LINK_LIBRARIES.html#prop_tgt:INTERFACE_LINK_LIBRARIES)  
or  
[https://cmake.org/cmake/help/v3.15/prop\_tgt/INTERFACE\_COMPILE\_OPTIONS.html#prop\_tgt:INTERFACE\_COMPILE\_OPTIONS](https://cmake.org/cmake/help/v3.15/prop_tgt/INTERFACE_COMPILE_OPTIONS.html#prop_tgt:INTERFACE_COMPILE_OPTIONS)  
in order to drag in proper `-pthread` compiler and link option when some target link to “Check”.

The find module may define those deps conditionnally depending on the platform used.

Note that `pkg_check_module` can create such an imported target for you, see doc of FindPkgConfig

so FindCheck.cmake#L23 can use it as well.

---

<div class="post-metadata">

**Author:** ![matwey](https://discourse.cmake.org/user_avatar/discourse.cmake.org/matwey/32/198_2.png) [@matwey](https://discourse.cmake.org/u/matwey)\
**Post date:** [November 28, 2019, 2:09pm UTC](https://discourse.cmake.org/t/how-to-use-find-library-correctly/280/3 "2019-11-28T14:09:29Z")

</div>

Could you please advice me a good reference `FindXXX.cmake`? I’ll try to follow the imported target idea.

---

<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:** [November 28, 2019, 2:51pm UTC](https://discourse.cmake.org/t/how-to-use-find-library-correctly/280/4 "2019-11-28T14:51:20Z")

</div>

I bet that most of the FindXXX modules that may be found in CMake source do now use IMPORTED target:

Simples ones may be easier to read:

> <https://github.com/Kitware/CMake/blob/master/Modules/FindZLIB.cmake>

  
More complex may have more platform specific tests and some use INTERFACE\_LINK\_LIBRARIES  

> <https://github.com/Kitware/CMake/blob/master/Modules/FindPNG.cmake>
