<div dir="ltr">Dear all,<div><br></div><div>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:</div><div><a href="https://cmake.org/cmake/help/v3.5/manual/cmake-buildsystem.7.html#object-libraries">https://cmake.org/cmake/help/v3.5/manual/cmake-buildsystem.7.html#object-libraries</a><br></div><div><br></div><div>This sounded like a perfect idea, so I tried it out on Windows 7 with the VS 2013 64bit generator. </div><div>The first step was to build a shared library out of an object library, which worked like a charm:</div><div><br></div><div>====</div><div>## Setup OBJECT library and a 'object link interface'<br></div><div>add_library(MyLib.Object OBJECT</div><div>  ... sources ..</div><div>)</div><div>target_include_directories(MyLib.Object </div><div>  PUBLIC</div><div>    .... includes</div><div>)</div><div><br></div><div>add_library(MyLib.Object.LinkInterface INTERFACE)</div><div>target_sources(MyLib.Object.LinkInterface INTERFACE $<TARGET_OBJECTS:MyLib.Object>)</div><div>target_include_directories(MyLib.Object.LinkInterface</div><div>  INTERFACE $<TARGET_PROPERTY:MyLib.Object,INTERFACE_INCLUDE_DIRECTORIES></div><div>)</div><div>target_link_libraries(MyLib.Object.LinkInterface</div><div>  INTERFACE</div><div>    .... some dependent libraries</div><div>)</div><div><br></div><div>## set up shared library:</div><div>add_library(MyLib SHARED </div><div>  .. some additional sources</div><div>)</div><div><br></div><div>target_link_libraries(MyLib</div><div>  PUBLIC</div><div>    MyLib.Object.LinkInterface</div><div>)</div><div>====</div><div><br></div><div>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:</div><div>====</div><div>add_executable(MyExe</div><div>  ... sources ...</div><div>)</div><div><br></div><div>target_link_libraries(MyExe</div><div>  MyLib</div><div>)</div><div>====</div><div><br></div><div>This results in the following error from CMake:</div><div><div>CMake Error at MyExe/CMakeLists.txt:16 (add_executable):</div><div>  Cannot find source file:</div><div><br></div><div>    <MyLibPath>/InstrumentControl.Object.dir/$(Configuration)/InstrumentControl.obj</div><div><br></div><div>  Tried extensions .c .C .c++ .cc .cpp .cxx .m .M .mm .h .hh .h++ .hm .hpp</div><div>  .hxx .in .txx</div><div><br></div><div>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.</div></div><div><br></div><div>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</div><div>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.</div><div><br></div><div>So, the questions I have:</div><div>- should the described procedure work?</div><div>- if yes, did I something wrong, or did I stumble across a CMake bug?</div><div>- 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</div><div><br></div><div><br></div><div>Sincerely,</div><div>Jakob</div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div></div>