[CMake] static library linking

Brad King brad.king at kitware.com
Sun Jun 15 19:36:39 EDT 2008


Steven Van Ingelgem wrote:
> I don't seem to be able to make it link correctly?
> 
> I do:
> set_property(TARGET ${SUB_PROJECT} PROPERTY IMPORTED_LOCATION ${PTHREAD} 
> ${RT} ${LIBC})
> 
> But it's still somehow linking wrongly :(...
> 
> What would be the correct line for this?

You've mis-read the example.  The target property should be set on a 
special IMPORTED target.  An IMPORTED target is a logical CMake target 
that can be linked but is not built.  Instead it points at an external 
file.  By using an imported target you can give CMake alot more information.

Try this:

   # add imported target
   add_library(imp_pthread STATIC IMPORTED)
   # point the imported target at the real file
   set_property(TARGET imp_pthread PROPERTY
                IMPORTED_LOCATION /usr/lib/libpthread.a)

   # import some more libs
   add_library(imp_rt STATIC IMPORTED)
   set_property(TARGET imp_rt PROPERTY
                IMPORTED_LOCATION /usr/lib/librt.a)
   add_library(imp_c STATIC IMPORTED)
   set_property(TARGET imp_c PROPERTY
                IMPORTED_LOCATION /usr/lib/libc.a)

   # Now link to the imported targets
   target_link_libraries(myexe imp_pthread imp_rt imp_c)

This will produce your desired link line.  More info on imported targets 
is here:

   http://www.cmake.org/Wiki/CMake_2.6_Notes#Importing_Targets

-Brad


More information about the CMake mailing list