[CMake] Duplicate builds with target_sources
Robert Maynard
robert.maynard at kitware.com
Tue Nov 20 10:33:48 EST 2018
You have defined 4 && 5 as public sources so that means that consumers
of mylib_core and mylib_core will build them. If you want the sources
to be part of mylib_core the sources should be private, if you want
the sources to be part of mylib_thing use interface. In general don't
use PUBLIC with target_sources.
On Tue, Nov 20, 2018 at 9:19 AM Wesley Smith <wesley.hoke at gmail.com> wrote:
>
> 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
>
> --
>
> 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:
> https://cmake.org/mailman/listinfo/cmake
More information about the CMake
mailing list