[CMake] FindPkgConfig: Using *_LIBRARY_DIRS

Hendrik Sattler post at hendrik-sattler.de
Fri Oct 23 08:41:22 EDT 2009


Zitat von Murray Cumming <murrayc at murrayc.com>:

> pkg_search_module() defines SOMEPREFIX_LIBRARY_DIRS among other things,
> as mentioned here
> http://public.kitware.com/cgi-bin/viewcvs.cgi/Modules/FindPkgConfig.cmake?root=CMake&view=markup
> (better than the documentation at
> http://www.itk.org/Wiki/CMake:How_To_Find_Libraries
> )
>
> But how can I use those directories so my executable can actually be
> linked against the SOMEPREFIX_LIBRARIES libraries that I've specified to
> target_link_libraries().

I usually use a foreach() with the list of FOO_LIBRARIES to search  
with find_library(), using FOO_LIBRARY_DIRS as HINTS. Then you have  
one bar_LIBRARY for each library 'bar' which contains the full path.  
These variables can then be easily used for target_link_libraries():
------------------snip(untested)----------------
pkg_check_modules(FOO_PC foo);
if(FOO_PC_FOUND)
   foreach(foolib ${FOO_PC_LIBRARIES})
     find_package(${foolib}_LIBRARY
       NAMES ${foolib}
       HINTS ${FOO_PC_LIBRARY_DIRS}
     )
     if (${${foolib}_LIBRARY})
       list(APPEND FOO_LIBRARIES ${${foolib}_LIBRARY})
     endif (${${foolib}_LIBRARY})
   endforeach(foolib)
   include_directories(${FOO_PC_INCLUDE_DIRS})
endif(FOO_PC_FOUND)

add_libary(bar ${BAR_SOURCES})
target_link_libraries(bar baz ${FOO_LIBRARIES})
#add other properties for CFLAGS and LDFLAGS
------------------snip----------------

It is still nice to also handle the case that neither pkg-config nor  
the .pc file are present (or cannot be found).
If you need that often, just write a small wrapper macro.

> Actually, I would much rather just tell the build system to use
> SOMEPREFIX_LDFLAGS instead of dealing with the directories and libraries
> separately. That's what I do with autoconf/automake.

autotools only support GNU-like toolchains. Using the CFLAGS and  
LDFLAGS from .pc files is not always possible, while defines, include  
directories and library names (including their locations) may be  
usuable.

> Also, if I use several libraries, do I need separate pkg_search_module()
> calls, and therefore several sets of variables? I'd much rather just
> give a list of libraries and get one set of CFLAGS and LDFLAGS. Again,
> that's what I do with autoconf/automake.

Read the documentation you already pointed at yourself and use  
pkg_check_modules().

Have fun

HS




More information about the CMake mailing list