title: Linking CMake interface libraries with object libraries
body: <p>I am trying to use interface libraries to define preprocessor macros. These interface libraries will then be linked to other libraries to propagate these macros. This approach has worked with shared libraries I am creating but does not work with CMake object libraries.</p>
  
  <p>I understand that you cannot directly link an interface library to an object library but you can indirectly link the <code>TARGET_OBJECTS</code> to the interface library.</p>
  
  <p><a href="https://cmake.org/cmake/help/v3.9/manual/cmake-buildsystem.7.html#object-libraries" rel="noreferrer">Docs</a>:</p>
  
  <blockquote>
    <p>Although object libraries may not be named directly in calls to the <code>target_link_libraries()</code> command, they can be “linked” indirectly by using an Interface Library whose <code>INTERFACE_SOURCES</code> target property is set to name <code>$&lt;TARGET_OBJECTS:objlib&gt;</code>.</p>
  </blockquote>
  
  <p>I have tried to do this but the object files are still not compiled with the appropriate definitions. Here is the minimal working example:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>// a.cpp    
  int a() {
    return
  #ifdef MY_DEF
    5;
  #endif
      }
  </code></pre>
  
  <p>CMakeLists:</p>
  
  <pre class="lang-cmake prettyprint-override"><code>cmake_minimum_required(VERSION 3.0.1)
  project(my_question)
  
  add_library(object_lib OBJECT a.cpp)
  
  add_library(interface_lib INTERFACE)
  target_compile_definitions(interface_lib INTERFACE MY_DEF)
  
  # This does not set the MY_DEF flag
  target_sources(interface_lib INTERFACE $&lt;TARGET_OBJECTS:object_lib&gt;)
  
  add_library(main_lib SHARED $&lt;TARGET_OBJECTS:object_lib&gt;)
  target_link_libraries(main_lib)
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>/Library/Developer/CommandLineTools/usr/bin/make -f CMakeFiles/object_lib.dir/build.make CMakeFiles/object_lib.dir/build
  [ 50%] Building CXX object CMakeFiles/object_lib.dir/a.cpp.o
  /Library/Developer/CommandLineTools/usr/bin/c++     -o CMakeFiles/object_lib.dir/a.cpp.o -c /Users/umar/devel/so_question/a.cpp
  /Users/umar/devel/so_question/a.cpp:7:5: error: expected expression
      }
      ^
  1 error generated.
  make[2]: *** [CMakeFiles/object_lib.dir/a.cpp.o] Error 1
  make[1]: *** [CMakeFiles/object_lib.dir/all] Error 2
  make: *** [all] Error 2
  </code></pre>
  
  <p>Based on the documentation this should be possible in CMake. I am not sure if I am doing something wrong or if this is an issue in CMake. I have been able to reproduce this in CMake version 3.6 and 3.8 on Debian and OSX.</p>
  
  <p>Edit:</p>
  
  <p>I have been able to get around this using @utopia's approach but I was curious why the approach I used in my example did not work. I did not know if I was doing this incorrectly or if this was a problem with the tool. Perhaps StackOverflow is not the correct platform for this type of question and I should file a bug report against the project.</p>
  
  <p>Edit2:</p>
  
  <p>As of the most current version of CMake(3.8) this not possible in CMake(<a href="https://gitlab.kitware.com/cmake/cmake/issues/16929" rel="noreferrer">see discussion</a>). This is something that may be supported in 3.9 via <a href="https://gitlab.kitware.com/cmake/cmake/merge_requests/712" rel="noreferrer">this</a> merge request. For older versions utopia's answer is the way to go.</p>