1

I want to use the Boost library on Windows but doing so has been troublesome. I downloaded the Windows package from here and extracted it to C:\Boost:

I added the following to my CMake file:

find_package(Boost 1.68 REQUIRED COMPONENTS filesystem)
# ...
target_link_libraries(MyExecutable ${Boost_LIBRARIES})

I'm getting the following CMake error:

C:\Users\User\AppData\Local\JetBrains\Toolbox\apps\CLion\ch-0\183.4284.104\bin\cmake\win\bin\cmake.exe -DCMAKE_BUILD_TYPE=Debug "-DCMAKE_MAKE_PROGRAM=C:/Program Files/mingw-w64/x86_64-8.1.0-win32-seh-rt_v6-rev0/mingw64/bin/mingw32-make.exe" "-DCMAKE_C_COMPILER=C:/Program Files/mingw-w64/x86_64-8.1.0-win32-seh-rt_v6-rev0/mingw64/bin/x86_64-w64-mingw32-gcc.exe" "-DCMAKE_CXX_COMPILER=C:/Program Files/mingw-w64/x86_64-8.1.0-win32-seh-rt_v6-rev0/mingw64/bin/x86_64-w64-mingw32-g++.exe" -G "CodeBlocks - MinGW Makefiles" D:\Cpp\MyProject
CMake Error at C:/Users/User/AppData/Local/JetBrains/Toolbox/apps/CLion/ch-0/183.4284.104/bin/cmake/win/share/cmake-3.12/Modules/FindBoost.cmake:2048 (message):
  Unable to find the requested Boost libraries.

  Boost version: 1.68.0

  Boost include path: C:/Boost

  Could not find the following Boost libraries:

          boost_filesystem

  Some (but not all) of the required Boost libraries were found.  You may
  need to install these additional Boost libraries.  Alternatively, set
  BOOST_LIBRARYDIR to the directory containing Boost libraries or BOOST_ROOT
  to the location of Boost.
Call Stack (most recent call first):
  CMakeLists.txt:6 (find_package)


-- Configuring incomplete, errors occurred!
See also "D:/Cpp/MyProject/cmake-build-debug/CMakeFiles/CMakeOutput.log".

[Failed to reload]

It apparently cannot find filesystem but it's there in C:\Boost\boost\filesystem (here is the documentation on FindBoost). enter image description here

How do I setup my CMake file to use Boost properly? I tried setting the Boost environment variables as well but it still didn't work:

SET (BOOST_ROOT "c:/Boost")
SET (BOOST_INCLUDEDIR "c:/Boost/boost")
SET (BOOST_LIBRARYDIR "c:/Boost/libs")

FIND_PACKAGE(Boost 1.68.0 REQUIRED COMPONENTS filesystem)
BullyWiiPlaza
  • 17,329
  • 10
  • 113
  • 185
  • Looks like you're using the wrong cmake, perhaps you want to check your `PATH` priority and push the JetBrains thing to the bottom. – Havenard Nov 18 '18 at 18:31
  • Did you compile the boost library with mingw? Your download link is the source code not mingw binaries. Also remember that you can not use Visual Studio binaries. Make sure you if you do download binaries that they were compiled for your toochain. – drescherjm Nov 18 '18 at 18:37
  • Since I'm using `CLion` by `Jetbrains` it's the correct `CMake` version. Also, `Boost` says that most things don't have to be compiled so I didn't do that. – BullyWiiPlaza Nov 18 '18 at 18:39

3 Answers3

2

Also, Boost says that most things don't have to be compiled so I didn't do that.

It cannot find the library boost::filesystem. Because boost::filesystem is one of the few libraries that need to be compiled (all the ones that you have to specify in the find package command have to be compiled).

You need to build boost first:

./booststrap.sh

And then:

bjam

It picks up whatever compiler is available, so you may have to set the proper toolset manually.

Matthieu Brucher
  • 21,634
  • 7
  • 38
  • 62
0

Not completely related, but I think your link line is incorrect, instead of:

target_link_libraries(MyExecutable Boost::filesystem)

it should say:

target_link_libraries(MyExecutable ${Boost_LIBRARIES})

Boost_LIBRARIES is automatically defined once Boost is found, so it's for free.

  • You're not wrong but this doesn't solve the problem so it should've been a comment instead. I guess you were lacking the reputation to do so... – BullyWiiPlaza Nov 24 '18 at 21:34
  • Indeed, my comment not just didn't resolve the issue, it was incorrect. Actually using `Boost::filesystem` is better than `${Boost_LIBRARIES}`, since the former only include what is required, while the latter includes ALL the libraries requested via `find_package()` (although in this case, they both match). My apologies. – Alvaro Palma Aste Jul 19 '19 at 14:42
0

The CMake FindBoost macro only fills Boost_Libraries if the find_package contains a COMPONENTS section and the macro actually finds those libraries on disk.

So here I'd be saying I need Boost to be found AND it must have filesystem

find_package(Boost 1.66.0 REQUIRED COMPONENTS filesystem)

The macro then tries to construct a bunch of potential filenames for the filesystem (taking into account if I want shared version, debug, multi-threaded etc.) and searches for those names under Boost_ROOT. If it doesn't find the file it may error and won't fill out Boost_Libraries.

If in doubt, add a line like this to your CMake prior to find_packages() to see what libs the macro is looking for and compare to what you actually have:

set (Boost_DEBUG ON)

For me I discovered that I had a Boost that placed an -x32 and -x64 architecture version of the libs, e.g. "libboost_system-mgw92-mt-x64-1_66.a". The macro wasn't filling in the architecture prefix into the filename it was looking for and failed.

I had to add a line like this to give the macro a clue which version I wanted:

set (Boost_ARCHITECTURE "-x64")

After that ${Boost_Libraries} expanded correctly and I was able to correctly use it to link.

locka
  • 5,809
  • 3
  • 33
  • 38