[CMake] link_libraries vs target_link_libraries

Bill Hoffman bill.hoffman at kitware.com
Wed Nov 12 08:53:58 EST 2008


Fernando Cacciola wrote:

> Ha I see... that is 2.6 specific right?
> 
> There are still too many 2.4 versions shiped with Linux et al, and we 
> don't want to ask our users to *manually* upgrade cmake when they 
> already have one installed, so I'm keeping all compatible with at least 
> 2.4.5

Well, not much we can do about that but wait...  :)
\
> We are telling our users to do:
> 
>  find_packge( CGAL REQUIRED <components> )
> 
>  include( ${CGAL_USE_FILE} )
> 
>  ....
>  add_executable( program ... )
> 
>  target_link_libraries
>    ( program ${CGAL_3RD_PARTY_LIBRARIES} ${CGAL_LIBRARIES} )
> 
> But then I wondered: why am I bothering them with that last line while 
> everything else is hidden in UseCGAL?  After all if they do not won't to 
> link with that, which would be really odd, they better don't use UseCGAL 
> at all and rather just use the outcome of FindCGAL manually.
> 
> So IMO UseCGAL should be all or nothing. Wouldn't you agree?
For an executable is it not as important since there is no transitive 
linking.  However, link_libraries is a bit of a blunt instrument as it 
will link with all the executables and libraries after it is called into 
sub directories.  So, I still think linking just specific libraries is 
better than not.  Also, it will be one less thing you have to change 
when 2.6 comes out.  What if the project had program1 and program2, and 
program2 used VTK and CGAL, but program1 only used CGAL?  Then the 
link_libraries approach would link too much.  The extra includes should 
not hurt because VTK and CGAL should not have conflicting headers.  So, 
there is a still a benefit to specifically linking libraries.
> 
> 
> OTOH, it could make sense to do the following:
> 
>  find_packge( CGAL REQUIRED <components> )
> 
>  include( ${CGAL_USE_FILE} )
> 
>  ....
>  add_executable( program ... )
> 
>  use_CGAL( program )
> 
> so it works now with 2.4, and eventually "upgrade" it to use target 
> properties instead.

That sounds like a good way to go, and is similar to what the boost 
folks are doing.


-Bill


More information about the CMake mailing list