[CMake] Linking hierarchy

svoboda at fi.muni.cz svoboda at fi.muni.cz
Thu May 11 04:15:35 EDT 2006


On Wed, 10 May 2006, Brad King wrote:

> svoboda at fi.muni.cz wrote:
>> I'm using CMake in the following way:
>
> What version of CMake are you using?

2.0.5

>
>> - I create a simple shared library
>> - The library requires many libraries during the built process
>
> Please be more specific.  Are the other libraries shared or static?

shared

>
>> - In the subdirectory (using the SUBDIRS command) I create examples,
>>   which are dependent on this shared library.
>> 
>> What is the problem:
>> 
>> - When building the examples in the subdirectory, all the required 
>> libraries are linked to these examples. I know, that these examples 
>> require only my shared library. Since CMake inherits linking dependencies 
>> I cannot avoid linking all the libraries, which were necessary when 
>> builing my library, to these simple examples.
>
> The dependency chaining is necessary for static libraries on all platforms 
> and even shared libraries on some platforms.

It is shared library. A simple example of CMakeLists.txt is attached to 
this mail. If you create a simple Makefile, you'll see the chaining of all 
the libraries despite 'example' target should be dependent only on 
'mylib'.

-David

> My guess is that you are building static "convenience" libraries and linking 
> them together into a single shared library.  This is not really supported by 
> CMake as it does not really make sense on all platforms (a set of .lib 
> archives on windows cannot be used to create a .dll).  It also does not work 
> on most UNIX environments unless the objects in the static archives are built 
> with the -fPIC or equivalent flag.
>
> What is your use case here?  If the shared library provides everything why 
> can't you just build all the sources in one target?
>
> -Brad
>
-------------- next part --------------
# project name
PROJECT (MY_PROJECT)

# build shared/static library
OPTION (BUILD_SHARED_LIBS "Static/Shared library" ON)

SET(MYLIBNAME mylib)
SET(SOURCES main.cc)

FIND_LIBRARY(TIFF tiff /usr/lib /usr/local/lib)
FIND_LIBRARY(JPEG jpeg /usr/lib /usr/local/lib)
	
ADD_LIBRARY(${MYLIBNAME} ${SOURCES})
TARGET_LINK_LIBRARIES(${MYLIBNAME} ${TIFF} ${JPEG})

ADD_EXECUTABLE(example example.cc)
TARGET_LINK_LIBRARIES(example ${MYLIBNAME})


More information about the CMake mailing list