[CMake] Static linking and find_library

Micha Renner Micha.Renner at t-online.de
Fri May 8 11:47:22 EDT 2009


Am Freitag, den 08.05.2009, 07:29 -0600 schrieb Bill Hoffman:
> Marcel Loose wrote:
> > Ping!
> > 
> > I sent this mail more than a week ago, but got no answers. 
This happens sometimes.
> > 
> > ------------------------------------------------------------------------
> > 
> > Hi all,
> > 
> > I've been searching the documentation, but couldn't find a way to
> > specify that I want to create a statically linked binary. The only
> > platform-specific and even compiler-specific answer I could find was to
> > add "-static" to CMAKE_EXE_LINKER_FLAGS.
I could be completly wrong, but you want link a static library to a
program. You don't need CMAKE_EXE_LINKER_FLAGS.

PROJECT(T)
FIND_LIBRARY(_pathAndNameOfLibrary NAMES foo)
ADD_EXECUTABLE(targetname yourSourcefiles)
TARGET_LINK_LIBRARIES(targetname ${_pathAndNameOfLibrary})
> > 
> > Now this solves only part of the problem. Suppose I want to link against
> > the static library of some 3rd party package Foo. Suppose that Foo only
> > supplies a libfoo.so (but I'm unaware of this fact).
In this case the program is linked to foo.so.  
> > 
> > When I search for the foo-library using find_library(foo_library foo) I
> > find a match, so CMake happily trods on. But when I type 'make', I'll
> > discover to my dismay that the system cannot create a  statically linked
> > executable, because it cannot find libfoo.a. Bummer!
You have to make sure that FIND_LIBRARY finds the library (paths, names)
See www.cmake.org/cmake/help/cmake2.6docs.html#command:find_library for
a detailed description 

You might come in trouble if both versions (.so .a) exist. If they are
located in different directories, this might help:
www.cmake.org/Wiki/CMake_FAQ#Why_does_find_library_look_in_system_directories_before_its_PATHS_option.3F

> > 
> > So my question is twofold:
> > 
> > 1) Is there a generic platform- and compiler-independent way to specify
> > that you want to create a statically linked executable?
> > 
> No.
Is this really necessary? Live is already difficult enough.
> > 2) How can I persuade find_library() to only search for static
> > libraries?
> > 
> You can't.
> 
> -Bill
> 



More information about the CMake mailing list