[CMake] static vs. shared lib with same name
Brad King
brad.king at kitware.com
Wed Sep 13 16:55:00 EDT 2006
Darby J Van Uitert wrote:
> I am linking against a library that I have as a static and shared
> library with the same name (foo.a and foo.so). When I build my project
> with BUILD_SHARED_LIBS on, I want it to look for foo.so and when I build
> static libs, I want it to link against foo.a. But it seems to always
> want to build against the .so unless I specify it directly using the
> variable used in the FIND_LIBRARY command. Is there a flag I can set to
> tell it to look for static libraries first? Thanks.
There are two parts to this problem:
1.) Getting FIND_LIBRARY to choose libfoo.a over libfoo.so.
2.) Getting the linker to choose libfoo.a over libfoo.so.
When FIND_LIBRARY gives back a file name like /path/to/libfoo.so and it
is given to TARGET_LINK_LIBRARIES CMake generates a link line containing
-L/path/to -lfoo
Then it is up to the linker to choose between libfoo.a and libfoo.so.
For part 1 the FIND_LIBRARY command should probably have a STATIC_ONLY
and/or SHARED_ONLY option.
For part 2 CMake should probably instead map /path/to/libfoo.so to
-L/path/to -lfoo
and /path/to/libfoo.a to
-L/path/to -Wl,-Bstatic -lfoo -Wl,-Bdynamic
You can submit a feature request here:
http://www.cmake.org/Bug
Meanwhile you can add the -Wl,-Bstatic flags yourself in CMakeLists.txt
code. Something like this may work:
FIND_LIBRARY(MYLIB ...)
IF(UNIX)
IF(NOT BUILD_SHARED_LIBS)
SET(MYLIB -Wl,-Bstatic ${MYLIB} -Wl,-Bdynamic)
ENDIF(NOT BUILD_SHARED_LIBS)
ENDIF(UNIX)
TARGET_LINK_LIBRARIES(mytarget ${MYLIB})
Don't worry whether FIND_LIBRARY returns a shared or static lib because
the link flags will override the choice anyway.
-Brad
More information about the CMake
mailing list