19

I'm using a windows system. I want to use the Boost library using CMake. I've installed boost on C:\boost_1_55_0\ Here is my CMakeLists.txt file

set(Boost_USE_STATIC_LIBS        ON)
set(Boost_USE_MULTITHREADED      ON)
set(Boost_USE_STATIC_RUNTIME    OFF)
find_package(Boost 1.55.0 COMPONENTS thread)

if(Boost_FOUND)
    include_directories(${Boost_INCLUDE_DIRS}) 
    LINK_DIRECTORIES(${Boost_LIBRARY_DIRS})
    add_executable (linking_with_boost main.cc sqr.cc)
    target_link_libraries(linking_with_boost ${Boost_LIBRARIES})
else()
    message(STATUS "Fail  asdasd!")
endif()

I'm getting --Could NOT find Boost Output:

$ cmake ../
-- Could NOT find Boost
-- Fail  asdasd!
-- Configuring done
-- Generating done
-- Build files have been written to: D:/ubuntu_share/programming/C++/practice/cm
ake/linking_with_boost/build_win
3
  • 2
    Try setting BOOST_ROOT=C:\boost_1_55_0 before running cmake again. Also look at cmake --help-module FindBoost for more help. Commented Jan 7, 2014 at 11:20
  • Well that helped. Thanks.please put as answer. To be precise it should be" set(BOOST_ROOT C:/boost_1_55_0) Commented Jan 8, 2014 at 3:33
  • 2
    You don't have to put that set(... into your CMakeLists.txt! Just set an environment variable before calling cmake. Commented Jan 8, 2014 at 8:11

6 Answers 6

23

On Windows 7 x64 I have Boost 1.58 installed to C:\SDKs\boost_1_58_0. In order to allow cMake to find all of the appropriate files, I had to add the following three system variables:

    BOOST_INCLUDEDIR    C:\SDKs\boost_1_58_0\
    BOOST_LIBRARYDIR    C:\SDKs\boost_1_58_0\lib64-msvc-12.0
    BOOST_ROOT          C:\SDKs\boost_1_58_0\boost
1
  • the only solution that works
    – user10838321
    Commented Sep 29, 2021 at 7:26
11

In addition to the BOOST_ROOT I also had to set the BOOST_LIBRARYDIR variable to succeed. In my case this was c:\Program Files\boost_1_56_0\lib64-msvc-12.0

1
  • That's because the BOOST_LIBRARYDIR is defined as ` BOOST_LIBRARYDIR - Preferred library directory e.g. <prefix>/lib` and ` BOOST_ROOT (or BOOSTROOT) - Preferred installation prefix ` That is, your Boost installation's library dir is prefix/lib64-msvc-12.0 not prefix/lib as the FindPackage will try to find
    – Alex Bitek
    Commented Feb 14, 2017 at 7:45
10

You need to set the environment variable BOOST_ROOT to c:\boost_1_55_0 before running cmake. Also look at cmake --help-module FindBoost for more help.

8

I spent many hours on this issue and finally resolved it by using a few variables outlined in FindBoost manual here https://cmake.org/cmake/help/v3.0/module/FindBoost.html

Following variables helped me:

set (Boost_DETAILED_FAILURE_MSG ON)
set (Boost_THREADAPI win32)
set (BOOST_ROOT "/boost_1_40_0")
set (Boost_LIBRARY_DIR  "/boost_1_40_0/lib")
set (Boost_COMPILER "-vc")
set (Boost_USE_STATIC_RUNTIME ON)  
set (Boost_DEBUG ON)  #<---------- Real life saver
3
  • 1
    only answer that helped me so far
    – IceFire
    Commented Jun 5, 2018 at 9:23
  • I had to be far more specific than on other platforms because the b2 --layout=versioned is default for Windows, versus b2 --layout=system for others. And that parameter is underdocumented: stackoverflow.com/questions/8940249/…. It doesn't really help you to switch it from default either. Especially, I had to include the compiler name as above.
    – thadk
    Commented Jun 4, 2020 at 0:01
  • Boost_DEBUG rocks!
    – Alex Che
    Commented Apr 9 at 12:28
0

Then building, dont forget to escape symbols for win paths. I used this one same as prev answer example, it worked. Build(helpful docs on boost.org), then use CMakeLists.txt

    cmake_minimum_required(VERSION 3.4.0) 
    project(name_me_correctly_pls VERSION 1.0)
    set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}/build)
    set(SOURCE_EXE main.cpp) 
    include_directories(${Boost_INCLUDE_DIRS})
    add_executable(some_exe ${SOURCE_EXE})  # Создает исполняемый файл
    set (Boost_THREADAPI win32)
    set (BOOST_ROOT "C:\\local\\boost_1_69_0\\boost")
    set (Boost_LIBRARY_DIR  "C:\\local\\boost_1_69_0\\lib64-msvc-14.1")
    set (Boost_COMPILER "-vc")
    set (Boost_USE_STATIC_RUNTIME ON)  
    set (Boost_DEBUG ON)
    find_package(Boost REQUIRED)
    include_directories(${Boost_INCLUDE_DIR})
    target_link_libraries(some_exe ${Boost_SYSTEM_LIBRARY})
0

This recipe is a way to build an app using boost lib with Visual Studio 2022 and CMake.

  1. Run CMD.exe
  2. Set environment variable
set BOOST_ROOT=C:\boost_1_55_0
  1. Run Visual Studio 2022 in the same CMD window of previous step 2.
"C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\IDE\devenv.exe" 

You can skip above steps 1, 2, 3 if you set BOOST_ROOT in your system environment variables.

  1. Create project folder and put the following two files.
  • main.cpp
// main.cpp
#include <boost/filesystem.hpp>
#include <iostream>

int main()
{
    std::string file_path = "c:\\Windows";
    
    if(boost::filesystem::exists(file_path))
        std::cout << file_path + " is exist\n";
    else
        std::cout << file_path + " is not exist\n";

    return 0;
}
  • CMakeLists.txt
# CMakeLists.txt
cmake_minimum_required(VERSION 3.16)
project(main)
find_package(Boost COMPONENTS filesystem REQUIRED)
include_directories(${Boost_INCLUDE_DIRS})
link_directories(${Boost_LIBRARY_DIRS})
add_executable(main main.cpp)
target_link_libraries(main Boost::filesystem)
  1. Click File/Open/Folder menu and select a folder containing source files with Visual Studio.
  2. Build will be done.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.