13

How to use Boost library in Clion with MinGW ? I have downloaded and unzipped boost_1_60_0.zip to C:\boost_1_60_0. What am I supposed to do now ? Do I have to install something ? Here is my 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})

Can not find Boost:

1

dimitris93
  • 4,155
  • 11
  • 50
  • 86
  • 1
    Unfortunately, I don't know very well neither MinGW nor Clion. Is your boost already build or have you just downloaded the source (in which case, you have to follow the installation instructions-they are well made). Once you're sure boost is properly installed, you have to add the relevant library and header paths in the [compiler options](http://www.mingw.org/wiki/includepathhowto) (or in global variables such as LIB et INCLUDE). With CLion,there are certainly a place where you can configure default pathes to be looked for (unless it get it from the environment variables I just mentionned). – Christophe Apr 09 '16 at 17:08

2 Answers2

7

I use MinGW distro by Stephan T. Lavavej with Boost libraries prebuilt.

In my cmaklist.txt I added this

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})

This post help me get it going. How to include external library (boost) into CLion C++ project with CMake?

Community
  • 1
  • 1
Hellonearthis
  • 1,664
  • 1
  • 18
  • 26
7

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.

  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 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)

  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.

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 .dlls 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.

kszyniu
  • 81
  • 1
  • 2
  • 4