简体   繁体   English

(C++17; Boost) CMake 无法找到请求的 Boost 库

[英](C++17; Boost) CMake unable to find the requested Boost libraries

I am new to C++ and want to include the boost library (specifically the filesystem part which needs to be built) in my project.我是 C++ 的新手,想在我的项目中包含 boost 库(特别是需要构建的文件系统部分)。 I tried many solutions from other stackoverflow users but they didn't help me at all.我尝试了其他 stackoverflow 用户的许多解决方案,但他们根本没有帮助我。 I am using CLion with CMake.我正在使用带有 CMake 的 CLion。 The main.cpp is calling the other.cpp files inside the modules/ folder. main.cpp 正在调用modules/文件夹中的 other.cpp 文件。

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.下载并解压缩时,boost 文件夹不包含整个 boost。 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)我在我的项目中将 boost_1_72_0中的 boost 文件夹拖到了里面(只是为了让你知道里面没有libs文件夹等)

The CMakeLists.txt 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 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.我知道它基本上告诉我我必须做什么,但我不知道 boost 的“根目录”、“包含 Boost 的标头”的目录以及如何将所有内容放在一起的确切含义。

Many thanks in advance!提前谢谢了!

I dragged the boost folder inside of boost_1_72_0 in my project我在我的项目中将 boost_1_72_0 中的 boost 文件夹拖到了里面

Looks like you just copied boost source into your project dir.看起来您刚刚将 boost 源复制到您的项目目录中。 You have to compile boost since you need filesystem.你必须编译boost 因为你需要文件系统。 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.由于您通过调用find_package(Boost)来使用boost - CMake 使用FindBoost模块。 It will try to find your boost installation inside system PATH variable or in some other "standard" places.它将尝试在系统PATH变量或其他一些“标准”位置找到您的 boost 安装。 Your boost "installation" is not common, so you have to specify where boost is with BOOST_ROOT variable.您的升压“安装”并不常见,因此您必须使用 BOOST_ROOT 变量指定升压的位置。 set(BOOST_ROOT boost/) is incorrect way to do this. set(BOOST_ROOT boost/)是不正确的方法。 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} .您必须指定绝对路径,例如set(BOOST_ROOT "C:/lib/boost/boost17.2")或相对于当前 CMakeList.txt - set(BOOST_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/boost}

With right installed boost all you have to do is:安装正确的 boost 您所要做的就是:

    find_package(Boost REQUIRED [COMPONENTS <libs>...])
    target_link_libraries(main PRIVATE ${Boost_LIBRARIES})
    target_include_directories(main PRIVATE ${Boost_INCLUDE_DIRS})

Usually, you don't need to set Boost_ARCHITECTURE and Boost_INCLUDE_DIRS CMake does it for you.通常,您不需要设置Boost_ARCHITECTUREBoost_INCLUDE_DIRS CMake 会为您完成。

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.当您使用带有REQUIRED选项的find_package时,您不需要检查是否找到库,因为 CMake 在找不到库时会引发错误。

BOOST_ROOT is a directory when boost installed or unpacked. BOOST_ROOT是 boost 安装或解压时的目录。 BOOST_INCLUDEDIR is a directory with boost headers (usually it's BOOST_ROOT/boost ). BOOST_INCLUDEDIR是一个带有 boost 标头的目录(通常是BOOST_ROOT/boost )。 So try to set the full path to your boost_1_72_0 directory to BOOST_ROOT CMake variable.因此,尝试将boost_1_72_0目录的完整路径设置为BOOST_ROOT CMake 变量。

Also, I had a problem with COMPONENTS option.另外,我对COMPONENTS选项有疑问。 Try to remove it if errors remain.如果错误仍然存在,请尝试将其删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM