How to use find_library correctly?

Hello,

I am trying to reuse existing 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.

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
or
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.

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

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:


More complex may have more platform specific tests and some use INTERFACE_LINK_LIBRARIES

2 Likes