[CMake] OBJECT libraries and INTERFACE_SOURCES

Stephen Kelly steveire at gmail.com
Thu Dec 8 16:56:12 EST 2016


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.




More information about the CMake mailing list