[CMake] Unable to link to static libraries on MacOS X

Thomas Christian Chust chust at web.de
Sun Feb 25 10:39:28 EST 2007


Hello,

part of the issues I had with creating and linking to libraries on MacOS
X have been resolved with help from this list. But the most important
problem remains.

I'm using CMake 2.4.6 on a PowerPC iBook running MacOS X 10.4.8 and I
can't get CMake to link against a static library if a dynamic library of
the same name exists somewhere on the search path.

I have attached a very simple example project to this e-mail that can be
used to demonstrate the problem. If you have a look at the
CMakeLists.txt in the example, you can see that it states that both a
shared and a static library should be built from the same sources plus
an executable should be built and linked against the static version of
the library.

Now if you create a build directory for the project using CMake and run
make in it, both libraries and the executable are built -- but if you
check the resulting executable with otool -L, you will see that
it is dynamically linked against libbug.dylib instead of statically
against libbug.a.

As CMake tries to be clever and converts any absolute path to a library
passed to TARGET_LINK_LIBRARIES into a combination of -L and -l flags
for the linker, it is completely impossible to link against a static
library on MacOS X if a dynamic library of the same name exists anywhere
on the library search path! The MacOS X linker always prefers dynamic
libraries over static ones if it can find them anywhere. Even when
adding -Wl,-search_paths_first to the link flags, the problem persists
if both the dynamic and static library reside in the same directory,
which is often the case for libraries installed by libtool based build
processes, for example.

Is there any portable workaround for this problem? Of course I can
always manually handle the linker flags correctly, but that's tedious
and exactly the type of thing I want to avoid by using CMake.

cu,
Thomas Chust

-------------- next part --------------
A non-text attachment was scrubbed...
Name: cmakebug.tar.gz
Type: application/x-tar
Size: 772 bytes
Desc: not available
Url : http://public.kitware.com/pipermail/cmake/attachments/20070225/70044e65/cmakebug.tar.tar


More information about the CMake mailing list