What is the best way to add a dependency on Boost when using the CMake FetContent module.
In researching this issue I found this answer which seems to work fine for compiled boost libraries like thread or filesystem but does not work for header only libraries like asio.
For reference here is the CMakeLists.txt file that I've tested:
cmake_minimum_required(VERSION 3.24)
project(p)
set(BOOST_INCLUDE_LIBRARIES thread filesystem system)
set(BOOST_ENABLE_CMAKE ON)
include(FetchContent)
FetchContent_Declare(
Boost
GIT_REPOSITORY https://github.com/boostorg/boost.git
GIT_TAG boost-1.74.0
)
FetchContent_MakeAvailable(Boost)
add_executable(boost_test boost_test.cpp)
target_link_libraries(boost_test PRIVATE Boost::boost Boost::filesystem)
And the corresponding boost_test.cpp file
#include <iostream>
#include <boost/asio.hpp>
int main(int argc, char * argv[])
{
std::cout << "hello boost" << std::endl;
}
When I attempt to compile this project in Windows I get the following:
C:\builds\junk\boost_test\build>cmake --build .
MSBuild version 17.4.0+18d5aef85 for .NET Framework
boost_atomic.vcxproj -> C:\builds\junk\boost_test\build\_deps\boost-build\libs\atomic\Debug\libboost_atomic-vc143-mt-
gd-x64-1_80.lib
boost_chrono.vcxproj -> C:\builds\junk\boost_test\build\_deps\boost-build\libs\chrono\Debug\libboost_chrono-vc143-mt-
gd-x64-1_80.lib
boost_container.vcxproj -> C:\builds\junk\boost_test\build\_deps\boost-build\libs\container\Debug\libboost_container-
vc143-mt-gd-x64-1_80.lib
boost_exception.vcxproj -> C:\builds\junk\boost_test\build\_deps\boost-build\libs\exception\Debug\libboost_exception-
vc143-mt-gd-x64-1_80.lib
boost_date_time.vcxproj -> C:\builds\junk\boost_test\build\_deps\boost-build\libs\date_time\Debug\libboost_date_time-
vc143-mt-gd-x64-1_80.lib
boost_filesystem.vcxproj -> C:\builds\junk\boost_test\build\_deps\boost-build\libs\filesystem\Debug\libboost_filesyst
em-vc143-mt-gd-x64-1_80.lib
boost_program_options.vcxproj -> C:\builds\junk\boost_test\build\_deps\boost-build\libs\program_options\Debug\libboos
t_program_options-vc143-mt-gd-x64-1_80.lib
boost_test.cpp
C:\builds\junk\boost_test\boost_test.cpp(2,10): fatal error C1083: Cannot open include file: 'boost/asio.hpp': No such
file or directory [C:\builds\junk\boost_test\build\boost_test.vcxproj]
boost_thread.vcxproj -> C:\builds\junk\boost_test\build\_deps\boost-build\libs\thread\Debug\libboost_thread-vc143-mt-
gd-x64-1_80.lib