[CMake] statically linked executables

Oliver Dole odole at sand-labs.org
Mon Dec 3 08:22:59 EST 2007


2007/12/3, Bill Hoffman <bill.hoffman at kitware.com>:
> Oliver Dole wrote:
> > 2007/11/27, Daniel Rueckert <dr at doc.ic.ac.uk>:
> >> Hi,
> >>
> >> I am trying to build some statically linked executables on a linux
> >> box. I have added the "-static" option to the linker flags and specified
> >> static link libraries but for some reason cmake still adds the option
> >> "-Wl,-Bdynamic" flag to the linker options. Is there any way around this?
> >>
> >> Thanks, Daniel
> > Hello,
> >
> > I exactly face the same problem with cmake-2.4.6:
> > http://www.sand-labs.org/owb/ticket/160
> > But I assure that it perfectly worked with cmake-2.4.3. So is there a
> > regression between cmake-2.4.3 and cmake-2.4.6 or a new way to manage
> > static link that I have missed (and so my CMakeFiles are not valid
> > anymore)?
> >
>
> A change was made to make sure that CMake picks the libraries you
> specified.  So, if you have /usr/lib/libfoo.so, CMake will add
> -Wl,-dynamic -lfoo around foo.  But if you have /usr/lib/libfoo.a, then
> cmake will add -Wl,-static -lfoo around foo.  So, if CMake is given a
> full path to a shared library it will add -Wl,-dyanmic around it.
>
> -Bill
>
Bill, thanks for the info and for the quick reply.
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 :'(

-- 
Olivier DOLE


More information about the CMake mailing list