[CMake] OBJECT libraries and INTERFACE_SOURCES

Giovanni Funchal gio at cloudnc.co.uk
Mon Dec 12 04:44:04 EST 2016


Hello,

Can someone confirm please if this is a bug and whether I should file a bug
report?

Also, the "PRIVATE" workaround doesn't suit me (because it breaks
transitivity of includes). Is there any other workaround I can use? I
assume reverting to static libraries is the best thing to do in the
meanwhile?

Thanks,
-- Giovanni


On Fri, Dec 9, 2016 at 4:25 PM, Giovanni Funchal <gio at cloudnc.co.uk> wrote:

> Hello,
>
> PRIVATE works, but I don't understand why (the only difference between the
> two examples is using a subdirectory).
>
> Using ${CMAKE_CURRENT_SOURCE_DIR} does not work.
>
> Thanks,
> -- Giovanni
>
>
> On Fri, Dec 9, 2016 at 4:17 PM, Chuck Atkins <chuck.atkins at kitware.com>
> wrote:
>
>> 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 at 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 at 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/opensource/opensource.html
>>>
>>> Follow this link to subscribe/unsubscribe:
>>> http://public.kitware.com/mailman/listinfo/cmake
>>>
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/cmake/attachments/20161212/d98d9405/attachment.html>


More information about the CMake mailing list