[CMake] Adding ext. libs only to one target and not transitively to others

Klaus Drechsler drechsler at gmx.net
Fri Nov 28 18:13:26 EST 2008


Dear all,

I am using the toolkit 'MITK' which includes a sample application which 
can be extended by functionalities (something like plugins that get 
compiles into the 'mainapplication').

To directory layout is similar to that:

mitk
  CMakeLists.txt
  corelibrary
    CMakeLists.txt
  mainapplication
    CMakeLists.txt
  functionalities
    CMakeLists.txt
    func1
      CMakeLists.txt
      <sources>
    func2
      CMakeLists.txt
      <sources>
    ...

Our internal policies state that we are allowed to add functionalites 
(add funcX in the functionalities directory), but not allowed to touch 
the other directories and files.

An original CMakeLists.txt of a functionality just includes one line 
like this:

CREATE_QFUNCTIONALITY(func1)

This macro call creates  a target  callesd 'func1' and adds the sources 
and so on.

Now I would like to add an external library 'myextlib' only to this 
functionality.

I tried it like this:

-----------
set(MYEXTLIB_BIN_DIR "" CACHE PATH "The root build directory of myextlib")
load_cache(${MYEXTLIB_BIN_DIR} READ_WITH_PREFIX X MYEXTLIB_SOURCE_DIR)
include_directories (${XMYEXTLIB_SOURCE_DIR}/myextlib/include)
link_directories (${MYEXTLIB_BIN_DIR}/myextlib)

add_library(myextlib STATIC IMPORTED)
set_property(TARGET myextlib PROPERTY IMPORTED_LOCATION "myextlib.lib")

CREATE_QFUNCTIONALITY(func1)

target_link_libraries(func1 myextlib)
-----------

The result is almost as I want it, with the exception that cmake also 
adds 'myextlib.lib' to 'mainapplication', which gives linker errors 
(btw: i am using visual studio), because the library cannot be found. 
The link and include directories are only added to 'func1'.

How can I prevent cmake from adding 'myextlib' to any other targets? I 
only want it to be added to 'func1' nowhere else.

Or, if this is not possible, how can I propagate the link directory and 
include directory 'up' to the target 'mainapplication'?

Any help on that is highly appreciated.

Regards

Klaus


More information about the CMake mailing list