C ++ 11 CMake: Regex doesn't work
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 should support RegEx properly), I still get the 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;
}
The compiler / linker doesn't complain about anything. The program works fine without 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 using the g++-4.9 -std=c++11 main.cpp
regex works. Why is the IDE / CMake version crashing?
source to share
Finally I found the problem:
My CMake version was 2.8-ish so CMake itself failed, Jetbrains CLion uses custom CMake (comes with IDE) which is some 3.1-ish but also not with RegEx.
I downloaded CMake 3.2.2 (newest version) and installed it ( installation notes ). Now compiling using CMake uses g ++ - 4.9 correctly and RegEx works fine. In CLion I had to change the settings to ignore the custom CMake and use my CMake 3.2.2 systems, now compiling with the IDE also uses g ++ - 4.9 correctly and RegEx works fine.
source to share