2

I am new to C++ and want to include the boost library (specifically the filesystem part which needs to be built) in my project. I tried many solutions from other stackoverflow users but they didn't help me at all. I am using CLion with CMake. The main.cpp is calling the other .cpp files inside the modules/ folder.

The file structure:

ProjectName
    >boost
        >lots of folders and .hpp files
    >cmake-build-debug
    >modules
        encryption.cpp
        encryption.h
        output.cpp
        output.h
    CMakeLists.txt
    main.cpp

The boost folder doesn't contain the entirety of boost when you download and extract it. I dragged the boost folder inside of boost_1_72_0 in my project (just so you know that there's no libs folder, etc. inside)

The CMakeLists.txt

cmake_minimum_required(VERSION 3.14)
project(ProjectName)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static-libstdc++ -static-libgcc")

set(SOURCE_FILES
        main.cpp
        modules/encryption.cpp modules/encryption.h modules/output.cpp modules/output.h
        )

set(Boost_ARCHITECTURE -x64)
set(BOOST_ROOT boost/)
set(Boost_INCLUDE_DIRS boost/filesystem)
find_package(Boost COMPONENTS system filesystem REQUIRED)
if(Boost_FOUND)
    include_directories(${Boost_INCLUDE_DIRS})
endif()

add_executable(ProjectName ${SOURCE_FILES})
target_link_libraries(ProjectName ${Boost_LIBRARIES})

output.cpp

// some includes //
#define BOOST_FILESYSTEM_NO_DEPRECATED

#include "../boost/filesystem.hpp"

// some code //

The Error message:

CMake Error at C:/Program Files/JetBrains/CLion 2019.1.4/bin/cmake/win/share/cmake-3.14/Modules/FindBoost.cmake:2147 (message):
  Unable to find the requested Boost libraries.

  Unable to find the Boost header files.  Please set BOOST_ROOT to the root
  directory containing Boost or BOOST_INCLUDEDIR to the directory containing
  Boost's headers.
Call Stack (most recent call first):
  CMakeLists.txt:15 (find_package)


-- Configuring incomplete, errors occurred!
See also "C:/Users/username/Desktop/C++/ProjectName/cmake-build-debug/CMakeFiles/CMakeOutput.log".
mingw32-make.exe: *** [cmake_check_build_system] Error 1
Makefile:235: recipe for target 'cmake_check_build_system' failed

I know that it's basically telling me what I have to do but I don't know what's exactly meant by the "root directory" of boost, by the directory "containing Boost's headers" and how to put everything together.

Many thanks in advance!

Frenggie
  • 149
  • 4
  • 16

2 Answers2

1

I dragged the boost folder inside of boost_1_72_0 in my project

Looks like you just copied boost source into your project dir. You have to compile boost since you need filesystem. Or you can get boost from:

I don't know what's exactly meant by the "root directory"...

Since you are using boost by calling find_package(Boost) - CMake uses FindBoost module. It will try to find your boost installation inside system PATH variable or in some other "standard" places. Your boost "installation" is not common, so you have to specify where boost is with BOOST_ROOT variable. set(BOOST_ROOT boost/) is incorrect way to do this. You have to specify absolute path like set(BOOST_ROOT "C:/lib/boost/boost17.2") or relative to current CMakeList.txt - set(BOOST_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/boost}.

With right installed boost all you have to do is:

    find_package(Boost REQUIRED [COMPONENTS <libs>...])
    target_link_libraries(main PRIVATE ${Boost_LIBRARIES})
    target_include_directories(main PRIVATE ${Boost_INCLUDE_DIRS})
  • I added your solution to my CMakeLists.txt file but I still have to compile boost. For some reason vcpkg won't work (I'm getting and MSBuild with C++ support error). When compiling boost manually with *bootstrap.bat* I get an error in the logfile: *Unknown toolset: vcunk*. – Frenggie Apr 12 '20 at 22:05
  • @Frenggie vcpkg required MSVC compiler. Do you have Visual Studio with c++ tools or standalone VS build tools installed? – VladBolotov Apr 12 '20 at 22:10
  • I have Visual Studio but without C++ tools. I am using CLion with mingw and cmake. Is there any way to build boost without any VS build tools? – Frenggie Apr 12 '20 at 23:32
  • I am installing 12 GB of Build Tools right now just to be sure I got anything I need. I used [this](https://stackoverflow.com/questions/42696948/how-can-i-install-the-vs2017-version-of-msbuild-on-a-build-server-without-instal) thread with the command from the top answer. I hope this will be it. – Frenggie Apr 12 '20 at 23:35
  • @Frenggie Yes, you can build boost with MinGW. Also you can create custom MinGW [triplet](https://github.com/Microsoft/vcpkg/blob/master/docs/users/triplets.md) for vcpkg. But I suggest you stick with MSVC. You can add [MSVC compiler to CLion](https://www.jetbrains.com/help/clion/quick-tutorial-on-configuring-clion-on-windows.html#MSVC). – VladBolotov Apr 13 '20 at 05:42
  • I've installed VC Build libraries. But when I wanted to do the user wide integration of vcpkg my windows defender gave me a warning for Trojan:Virus\FueryC!cl. It's probably bs but I am kinda paranoid. Could there be a problem with vcpkg or my C++ installation? – Frenggie Apr 13 '20 at 13:59
  • @Frenggie VCPKG is a Microsoft project. Warning about Trojan sound… weird for me and I never ever had such “problem” before on all my Windows (7/8/10) machines with Defender ON. Consider creating an issue on VCPKG GitHub page and check your PC with some free AV utilities like [CureIT](https://free.drweb.com/download+cureit+free/?lng=en) or [KAV Removal Tool](https://www.kaspersky.com/downloads/thank-you/free-virus-removal-tool) – VladBolotov Apr 13 '20 at 14:24
  • I already ran 4 scans through the night (Defender Fast Scan, Defender Full Scan, Malwarebytes full scan, Malwarebytes scan of vcpkg files) and I uploaded vkpkg to Virustotal in case the project got compromised (very unlikely but possible I guess). No even one virus alert from those scans. The Defender message pointed directly on vcpkg.exe in the github files. Oh and I did an defender offline scan. – Frenggie Apr 13 '20 at 14:37
1

Usually, you don't need to set Boost_ARCHITECTURE and Boost_INCLUDE_DIRS CMake does it for you.

When you use find_package with REQUIRED option you don't need to check whether the library found or not since CMake raises an error when a library isn't found.

BOOST_ROOT is a directory when boost installed or unpacked. BOOST_INCLUDEDIR is a directory with boost headers (usually it's BOOST_ROOT/boost). So try to set the full path to your boost_1_72_0 directory to BOOST_ROOT CMake variable.

Also, I had a problem with COMPONENTS option. Try to remove it if errors remain.

triclosan
  • 5,578
  • 6
  • 26
  • 50