在Clion中设置Boost

13

如何在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


1
很遗憾,我对MinGW和Clion都不是很了解。你的boost已经构建好了还是只是下载了源代码(如果是后者,你需要按照安装说明进行安装-它们做得很好)。一旦你确定boost已经正确安装,你就需要在编译器选项中添加相关的库和头文件路径(或者在全局变量中,如LIB和INCLUDE)。使用CLion时,肯定有一个地方可以配置要查找的默认路径(除非它从我刚提到的环境变量中获取)。 - Christophe
2个回答

7
我使用由Stephan T. Lavavej提供的MinGW发行版,并预先构建了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})

这篇文章帮助我启动了项目。如何使用CMake将外部库(boost)包含到CLion C++项目中?

这也是我做的事情。忘记发布答案了。 - dimitris93
我正在使用MSVC,你能展示一下如何在MSVC中完成吗? - Nguyễn Đức Tâm

7
这是一个使用Boost的正则表达式库的CLion示例项目。它参考了这个教程。
目标:使用CLion创建.exe文件,按照Boost的教程处理jayne.txt。
我的示例围绕着使用GCC构建Boost库二进制文件,配置CLion项目并使用CMake展开。这可能有点冗余,但我相信你可以适应。另一方面,我会尽可能使项目本身与系统无关。
配置:
操作系统:Windows 8.1
CLion:2018.2.4
Boost:1.68.0
编译器:GCC-6.3.0(由MinGW提供)
MinGW根据其说明JetBrains的建议进行了配置。(如果有影响,我于2018年10月14日下载了MinGW的安装程序。)
关于工具结构的说明:
我决定为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. Download and unpack Boost ("D:\SDK\boost\boost_1_68_0");
  2. Build libraries (5.2):

    1. Open command prompt at \tools\build of Boost root ("D:\SDK\boost\boost_1_68_0\tools\build")
    2. Run bootstrap.bat
    3. Run b2 install --prefix="D:\SDK\boost\1_68_0\build" --toolset=gcc-6.3.0. This creates b2.exe under "D:\SDK\boost\1_68_0\build\bin"
    4. Move command prompt to Boost root (cd "D:\SDK\boost\boost_1_68_0")
    5. (You can consider using "D:\SDK\boost\1_68_0\build\bin\b2.exe --show-directories". It's worth to specify libraries to build (--with-library-name) in the following command, because this step can take a while.) Run "D:\SDK\boost\1_68_0\build\bin\b2" --build-dir="D:\SDK\boost\1_68_0\buildDir" toolset=gcc-6.3.0 --build-type=complete stage --stagedir="D:\SDK\boost\1_68_0\stageDir" --with-regex. It creates following files under D:\SDK\boost\1_68_0\stageDir\lib directory:

      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上)文件 -> 设置 -> 构建,执行,部署 -> CMake,在CMake选项中添加指向Boost根目录的路径,并使用-DBOOST_ROOT=,例如:-DBOOST_ROOT="D:\SDK\boost\boost_1_68_0"。如果构建库的目录位于Boost根目录之外,则使用-DBOOST_LIBRARYDIR=添加它,例如:-DBOOST_LIBRARYDIR="D:\SDK\boost\1_68_0\stageDir\lib"。命令使用空格分隔。

决定你是想进行静态链接还是动态链接。

选项一:静态链接

对于静态链接,您的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})

方法二:动态链接

CMakeLists.txt的设置应该与静态链接相同,但是要删除target_link_libraries(CLionBoostRegex -static)这一行。

在构建项目后,请确保将.dll库文件从MinGW\bin目录(D:\SDK\MinGW\bin)复制到可执行文件所在的目录(libboost_regex-mgw63-mt-x32-1_68.dll),同时还需将libstdc++-6.dll一同复制。或者考虑在CMakeLists.txt文件中包含target_link_libraries(CLionBoostRegex -static-libstdc++)这一行,或者在设置的CMake选项中添加-DCMAKE_CXX_FLAGS="-static-libstdc++"

运行程序(6.4)

  1. Build your target (it creates cmake-build-debug\ directory with default config) (if you picked dynamic linking make sure to add necessary .dlls)
  2. In directory with your executable create jayne.txt file with following content:

    To: George Shmidlap
    From: Rita Marlowe
    Subject: Will Success Spoil Rock Hunter?
    ---
    See subject.
    
  3. Open command prompt there
  4. Run CLionBoostRegex.exe < jayne.txt
  5. Program should output Will Success Spoil Rock Hunter? as shown in tutorial.

笔记

.a 库更改名称并选择 -static 链接是后续工作中最简单的方法,你不必复制任何额外的库,但要付出更大的可执行文件大小。当可执行文件大小更重要时,可以在 Boost 库目录中更改 .dll 库的名称,然后为你的 .exe 复制缺失的 .dll(例如: libboost_regex-mgw63-mt-x32-1_68.dlllibstdc++-6.dll)。

你可以在你的 CMakeLists.txt 中包含 set(Boost_DEBUG ON) 行或将 -DBoost_DEBUG=1 添加到 CMake 选项 中以获取一些宝贵的信息。

我参考了其他问题来编写此帖,尤其是:1234


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接