Cmake Boost::boost target PRIVATE link, forcing dependency on client
I am having a problem with make 3.12 (windows) and cmake version 3.13.0-rc2 (linux). I am attempting to link boost headers privately to a STATIC
library, which is then linked by a client executable, that I do not want to depend on boost.
According to the docs, the find_package(Boost)
provides a target for the header only portion of the boost library, Boost::boost
. This is the target I am using, and I am linking the target with PRIVATE, ie
target_link_libraries(testlib PRIVATE Boost::boost)
However a the client testexe
which uses testlib
from the install dir with find_package(testlib)
and links it with target_link_libraries(testexe testlib::testlib)
is causing cmake to search for a Boost dependency for the testexe
. As boost is linked privately (and only headers) to testlib
, it shouldn't as far as I can see.
Interestingly adding an INTERFACE IMPORTED
target in testlib
and using this to link, and the client no longer needs boost.
find_package(Boost REQUIRED)
add_library(boost INTERFACE IMPORTED)
set_property(TARGET boost PROPERTY
INTERFACE_INCLUDE_DIRECTORIES ${Boost_INCLUDE_DIR})
target_link_libraries(testlib
PRIVATE
#using the imported target instead
boost
#Boost::boost)
this is almost identical to the lines in the FindBoost
source here. The only difference being the target the properties are set on.
I have a workaround but I would like to know what is going on.
The complete source for the two test projects are available here.
This is the output from cmake with using the Boost::boost
target...
#using to the provided Boost::boost target
C:\Temp\cmake_boost\testlib\build>cmake ../ -DBOOST_ROOT=d:\sdk\boost\boost_1_68_0
-- Boost version: 1.68.0-- Configuring done
-- Generating done
-- Build files have been written to: C:/Temp/cmake_boost/testlib/build
C:\Temp\cmake_boost\testexe\build>cmake ../ -DCMAKE_PREFIX_PATH="C:\Temp\cmake_boost\testlib\build"-- Configuring done
CMake Error at CMakeLists.txt:6 (add_executable):
Target "test" links to target "Boost::boost" but the target was not found.
Perhaps a find_package() call is missing for an IMPORTED target, or an
ALIAS target is missing?............-- Generating done
-- Build files have been written to: C:/Temp/cmake_boost/testexe/build
... and the INTERFACE IMPORTED
target, which works
# using the 'INTERFACE IMPORTED library' target.
C:\Temp\cmake_boost\testlib\build>cmake ../ -DBOOST_ROOT=d:\sdk\boost\boost_1_68_0
-- Boost version: 1.68.0-- Configuring done
-- Generating done
-- Build files have been written to: C:/Temp/cmake_boost/testlib/build
C:\Temp\cmake_boost\testexe\build>cmake ../ -DCMAKE_PREFIX_PATH="C:\Temp\cmake_boost\testlib\build"-- Configuring done
-- Generating done
-- Build files have been written to: C:/Temp/cmake_boost/testexe/build
Thanks!