4

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

gexicide
  • 38,535
  • 21
  • 92
  • 152
  • What CMake version are you using ? 3.16.3? Then Boost version 1.73.0 is not known to your CMake version and you should try to add `set( Boost_ADDITIONAL_VERSIONS 1.73.0 1.73)` before your `find_package(Boost ...)` call. Don't forget to delete the CMakeCache.txt file in the build directory before rerunning CMake. – vre Dec 18 '20 at 13:32
  • @vre: This didn't solve it. I tried adding this, deleting my build directory. Still same output. The CMake version that I'm using is 3.17.3. – gexicide Dec 18 '20 at 13:45
  • 2
    Add `set(Boost_DEBUG ON)` before `find_package(Boost)` and run configuration again. Its output will contain information actual Boost libraries which are searched and search locations. Then compare this info with files you have in your Boost installation. – Tsyvarev Dec 18 '20 at 14:01
  • 1
    FYI: If you add `REQUIRED` keyword to `find_package` call, then CMake will automatically terminate if Boost is not found. So you could omit the check for `Boost_FOUND` variable. – Tsyvarev Dec 18 '20 at 14:04
  • @Tsyvarev Boost_DEBUG helped: It seems that it tries different library names: Searching for PROGRAM_OPTIONS_LIBRARY_RELEASE: boost_program_options-clangw10-mt-x64-1_73;boost_program_options-clangw10-mt-x64;boost_program_options-clangw10-mt;boost_program_options-vc6-mt-x64-1_73;boost_program_options-vc6-mt-x64;boost_program_options-vc6-mt;boost_program_options-mt-x64-1_73;boost_program_options-mt-x64;boost_program_options-mt;boost_program_options-mt;boost_program_options The versions I have all have vc142 in them. How do I get library versions with vc6 or clangw10 in their names? – gexicide Dec 18 '20 at 14:18
  • As you use Clang for compile your project, you need Boost compiled in the same way. Not sure where you can get ready-made Boost which is built by clang on Windows, but you may build Boost by yourself. See e.g. [that question](https://stackoverflow.com/questions/33041534/building-boost-with-clang-3-8-on-windows) on this topic. – Tsyvarev Dec 18 '20 at 15:36
  • Did you build boost yourself? If this is the case, did you did and used `b2 ... install` you should be able to pass the directory provided as `--prefix` option as path hint to the config version of `find_package`. The `lib64-msvc-14.2` part in the path may be an indication that you did not do the install step or you use the build dir instead of the install dir. – fabian Dec 18 '20 at 22:24

1 Answers1

2

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! :)

kenba
  • 4,303
  • 1
  • 23
  • 40
  • Another possible pitfall: In my project, I had `Boost_USE_STATIC_LIBS=ON`, but my MSVC compiler triplet was set to `x86-windows` (without `-static`). cmake could find boost, but not the static libraries. – Hermann May 16 '21 at 10:35