18

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
Vinzenz
  • 2,749
  • 17
  • 23
Pritesh Acharya
  • 1,596
  • 5
  • 15
  • 36
  • 1
    Try setting `BOOST_ROOT=C:\boost_1_55_0` before running `cmake` again. Also look at `cmake --help-module FindBoost` for more help. – Stefan Näwe Jan 07 '14 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) – Pritesh Acharya Jan 08 '14 at 03:33
  • 2
    You don't have to put that `set(...` into your CMakeLists.txt! Just set an environment variable before calling `cmake`. – Stefan Näwe Jan 08 '14 at 08:11

6 Answers6

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
DJ Quimby
  • 3,669
  • 25
  • 35
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

dothebart
  • 5,972
  • 16
  • 40
  • That's because the BOOST_LIBRARYDIR is defined as ` BOOST_LIBRARYDIR - Preferred library directory e.g. /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 Feb 14 '17 at 07: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.

Stefan Näwe
  • 3,040
  • 1
  • 18
  • 19
6

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
Chnossos
  • 9,971
  • 4
  • 28
  • 40
asami
  • 773
  • 6
  • 12
  • 1
    only answer that helped me so far – IceFire Jun 05 '18 at 09: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: https://stackoverflow.com/questions/8940249/boost-how-bjam-constructs-a-library-name. It doesn't really help you to switch it from default either. Especially, I had to include the compiler name as above. – thadk Jun 04 '20 at 00:01
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})
ale5000
  • 1
  • 1
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.
Hill
  • 3,391
  • 3
  • 24
  • 28