I cannot get CMake (3.16.4) and boost to work together like they should.
My system has boost installed (v1.72), and I compiled another version like so:
wget https://dl.bintray.com/boostorg/release/1.71.0/source/boost_1_71_0.tar.gz
tar -xf boost_1_71_0.tar.gz
cd boost_1_71_0 && ./bootstrap.sh && ./b2 --prefix=../external/boost_1_71_0 --with-timer install
I have a simple test program using a boost timer:
#include <iostream>
int main()
{
boost::timer::auto_cpu_timer t;
std::cout << "Hello Timer" << std::endl;
return 0;
}
And that's the CMakeLists.txt:
cmake_minimum_required(VERSION 3.16.4)
Project(CmakeBoostText)
set(Boost_DEBUG 1)
set(Boost_NO_SYSTEM_PATHS ON)
set(BOOST_ROOT ../external/boost_1_71_0)
find_package(Boost 1.71 REQUIRED COMPONENTS timer)
add_executable(main main.cpp)
target_link_libraries(main Boost::timer)
Then I try to compile my test binary:
rm -rf build/* && cmake -S . -B build && cmake --build build
In the output, there you can clearly see that he is using the system variant of boost:
-- [ /usr/share/cmake-3.16/Modules/FindBoost.cmake:1468 ] Boost_NO_SYSTEM_PATHS = "ON"
-- [ /usr/share/cmake-3.16/Modules/FindBoost.cmake:1537 ] BOOST_ROOT = "../external/boost_1_71_0"
[...]
Found Boost: /usr/lib64/cmake/Boost-1.72.0/BoostConfig.cmake (found suitable version "1.72.0", minimum required is "1.71") found components: timer
Using ldd I can also verify the binary is linked to the system library of boost.
This is quite odd. According to the documentation in cmake/find_package, Boost_NO_SYSTEM_PATHS should handle this.
If I then change the find_package to include 'EXACT' it suddenly works (it finds and uses the correct version. But I consider this to be a big hack.
find_package(Boost 1.71 EXACT REQUIRED COMPONENTS timer)
Why is that not working without EXACT? Clarifications welcome.
Note: I don't want to use anything involving include_directories(). So these workarounds are out of scope.