[CMake] statically linked executables

Hendrik Sattler post at hendrik-sattler.de
Mon Dec 3 09:11:17 EST 2007


Quoting Oliver Dole <odole at sand-labs.org>:
> The problem is that I use pkg-config (and stuff like that) to retrieve
> my libraries. So CMake does not know wether it is static or shared
> libraries to use. Nonetheless I have found a workaround thanks to this
> thread: http://www.cmake.org/pipermail/cmake/2006-September/011096.html
> So I have just added the following in my CMakeFiles:
>     IF (NOT BUILD_SHARED_LIBS)
>         SET (LIBRARIES_TO_LINK ${LIBRARIES_TO_LINK} -Wl,-Bstatic
> ${LIBXSLT_LIBS})
>     ELSE (NOT BUILD_SHARED_LIBS)
>         SET (LIBRARIES_TO_LINK ${LIBRARIES_TO_LINK} ${LIBXSLT_LIBS})
>     ENDIF (NOT BUILD_SHARED_LIBS)
> Unfortunately I still have a problem, but which is not due to cmake.
> My application indirectly needs to link with -lgcc_s and I do not have
> the static library for that, so I get an error :'(

Why do you try to work around that? Instead, do it properly by using  
find_library:
pkg_check_modules ( PKGCONFIG_MYPREFIX something )
foreach ( i ${PKGCONFIG_MYPREFIX_STATIC_LIBRARIES} )
   find_library ( ${i}_LIBRARY
                  NAMES ${i}
                  PATHS ${PKGCONFIG_MYPREFIX_LIBRARY_DIRS}
                )
   list ( APPEND MYPREFIX_LIBRARIES ${i}_LIBRARY );
endforeach ( i)
Actually, FindPkgConfig.cmake should be changed to give full paths to  
libraries.
Also note, that you MUST use the pkgconfig variables for static  
linking, not the ones for dynamic linking (see module help of  
FindPkgConfig)

I am sure that somehow this can be made to find only the static libs,  
if not then that should be fixed.
OTOH, you may want to reconsider if you really want to give your users  
so much pain :-/ RAM is cheap but not available for free.

Additionally, building a static library does NOT mean that you have to  
link everything statically. You'll regret such a decision sooner or  
later.

HS




More information about the CMake mailing list