Discussion:
[CMake] Duplicate builds with target_sources
Wesley Smith
2018-11-20 14:19:38 UTC
Permalink
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
Robert Maynard
2018-11-20 15:33:48 UTC
Permalink
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.
Post by Wesley Smith
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()
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
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
https://cmake.org/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:
https://cmake.org/mailman/listinfo/cmake
Wesley Smith
2018-11-20 17:12:53 UTC
Permalink
Thank you for the explanation Robert.
Post by Robert Maynard
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.
Post by Wesley Smith
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()
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.
Post by Wesley Smith
[ 60%] Building CXX object
mylib_thing/CMakeFiles/mylib_thing.dir/thing_source1.cpp.o
Post by Wesley Smith
[ 60%] Building CXX object
mylib_thing/CMakeFiles/mylib_thing.dir/thing_source2.cpp.o
Post by Wesley Smith
[ 60%] Building CXX object
mylib_thing/CMakeFiles/mylib_thing.dir/thing_source3.cpp.o
Post by Wesley Smith
[ 60%] Building CXX object
mylib_thing/CMakeFiles/mylib_thing.dir/__/mylib_core/core_source4.cpp.o
Post by Wesley Smith
[ 60%] Building CXX object
mylib_thing/CMakeFiles/mylib_thing.dir/__/mylib_core/core_source5.cpp.o
Post by Wesley Smith
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()?
Post by Wesley Smith
thanks,
wes
--
Powered by www.kitware.com
http://www.cmake.org/Wiki/CMake_FAQ
Post by Wesley Smith
Kitware offers various services to support the CMake community. For more
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
Post by Wesley Smith
https://cmake.org/mailman/listinfo/cmake
Loading...