Not being able to create a 'link' interface for an OBJECT library

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

Not being able to create a 'link' interface for an OBJECT library

Jakob van Bethlehem
Dear all,

Recently I read the excellent tip of creating a 'link interface' for an OBJECT library in order to overcome the inability to directly link an OBJECT library, see:

This sounded like a perfect idea, so I tried it out on Windows 7 with the VS 2013 64bit generator. 
The first step was to build a shared library out of an object library, which worked like a charm:

## Setup OBJECT library and a 'object link interface'
add_library(MyLib.Object OBJECT
  ... sources ..
    .... includes

add_library(MyLib.Object.LinkInterface INTERFACE)
target_sources(MyLib.Object.LinkInterface INTERFACE $<TARGET_OBJECTS:MyLib.Object>)
    .... some dependent libraries

## set up shared library:
add_library(MyLib SHARED 
  .. some additional sources


The second step was to try to create an executable that links with the shared library. That is were I ran into some problems I can't quite fathom:
  ... sources ...


This results in the following error from CMake:
CMake Error at MyExe/CMakeLists.txt:16 (add_executable):
  Cannot find source file:


  Tried extensions .c .C .c++ .cc .cpp .cxx .m .M .mm .h .hh .h++ .hm .hpp
  .hxx .in .txx

It seems that for some reason CMake is trying to reuse the same target sources on the executable, which doesn't make any sense really, as I'm trying to link against a normal shared library.

The solution is to remove the 'target_sources' and put the $<TARGET_OBJECTS:..> directly as a source when creating the shared library. That is quite unfortunate however. The entire
purpose of the 'object link interface' was to remove the need to put these generator expressions where there are needed, as explained on the documentation page I referred to above.

So, the questions I have:
- should the described procedure work?
- if yes, did I something wrong, or did I stumble across a CMake bug?
- if no, it makes sense to put in a very clear warning on the referred webpage, that the 'trick' will *not* work if one ever tries to link against the final library...... ajjjj



Powered by

Please keep messages on-topic and check the CMake FAQ at:

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support:
CMake Consulting:
CMake Training Courses:

Visit other Kitware open-source projects at

Follow this link to subscribe/unsubscribe: