这是一个使用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\
boost\
rst.css
...other directories and files...
1_68_0\
build\
buildDir\
stageDir\
lib\
MinGW\
bin\
...other directories...
我保持了Boost根目录不变,没有创建任何额外的目录。这样可以将Boost源代码与创建的工具和库文件分开,以便明确指定这些目录。
获取Boost库二进制文件
我决定使用GCC从源代码构建库文件。
- Download and unpack Boost (
"D:\SDK\boost\boost_1_68_0"
);
Build libraries (5.2):
- Open command prompt at
\tools\build
of Boost root ("D:\SDK\boost\boost_1_68_0\tools\build"
)
- Run
bootstrap.bat
- 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"
- Move command prompt to Boost root (
cd "D:\SDK\boost\boost_1_68_0"
)
(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++"
。
- Build your target (it creates
cmake-build-debug\
directory with default config) (if you picked dynamic linking make sure to add necessary .dll
s)
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.
- Open command prompt there
- Run
CLionBoostRegex.exe < jayne.txt
- 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.dll
和 libstdc++-6.dll
)。
你可以在你的 CMakeLists.txt 中包含 set(Boost_DEBUG ON)
行或将 -DBoost_DEBUG=1
添加到 CMake 选项
中以获取一些宝贵的信息。
我参考了其他问题来编写此帖,尤其是:1、2、3、4。