这是使用 Boost 正则表达式库的 CLion 示例项目。它引用了this 教程。
目标:使用 CLion 创建处理 jayne.txt 的 .exe,如 Boost 教程中所示。
我的示例围绕使用 GCC 构建 Boost Library Binary、在 CLion 中配置项目和使用 CMake。它很有表现力,甚至可能有点矫枉过正,但我相信你可以适应。另一方面,我会尽量让项目本身独立于系统。
配置:
- 操作系统:Windows 8.1
- 克莱恩:2018.2.4
- 提升:1.68.0
- 编译器:GCC-6.3.0(MinGW提供)
MinGW 是根据its instructions 和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 从源代码构建库。
- 下载并解压 Boost (
"D:\SDK\boost\boost_1_68_0");
-
构建库(5.2):
- 在 Boost root (
"D:\SDK\boost\boost_1_68_0\tools\build") 的 \tools\build 打开命令提示符
- 运行
bootstrap.bat
- 运行
b2 install --prefix="D:\SDK\boost\1_68_0\build" --toolset=gcc-6.3.0。这会在"D:\SDK\boost\1_68_0\build\bin" 下创建 b2.exe
- 将命令提示符移至 Boost 根目录 (
cd "D:\SDK\boost\boost_1_68_0")
-
(您可以考虑使用"D:\SDK\boost\1_68_0\build\bin\b2.exe --show-directories"。值得在以下命令中指定要构建的库(--with-library-name),因为此步骤可能需要一段时间。)运行"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。它在D:\SDK\boost\1_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 根目录之外,请使用 -DBOOST_LIBRARYDIR= 添加它,即:-DBOOST_LIBRARYDIR="D:\SDK\boost\1_68_0\stageDir\lib"。命令以空格分隔。
决定是要静态链接还是动态链接。
选项 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) 的目录以及 MinGW\bin 目录中的 libstdc++-6.dll (D:\SDK\MinGW\bin) (或考虑在 CMakeLists.txt 中包含target_link_libraries(CLionBoostRegex -static-libstdc++) 行或在设置中将-DCMAKE_CXX_FLAGS="-static-libstdc++" 添加到CMake options。
- 构建您的目标(它使用默认配置创建
cmake-build-debug\ 目录)(如果您选择动态链接,请确保添加必要的.dlls)
-
在您的可执行文件目录中创建jayne.txt 文件,内容如下:
To: George Shmidlap
From: Rita Marlowe
Subject: Will Success Spoil Rock Hunter?
---
See subject.
- 在那里打开命令提示符
- 运行
CLionBoostRegex.exe < jayne.txt
- 程序应输出
Will Success Spoil Rock Hunter?,如教程所示。
注意事项
更改.a 库的名称并选择-static 链接后工作量最少 - 您不必以更大的可执行文件大小为代价复制任何其他库。当可执行文件大小更重要时,您可以改为在 Boost 库目录中更改 .dll 库的名称,然后为您的 .exe 复制缺少的 .dlls(即:libboost_regex-mgw63-mt-x32-1_68.dll 和 libstdc++-6.dll)。
您可以在 CMakeLists.txt 中包含 set(Boost_DEBUG ON) 行或 -DBoost_DEBUG=1 到 CMake options 以获取一些宝贵的信息。
我使用其他问题来写这篇文章,最值得注意的是:1、2、3、4。