[CMake] How to link libraries correctly?

Enrico Scholz enrico.scholz at informatik.tu-chemnitz.de
Fri Sep 22 03:10:15 EDT 2006


Hello,

consider the following CMakeLists.txt

----
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/bar.c "void foo() { }\n")
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/foo.c "void bar() { }\n")

add_library(foo SHARED ${CMAKE_CURRENT_BINARY_DIR}/foo.c)
target_link_libraries(foo -lz -lcups -lcrypt)

add_library(bar SHARED ${CMAKE_CURRENT_BINARY_DIR}/bar.c)
target_link_libraries(bar foo)
----

This:
* creates a library 'libfoo.so' and a library 'libbar.so'
* 'libfoo.so' depends on lot of other (dynamic) system-libraries
* 'libbar.so' depends only on 'libfoo.so'



'cmake' seems to mislink the example above; after make, 'libbar.so'
depends on the all the stuff which is brought in by 'libfoo' already:


| $ make VERBOSE=1
| /usr/lib/ccache/gcc  -fPIC    -shared -Wl,-soname,libbar.so -o libbar.so "CMakeFiles/bar.dir/bar.o" -L/var/tmp/cm -lfoo -lz -lcups -lcrypt -Wl,-rpath,/var/tmp/cm 
| 
| $ ldd libbar.so 
|         linux-gate.so.1 =>  (0xffffe000)
|         libfoo.so => /var/tmp/cm/libfoo.so (0xb7fec000)
|         libz.so.1 => /usr/lib/libz.so.1 (0xb7fb5000)
|         libcups.so.2 => /usr/lib/libcups.so.2 (0xb7f7f000)
|         libcrypt.so.1 => /lib/libcrypt.so.1 (0xb7f51000)
|         libc.so.6 => /lib/libc.so.6 (0xb7e1d000)
|         libgnutls.so.12 => /usr/lib/libgnutls.so.12 (0xb7da2000)
|         libpthread.so.0 => /lib/libpthread.so.0 (0xb7d8e000)
|         libm.so.6 => /lib/libm.so.6 (0xb7d69000)
|         /lib/ld-linux.so.2 (0x80000000)
|         libgcrypt.so.11 => /usr/lib/libgcrypt.so.11 (0xb7d14000)
|         libgpg-error.so.0 => /usr/lib/libgpg-error.so.0 (0xb7d10000)
|         libnsl.so.1 => /lib/libnsl.so.1 (0xb7cf9000)


Specifying the '-lz -lcups -lcrypt' for libbar.so is needed only, when these
libraries are static ones. For dynamic libraries in bloats up the symbol
resolving only. Accordingly http://people.redhat.com/drepper/dsohowto.pdf
only '-lfoo' should be used when linking 'libbar.so'.

Does there exist a switch, option or statement to enforce correct
linking? Result should be that 'ldd libbar.so' gives out only

|         linux-gate.so.1 =>  (0xffffe000)
|         libfoo.so => /var/tmp/cm/libfoo.so (0xb7fec000)

I am aware of '-Wl,--as-needed' but it might fail when system libraries
are having incorrect dependencies.




Enrico
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 480 bytes
Desc: not available
Url : http://public.kitware.com/pipermail/cmake/attachments/20060922/b148bd25/attachment.pgp


More information about the CMake mailing list