[CMake] Out-of-project link library problem

Alan W. Irwin irwin at beluga.phys.uvic.ca
Wed Sep 3 21:50:59 EDT 2008


On 2008-09-03 20:16-0400 Andrew Sayman wrote:

> On Wed, Sep 3, 2008 at 7:57 PM, Christopher Harvey
> <chris at basementcode.com> wrote:
>> try getting rid of quotes
>
> Getting rid of the quotes doesn't change anything.
>
>> putting them all in a single command.
>
> This isn't helpful without removing all of the benefits of the CMake
> standard modules. If I wanted to hard code every library requirement I
> would. I'd rather not have to maintain a bunch of separate lists for
> each build machine and build OS however. A better example of what I'm
> doing, rather than just what triggers this bug, might be the
> following:
>
> target_link_libraries( my_project m crypt pthread ) # Needed either way
> if( USER_OPTION_X11 )
>   include( FindX11 )
>   target_link_libraries( my_project ${X11_LIBRARIES} )
> endif( USER_OPTION_X11 )
> if( USER_OPTION_GTK )
>   include( FindPkgConfig )
>   pkg_check_modules( GTK REQUIRED gtk+-2.0 )
>   target_link_libraries( my_project ${GTK_LIBRARIES} )
> endif( USER_OPTION_GTK )
>
> In this case my project needs m, crypt, and pthread no matter what.
> Both X11_LIBRARIES and GTK_LIBRARIES also have m and pthread as part
> of their required libs. This creates a redundancy and the libraries
> for all three are listed multiple times with the gtk libraries being
> repeated the most.

What happens if you use a single target_link_libraries command with an
overall list of libraries?  That is, you use the following transformation of
what you have above:

set(LIBRARY_LIST m crypt pthread)
if( USER_OPTION_X11 )
    include( FindX11 )
    list(APPEND LIBRARY_LIST ${X11_LIBRARIES})
endif( USER_OPTION_X11 )
if( USER_OPTION_GTK )
   include( FindPkgConfig )
   pkg_check_modules( GTK REQUIRED gtk+-2.0 )
   list(APPEND LIBRARY_LIST ${GTK_LIBRARIES} )
endif( USER_OPTION_GTK )
target_link_libraries( my_project ${LIBRARY_LIST} )

I don't guarantee the above syntax is correct, but you should get the idea.

This is the general method we use for PLplot (for most of the libraries you
mention and more). The method works like a charm and gives us complete
freedom to optionally append libraries to the list when needed for our
various platforms.

Alan
__________________________
Alan W. Irwin

Astronomical research affiliation with Department of Physics and Astronomy,
University of Victoria (astrowww.phys.uvic.ca).

Programming affiliations with the FreeEOS equation-of-state implementation
for stellar interiors (freeeos.sf.net); PLplot scientific plotting software
package (plplot.org); the libLASi project (unifont.org/lasi); the Loads of
Linux Links project (loll.sf.net); and the Linux Brochure Project
(lbproject.sf.net).
__________________________

Linux-powered Science
__________________________


More information about the CMake mailing list