gexicide
gexicide

Reputation: 40118

CMake cannot find Boost on Windows

I have a very simple CMake file which works fine on linux, but on windows, it says that it cannot find boost (even though it seems to find it, as it says "found suitable version"). Here is the initial build file:

cmake_minimum_required(VERSION 3.16.3)
project(filecompare)

set(CMAKE_CXX_STANDARD 20)

find_package(Boost 1.73.0 COMPONENTS program_options)

if(Boost_FOUND)
    include_directories(${Boost_INCLUDE_DIRS})
    add_executable(filecompare filecompare.cpp)
    target_link_libraries(filecompare ${Boost_LIBRARIES})
endif()

CMake output:

-- Could NOT find Boost (missing: Boost_INCLUDE_DIR program_options) (Required is at least version "1.73.0")
-- Configuring done
-- Generating done
-- Build files have been written to: C:/repos/filecompare/cmake-build-debug

So it seems it cannot find the library on its own, so I added these lines above find_package:

SET(CMAKE_INCLUDE_PATH ${CMAKE_INCLUDE_PATH} "C:/local/boost_1_73_0")
SET(CMAKE_LIBRARY_PATH ${CMAKE_LIBRARY_PATH} "C:/local/boost_1_73_0/lib64-msvc-14.2")

Now it somehow seems to find the library, but doesn't, here is the output:

-- Could NOT find Boost (missing: program_options) (found suitable version "1.73.0", minimum required is "1.73.0")
-- Configuring done
-- Generating done
-- Build files have been written to: C:/repos/filecompare/cmake-build-debug

I tried setting stuff like:

set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
SET(BOOST_ROOT "C:/local/boost_1_73_0/boost")

but that doesn't help either. So what am I doing wrong here? I'm using the clang-cl on CLion with VisualStudio 2019

Upvotes: 4

Views: 9936

Answers (1)

kenba
kenba

Reputation: 4549

To my mind you have two different issues:

  1. CMake does not know where to find boost.
  2. You have not built the boost library binaries for clang.

The first issue is easy to solve and you nearly had got it right. For CMake to find boost you need to set: BOOST_ROOT and BOOST_LIBRARYDIR, CMake will generate CMAKE_INCLUDE_PATH and CMAKE_LIBRARY_PATH from these variables. I usually set them as environment variables; in your case they should be:

BOOST_ROOT C:/local/boost_1_73_0
BOOST_LIBRARYDIR C:/local/boost_1_73_0/lib64-msvc-14.2

assuming that lib64-msvc-14.2 is where you built the boost libraries with clang?!
Note: the library binaries (including dlls if built as shared) should be in a sub-directory of lib64-msvc-14.2 named lib.

The second issue is that you need to build the boost libraries, specifically the program_options library, for clang.

I've only tried to build boost with clang once, and then I could only build 32 bit binaries, see Building Boost with Clang “Failed to build Boost.build engine”. However, that was 18 months ago and things may have improved since then.

Be aware that I often use boost libraries built with mingw (gcc) on Windows and have issues with CMake not finding boost see: cmake FindBoost not finding Boost libraries when building with MinGW on Windows I wouldn't be surprised if you have similar issues with clang. Many C++ libraries and tools (including CMake) assume that if you're on Windows then you're using Visual Studio...

I recommend building with Visual Studio first, since you already have the Visual Studio 19 boost library binaries. I also recommend setting CMAKE_CXX_STANDARD to 17, because you're pushing everything to the limit at the moment! :)

Upvotes: 3

Related Questions