[prev in list] [next in list] [prev in thread] [next in thread] 

List:       cmake
Subject:    Re: [CMake] OBJECT libraries and INTERFACE_SOURCES
From:       Chuck Atkins <chuck.atkins () kitware ! com>
Date:       2016-12-09 16:17:02
Message-ID: CAOXZzG4jzfKkJZtdSYXO1zNra_FxyJsk+sefe2gUyz8s1KxRqw () mail ! gmail ! com
[Download RAW message or body]

[Attachment #2 (multipart/alternative)]


Hi Giovanni,
Does it work if you add lib1 as a private dependency of lib2?  i.e.:
target_link_libraries(lib2 PRIVATE lib1)

Also, it shouldn't matter but what about in your original example if you
use the full path to the source file instead while still keeping the public
link dependency?  i.e.:
add_library(lib1-obj OBJECT $(CMAKE_CURRENT_SOURCE_DIR}/lib1.cpp).

----------
Chuck Atkins
Staff R&D Engineer, Scientific Computing
Kitware, Inc.


On Fri, Dec 9, 2016 at 5:26 AM, Giovanni Funchal <gio@cloudnc.co.uk> wrote:

> Hi,
>
> Apologies, the problem wasn't properly minimised because I had some
> trouble wrapping my head around it. Here's two complete examples which I
> hope can explain the problem better this time.
>
> 1) This works:
>
> [CMakeLists.txt]
>     cmake_minimum_required(VERSION 3.6)
>     project(Test)
>     add_library(lib1-obj OBJECT lib1.cpp)
>     add_library(lib1 INTERFACE)
>     set_target_properties(lib1 PROPERTIES INTERFACE_SOURCES
> $<TARGET_OBJECTS:lib1-obj>)
>     add_library(lib2 lib2.cpp)
>     target_link_libraries(lib2 lib1)
>     add_executable(main main.cpp)
>     target_link_libraries(main lib2)
>
> 2) But this does not:
>
> [CMakeLists.txt]
>     cmake_minimum_required(VERSION 3.6)
>     project(Test)
>     add_library(lib1-obj OBJECT lib1.cpp)
>     add_library(lib1 INTERFACE)
>     set_target_properties(lib1 PROPERTIES INTERFACE_SOURCES
> $<TARGET_OBJECTS:lib1-obj>)
>     add_library(lib2 lib2.cpp)
>     target_link_libraries(lib2 lib1)
>     add_subdirectory(main)
>
> [main/CMakeLists.txt]
>     add_executable(main main.cpp)
>     target_link_libraries(main lib2)
>
> With the second example, I get an error "cannot find source file
> lib1.cpp.o tried extensions...".
>
> Thanks,
> -- Giovanni
>
>
> On Thu, Dec 8, 2016 at 9:56 PM, Stephen Kelly <steveire@gmail.com> wrote:
>
>> Giovanni Funchal wrote:
>>
>> > Hi,
>> >
>> > The help page [1] mentions that:
>> >
>> >> Although object libraries may not be named directly in calls to the
>> >> target_link_libraries() command, they can be "linked" indirectly by
>> >> using an Interface Library whose INTERFACE_SOURCES target property
>> >> is set to name $<TARGET_OBJECTS:objlib>.
>> >
>> > However, I was unable to get this to work. Doing this:
>> >
>> > add_library(lib-obj OBJECT test.cpp)
>> > add_library(lib INTERFACE)
>> > add_dependencies(lib lib-obj)
>> > set_target_properties(lib PROPERTIES INTERFACE_SOURCES
>> > $<TARGET_OBJECTS:lib-obj>)
>> >
>> > And then trying to specify lib in link_libraries of an executable yields
>> > an error message "Cannot find source file: ... Tried extensions:... ".
>> >
>> > Am I doing something wrong?
>>
>> This works for me:
>>
>>     cmake_minimum_required(VERSION 3.3)
>>
>>     project(testit CXX)
>>
>>     add_library(lib-obj OBJECT foo.cpp)
>>     add_library(lib-iface INTERFACE)
>>     set_target_properties(lib-iface PROPERTIES
>>       INTERFACE_SOURCES $<TARGET_OBJECTS:lib-obj>
>>     )
>>     add_executable(main foo-user.cpp)
>>     target_link_libraries(main lib-iface)
>>
>>
>> Thanks,
>>
>> Steve.
>>
>>
>> --
>>
>> Powered by www.kitware.com
>>
>> Please keep messages on-topic and check the CMake FAQ at:
>> http://www.cmake.org/Wiki/CMake_FAQ
>>
>> Kitware offers various services to support the CMake community. For more
>> information on each offering, please visit:
>>
>> CMake Support: http://cmake.org/cmake/help/support.html
>> CMake Consulting: http://cmake.org/cmake/help/consulting.html
>> CMake Training Courses: http://cmake.org/cmake/help/training.html
>>
>> Visit other Kitware open-source projects at
>> http://www.kitware.com/opensource/opensource.html
>>
>> Follow this link to subscribe/unsubscribe:
>> http://public.kitware.com/mailman/listinfo/cmake
>
>
>
> --
>
> Powered by www.kitware.com
>
> Please keep messages on-topic and check the CMake FAQ at:
> http://www.cmake.org/Wiki/CMake_FAQ
>
> Kitware offers various services to support the CMake community. For more
> information on each offering, please visit:
>
> CMake Support: http://cmake.org/cmake/help/support.html
> CMake Consulting: http://cmake.org/cmake/help/consulting.html
> CMake Training Courses: http://cmake.org/cmake/help/training.html
>
> Visit other Kitware open-source projects at http://www.kitware.com/opensou
> rce/opensource.html
>
> Follow this link to subscribe/unsubscribe:
> http://public.kitware.com/mailman/listinfo/cmake
>

[Attachment #5 (text/html)]

<div dir="ltr"><div>Hi Giovanni,<br>Does it work if you add lib1 as a private \
dependency of lib2?   i.e.:<br><span \
style="font-family:monospace,monospace">target_link_libraries(lib2 PRIVATE \
lib1)</span><br><br>Also, it shouldn&#39;t matter but what about in your original \
example if you use the full path to the source file instead while still keeping the \
public link dependency?   i.e.:<br><span \
style="font-family:monospace,monospace">add_library(lib1-obj OBJECT \
$(CMAKE_CURRENT_SOURCE_DIR}/<wbr>lib1.cpp).</span><br></div><div \
class="gmail_extra"><br clear="all"><div><div \
class="m_-5799557117749818928gmail_signature" data-smartmail="gmail_signature"><div \
dir="ltr"><div><div dir="ltr"><div><div dir="ltr">----------<br>Chuck Atkins<br>Staff \
R&amp;D Engineer, Scientific Computing<br>Kitware, \
Inc.<br><br></div></div></div></div></div></div></div> <br><div \
class="gmail_quote">On Fri, Dec 9, 2016 at 5:26 AM, Giovanni Funchal <span \
dir="ltr">&lt;<a href="mailto:gio@cloudnc.co.uk" \
target="_blank">gio@cloudnc.co.uk</a>&gt;</span> wrote:<br><blockquote \
class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc \
solid;padding-left:1ex"><div dir="ltr">Hi,<div><br></div><div>Apologies, the problem \
wasn&#39;t properly minimised because I had some trouble wrapping my head around it. \
Here&#39;s two complete examples which I hope can explain the problem better this \
time.</div><div><br></div><div>1) This \
works:</div><div><br></div><div>[CMakeLists.txt]</div><div><div>      \
cmake_minimum_required(VERSION 3.6)</div><div>      project(Test)</div><div>      \
add_library(lib1-obj OBJECT lib1.cpp)<br></div><div>      add_library(lib1 \
INTERFACE)<br></div><div>      set_target_properties(lib1 PROPERTIES \
INTERFACE_SOURCES $&lt;TARGET_OBJECTS:lib1-obj&gt;)</div><div>      add_library(lib2 \
lib2.cpp)<br></div><div>      target_link_libraries(lib2 lib1)</div><div>      \
add_executable(main main.cpp)<br></div><div>      target_link_libraries(main \
lib2)</div></div><div><br></div><div>2) But this does \
not:</div><div><br></div><div><div>[CMakeLists.txt]</div><div></div><div>      \
cmake_minimum_required(VERSION 3.6)</div><div>      project(Test)</div><div>      \
add_library(lib1-obj OBJECT lib1.cpp)<br></div><div>      add_library(lib1 \
INTERFACE)<br></div><div>      set_target_properties(lib1 PROPERTIES \
INTERFACE_SOURCES $&lt;TARGET_OBJECTS:lib1-obj&gt;)</div><div>      add_library(lib2 \
lib2.cpp)<br></div><div>      target_link_libraries(lib2 lib1)</div><div>      \
add_subdirectory(main)<br></div></div><div><br></div><div>[main/CMakeLists.txt]</div><div><div> \
add_executable(main main.cpp)<br></div><div>      target_link_libraries(main \
lib2)</div></div><div><br></div><div>With the second example, I get an error \
&quot;cannot find source file lib1.cpp.o tried \
extensions...&quot;.</div><div><br></div><div>Thanks,</div><div>-- \
Giovanni</div><div><div class="m_-5799557117749818928h5"><div><br></div><div \
class="gmail_extra"><br><div class="gmail_quote">On Thu, Dec 8, 2016 at 9:56 PM, \
Stephen Kelly <span dir="ltr">&lt;<a href="mailto:steveire@gmail.com" \
target="_blank">steveire@gmail.com</a>&gt;</span> wrote:<br><blockquote \
class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid \
rgb(204,204,204);padding-left:1ex"><span \
class="m_-5799557117749818928m_3387639873968406080gmail-">Giovanni Funchal wrote:<br> \
<br> &gt; Hi,<br>
&gt;<br>
&gt; The help page [1] mentions that:<br>
&gt;<br>
&gt;&gt; Although object libraries may not be named directly in calls to the<br>
&gt;&gt; target_link_libraries() command, they can be "linked" indirectly by<br>
&gt;&gt; using an Interface Library whose INTERFACE_SOURCES target property<br>
&gt;&gt; is set to name $&lt;TARGET_OBJECTS:objlib&gt;.<br>
&gt;<br>
&gt; However, I was unable to get this to work. Doing this:<br>
&gt;<br>
&gt; add_library(lib-obj OBJECT test.cpp)<br>
&gt; add_library(lib INTERFACE)<br>
&gt; add_dependencies(lib lib-obj)<br>
&gt; set_target_properties(lib PROPERTIES INTERFACE_SOURCES<br>
&gt; $&lt;TARGET_OBJECTS:lib-obj&gt;)<br>
&gt;<br>
&gt; And then trying to specify lib in link_libraries of an executable yields<br>
&gt; an error message &quot;Cannot find source file: ... Tried extensions:... \
&quot;.<br> &gt;<br>
&gt; Am I doing something wrong?<br>
<br>
</span>This works for me:<br>
<br>
      cmake_minimum_required(VERSION 3.3)<br>
<br>
      project(testit CXX)<br>
<br>
      add_library(lib-obj OBJECT foo.cpp)<br>
      add_library(lib-iface INTERFACE)<br>
      set_target_properties(lib-ifac<wbr>e PROPERTIES<br>
<span class="m_-5799557117749818928m_3387639873968406080gmail-">         \
INTERFACE_SOURCES $&lt;TARGET_OBJECTS:lib-obj&gt;<br>  )<br>
</span>      add_executable(main foo-user.cpp)<br>
      target_link_libraries(main lib-iface)<br>
<br>
<br>
Thanks,<br>
<br>
Steve.<br>
<br>
<br>
--<br>
<br>
Powered by <a href="http://www.kitware.com" rel="noreferrer" \
target="_blank">www.kitware.com</a><br> <br>
Please keep messages on-topic and check the CMake FAQ at: <a \
href="http://www.cmake.org/Wiki/CMake_FAQ" rel="noreferrer" \
target="_blank">http://www.cmake.org/Wiki/CMak<wbr>e_FAQ</a><br> <br>
Kitware offers various services to support the CMake community. For more information \
on each offering, please visit:<br> <br>
CMake Support: <a href="http://cmake.org/cmake/help/support.html" rel="noreferrer" \
target="_blank">http://cmake.org/cmake/help/su<wbr>pport.html</a><br> CMake \
Consulting: <a href="http://cmake.org/cmake/help/consulting.html" rel="noreferrer" \
target="_blank">http://cmake.org/cmake/help/co<wbr>nsulting.html</a><br> CMake \
Training Courses: <a href="http://cmake.org/cmake/help/training.html" \
rel="noreferrer" target="_blank">http://cmake.org/cmake/help/tr<wbr>aining.html</a><br>
 <br>
Visit other Kitware open-source projects at <a \
href="http://www.kitware.com/opensource/opensource.html" rel="noreferrer" \
target="_blank">http://www.kitware.com/opensou<wbr>rce/opensource.html</a><br> <br>
Follow this link to subscribe/unsubscribe:<br>
<a href="http://public.kitware.com/mailman/listinfo/cmake" rel="noreferrer" \
target="_blank">http://public.kitware.com/mail<wbr>man/listinfo/cmake</a></blockquote></div><br></div></div></div></div>
 <br>--<br>
<br>
Powered by <a href="http://www.kitware.com" rel="noreferrer" \
target="_blank">www.kitware.com</a><br> <br>
Please keep messages on-topic and check the CMake FAQ at: <a \
href="http://www.cmake.org/Wiki/CMake_FAQ" rel="noreferrer" \
target="_blank">http://www.cmake.org/Wiki/CMak<wbr>e_FAQ</a><br> <br>
Kitware offers various services to support the CMake community. For more information \
on each offering, please visit:<br> <br>
CMake Support: <a href="http://cmake.org/cmake/help/support.html" rel="noreferrer" \
target="_blank">http://cmake.org/cmake/help/su<wbr>pport.html</a><br> CMake \
Consulting: <a href="http://cmake.org/cmake/help/consulting.html" rel="noreferrer" \
target="_blank">http://cmake.org/cmake/help/co<wbr>nsulting.html</a><br> CMake \
Training Courses: <a href="http://cmake.org/cmake/help/training.html" \
rel="noreferrer" target="_blank">http://cmake.org/cmake/help/tr<wbr>aining.html</a><br>
 <br>
Visit other Kitware open-source projects at <a \
href="http://www.kitware.com/opensource/opensource.html" rel="noreferrer" \
target="_blank">http://www.kitware.com/opensou<wbr>rce/opensource.html</a><br> <br>
Follow this link to subscribe/unsubscribe:<br>
<a href="http://public.kitware.com/mailman/listinfo/cmake" rel="noreferrer" \
target="_blank">http://public.kitware.com/mail<wbr>man/listinfo/cmake</a><br></blockquote></div><br></div></div>




-- 

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: \
http://www.cmake.org/Wiki/CMake_FAQ

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

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at \
http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/cmake



[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic