在 Clion 中设置 Boost

Setup Boost in Clion

如何在 Clion 和 MinGW 中使用 Boost 库?我已经下载并解压 boost_1_60_0.zipC:\boost_1_60_0。我现在该怎么办?我必须安装一些东西吗?这是我的 CMakeLists.txt:

cmake_minimum_required(VERSION 3.3)
project(server_client)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -s -O3")
set(CMAKE_EXE_LINKER_FLAGS -static)

set(BOOST_ROOT "C:/boost_1_60_0")
set(BOOSTROOT "C:/boost_1_60_0")
find_package(Boost 1.60.0)
if(NOT Boost_FOUND)
    message(FATAL_ERROR "Could not find boost!")
endif()

set(SOURCE_FILES chat_server.cpp)
add_executable(server_client ${SOURCE_FILES})

找不到Boost:

  1. 首先你必须get started with boost. As boost.asio is not a header-only library, you must be sure that the library was properly build.

  2. 然后您必须配置 CLion 以采用 headers 和库的正确路径。可以在 SO question 上找到第二个问题的解决方案。

我使用 MinGW Stephan T. Lavavej 的发行版和预建的 Boost 库。

在我的 cmaklist.txt 我添加了这个

set(Boost_INCLUDE_DIR c:/mingw/include/)
set(Boost_LIBRARY_DIR c:/mingw/lib/)
find_package(Boost COMPONENTS system filesystem REQUIRED)
include_directories(${Boost_INCLUDE_DIR})

这个 post 帮助我开始它。

这是使用 Boost 的正则表达式库的 CLion 示例项目。它引用了 this 教程。

目标:使用 CLion 创建 .exe,该进程 jayne.txt 如 Boost 教程中所示。

我的示例围绕使用 GCC 构建 Boost 库二进制文件、在 CLion 中配置项目和使用 CMake。它很有表现力,甚至可能有点矫枉过正,但我​​相信你可以适应。另一方面,我将尝试使项目本身尽可能独立于系统。

配置:

  • OS: Windows 8.1
  • CLion: 2018.2.4
  • 提升:1.68.0
  • 编译器:GCC-6.3.0(由MinGW提供)

MinGW 是根据 its instructions and JetBrains's suggestions 配置的。 (如果重要的话,我下载了 MinGW 14/10/2018 的安装程序。)

关于工具结构的一句话

我决定为 Boost 及其周围的事物创建以下结构:

D:\
  SDK\
    boost\
      boost_1_68_0\     # untouched Boost root
        boost\
        rst.css
        ...other directories and files...
      1_68_0\
        build\          # dir for Boost.Build, b2.exe
        buildDir\       # intermediate dir for creating libraries
        stageDir\       # dir for libraries
          lib\
    MinGW\
      bin\
      ...other directories...

我保留了 Boost 根目录——我没有创建任何额外的目录。这将 Boost 源与创建的工具和库分开,因此我可以展示如何明确指定这些目录。

获取 Boost 库二进制文件

我决定使用 GCC 从源代码构建库。

  1. 下载并解压 Boost ("D:\SDK\boost\boost_1_68_0");
  2. 构建库(5.2):

    1. 在 Boost root ("D:\SDK\boost\boost_1_68_0\tools\build") 的 \tools\build 打开命令提示符
    2. 运行bootstrap.bat
    3. 运行b2 install --prefix="D:\SDK\boost_68_0\build" --toolset=gcc-6.3.0。这会在 "D:\SDK\boost_68_0\build\bin"
    4. 下创建 b2.exe
    5. 将命令提示符移动到 Boost root (cd "D:\SDK\boost\boost_1_68_0")
    6. (你可以考虑使用"D:\SDK\boost_68_0\build\bin\b2.exe --show-directories"。值得在以下命令中指定要构建的库(--with-library-name),因为这一步可能需要一段时间。) 运行 "D:\SDK\boost_68_0\build\bin\b2" --build-dir="D:\SDK\boost_68_0\buildDir" toolset=gcc-6.3.0 --build-type=complete stage --stagedir="D:\SDK\boost_68_0\stageDir" --with-regex。它在 D:\SDK\boost_68_0\stageDir\lib 目录下创建以下文件:

      libboost_regex-mgw63-mt-d-x32-1_68.a
      libboost_regex-mgw63-mt-d-x32-1_68.dll
      libboost_regex-mgw63-mt-d-x32-1_68.dll.a
      libboost_regex-mgw63-mt-sd-x32-1_68.a
      libboost_regex-mgw63-mt-s-x32-1_68.a
      libboost_regex-mgw63-mt-x32-1_68.a
      libboost_regex-mgw63-mt-x32-1_68.dll
      libboost_regex-mgw63-mt-x32-1_68.dll.a
      

CMake 在库文件夹中查找具有特定名称的文件,因此请务必(复制并)更改其中 .a 个文件之一的名称。对于此示例,我将 libboost_regex-mgw63-mt-x32-1_68.a 更改为 boost_regex.a

创建 CLion 项目

创建一个新项目(在本例中其名称为CLionBoostRegex)并将内容放入main.cpp (6):

#include <boost/regex.hpp>
#include <iostream>
#include <string>

int main()
{
    std::string line;
    boost::regex pat( "^Subject: (Re: |Aw: )*(.*)" );

    while (std::cin)
    {
        std::getline(std::cin, line);
        boost::smatch matches;
        if (boost::regex_match(line, matches, pat))
            std::cout << matches[2] << std::endl;
    }
}

配置 CLion

转到(Windows)File -> Settings -> Build, Execution, Deployment -> CMake,并在 CMake options 中使用 -DBOOST_ROOT= 添加到 Boost 根目录的路径,即:-DBOOST_ROOT="D:\SDK\boost\boost_1_68_0"。如果构建库的目录位于 Boost root 之外,请添加 -DBOOST_LIBRARYDIR=,即:-DBOOST_LIBRARYDIR="D:\SDK\boost_68_0\stageDir\lib"。命令 space 分开。

决定是要静态链接还是动态链接。

选项 1:静态链接

对于静态链接,您的 CMakeLists.txt 应该如下所示:

cmake_minimum_required(VERSION 3.12)
project(CLionBoostRegex)
set(CMAKE_CXX_STANDARD 98)

find_package(Boost REQUIRED COMPONENTS regex)
include_directories(${Boost_INCLUDE_DIRS})
add_executable(CLionBoostRegex main.cpp)
target_link_libraries(CLionBoostRegex -static)
target_link_libraries(CLionBoostRegex ${Boost_LIBRARIES})

选项 2:动态链接

CMakeLists.txt 应该看起来像静态链接,但删除 target_link_libraries(CLionBoostRegex -static) 行。

构建项目后,确保将 .dll 库复制到包含可执行文件 (libboost_regex-mgw63-mt-x32-1_68.dll) 的目录 以及 libstdc++-6.dll 来自 MinGW\bin 目录 (D:\SDK\MinGW\bin) ( 考虑在 CMakeLists.txt 中包含 target_link_libraries(CLionBoostRegex -static-libstdc++) 行 添加 -DCMAKE_CXX_FLAGS="-static-libstdc++"CMake options 在设置中)。

运行你的程序(6.4)

  1. 构建你的目标(它会创建 cmake-build-debug\ 带有默认配置的目录)(如果你选择了动态链接,请确保添加必要的 .dlls)
  2. 在包含可执行文件的目录中创建 jayne.txt 文件,内容如下:

    To: George Shmidlap
    From: Rita Marlowe
    Subject: Will Success Spoil Rock Hunter?
    ---
    See subject.
    
  3. 在那里打开命令提示符
  4. 运行CLionBoostRegex.exe < jayne.txt
  5. 程序应如教程中所示输出 Will Success Spoil Rock Hunter?

备注

更改 .a 库的名称并选择 -static 链接后的工作量最少 - 您不必以更大的可执行文件大小为代价复制任何其他库。当可执行文件的大小更重要时,您可以在 Boost 库目录中更改 .dll 库的名称,然后为 .exe 复制缺少的 .dll(即:libboost_regex-mgw63-mt-x32-1_68.dlllibstdc++-6.dll).

您可以在 CMakeLists.txt -DBoost_DEBUG=1CMake options 中包含 set(Boost_DEBUG ON) 行以获得一些宝贵的信息。

我用了其他问题来写这个 post,最值得注意的是:1, 2, , 4