daniel451
daniel451

Reputation: 11002

C++11 CMake: Regex fails

I ran into the problem that gcc/g++ <= 4.8.X does not support RegEx (my first reaction was: WHAT?!).

After installing (Ubuntu 14.04 LTS) gcc-4.9 and g++-4.9 (which is supposed to support RegEx properly) I still the get same error:

terminate called after throwing an instance of 'std::regex_error'
  what():  regex_error
[1]    13608 abort (core dumped)

My CMakeLists.txt looks like this (working with Jetbrains CLion as IDE):

set(CMAKE_CXX_COMPILER g++-4.9)

cmake_minimum_required(VERSION 3.1)

project(project1)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

set(SOURCE_FILES main.cpp)

add_executable(project1 ${SOURCE_FILES})

My code looks like this:

#include <iostream>
#include <string>
#include <fstream>
#include <regex>

using namespace std;

(...)

char encryptChar(char cinput)
{
    std::string s = std::string(1, cinput);
    // simplified regex (also crashes)
    std::regex e = std::regex("[a-z]");

    if(std::regex_match(s, e))
    {
        // do some stuff, if string matches conditions
    }

    return cinput;
}

Compiler/Linker do not complain about anything. Program runs fine without the regex-lines.

> g++-4.9 --version
>>> g++-4.9 (Ubuntu 4.9.2-0ubuntu1~14.04) 4.9.2
>>> Copyright (C) 2014 Free Software Foundation, Inc.

EDIT: After manually compiling the code with g++-4.9 -std=c++11 main.cpp the regex works. Why does the IDE/CMake version fails?

Upvotes: 3

Views: 1727

Answers (1)

daniel451
daniel451

Reputation: 11002

Finally, I found the problem:

My CMake version was 2.8-ish so CMake itself failed, Jetbrains CLion uses a custom CMake (shipped with the IDE) which is some 3.1-ish but also failed with RegEx.

I downloaded CMake 3.2.2 (newest version) and installed it (installation notes). Now compiling with CMake uses g++-4.9 properly and RegEx runs fine. In CLion I had to change the settings to ignore the custom CMake and use my systems CMake 3.2.2, now compiling with the IDE uses g++-4.9 properly, too and RegEx runs fine.

Upvotes: 2

Related Questions