[CMake] Duplicate builds with target_sources

Wesley Smith wesley.hoke at gmail.com
Tue Nov 20 09:19:38 EST 2018


I have a library target defined as


add_library(mylib_core
    core_source1.cpp
    core_source2.cpp
    core_source3.cpp
)

if(UNIX)
    target_sources(mylib_core PUBLIC
        ${CMAKE_CURRENT_SOURCE_DIR}/core_source4.cpp
        ${CMAKE_CURRENT_SOURCE_DIR}/core_source5.cpp
    )
endif()


Then a target that uses it as a dependency:


add_library(mylib_thing
    thing_source1.cpp
    thing_source2.cpp
    thing_source3.cpp
)

target_link_libraries(mylib_thing
    PUBLIC
        mylib_core
)

When building with static libs, I noticed that the sources defined on
mylib_core with target_sources are building twice.


[ 60%] Building CXX object
mylib_thing/CMakeFiles/mylib_thing.dir/thing_source1.cpp.o
[ 60%] Building CXX object
mylib_thing/CMakeFiles/mylib_thing.dir/thing_source2.cpp.o
[ 60%] Building CXX object
mylib_thing/CMakeFiles/mylib_thing.dir/thing_source3.cpp.o
[ 60%] Building CXX object
mylib_thing/CMakeFiles/mylib_thing.dir/__/mylib_core/core_source4.cpp.o
[ 60%] Building CXX object
mylib_thing/CMakeFiles/mylib_thing.dir/__/mylib_core/core_source5.cpp.o

The last two source files were already built when mylib_core was built.
Why are they being built again under mylib_thing?  When these files were
defined in the add_library() call, this didn't happen.  Any thoughts?  Is
it related to the PUBLIC v. PRIVATE enum?  If so, what is the setting on
sources when defining them via add_library()?

thanks,
wes
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://cmake.org/pipermail/cmake/attachments/20181120/7dfd75bc/attachment.html>


More information about the CMake mailing list