Here's example project for CLion that uses Boost's regex library. It references to this tutorial.
Goal: with CLion create .exe that process jayne.txt as shown in Boost's tutorial.
My example revolves around building Boost Library Binary with GCC, configuring project in CLion and using CMake. It's quite expressive, maybe even an overkill, but I believe you can adapt. On the other hand I'll try to make project itself as system independent as its possible.
Configuration:
- OS: Windows 8.1
- CLion: 2018.2.4
- Boost: 1.68.0
- compiler: GCC-6.3.0 (provided by MinGW)
MinGW was configured according to its instructions and JetBrains's suggestions. (I downloaded setup for MinGW 14/10/2018 if that matters.)
A word about tools structure
I decided to create following structure for Boost and things aroung it:
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...
I left Boost root untouched -- I didn't create any additional directories. This separates Boost sources from created tools and libraries so I can show how to specify these directories explicitly.
Obtain Boost Library Binary
I decided to build libraries from source with 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 looks for files with specific names in library folder, so be sure to (copy and) change name for one of .a
files there. For this example, I changed libboost_regex-mgw63-mt-x32-1_68.a
to boost_regex.a
.
Create CLion project
Create a new project (for this example its name is CLionBoostRegex
) and put content to 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;
}
}
Configure CLion
Go to (on Windows) File -> Settings -> Build, Execution, Deployment -> CMake
, and in CMake options
add path to Boost root directory with -DBOOST_ROOT=
, i.e.: -DBOOST_ROOT="D:\SDK\boost\boost_1_68_0"
. If directory with built libraries is placed outside Boost root, add it with -DBOOST_LIBRARYDIR=
, i.e.: -DBOOST_LIBRARYDIR="D:\SDK\boost\1_68_0\stageDir\lib"
. Commands are space separated.
Decide if you want static or dynamic linking.
Option 1: Static linking
For static linking your CMakeLists.txt should look like this:
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})
Option 2: Dynamic linking
CMakeLists.txt should look like for static linking, but remove target_link_libraries(CLionBoostRegex -static)
line.
After building your project make sure to copy .dll
library to directory with executable (libboost_regex-mgw63-mt-x32-1_68.dll
) along with libstdc++-6.dll
from MinGW\bin
directory (D:\SDK\MinGW\bin
) (or consider including target_link_libraries(CLionBoostRegex -static-libstdc++)
line in CMakeLists.txt or add -DCMAKE_CXX_FLAGS="-static-libstdc++"
to CMake options
in settings).
Run your program (6.4)
- 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.
Notes
Changing name for an .a
library and choosing -static
linking causes the least effort afterwards - you won't have to copy any additional libraries at the price of bigger executable size. When executable size is more important, you can change name for .dll
library in Boost libraries directory instead and then copy missing .dll
s for your .exe
(i.e.: libboost_regex-mgw63-mt-x32-1_68.dll
and libstdc++-6.dll
).
You can include set(Boost_DEBUG ON)
line in your CMakeLists.txt or -DBoost_DEBUG=1
to CMake options
to get some precious info.
I used other questions to write this post, most notably: 1, 2, 3, 4.