Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know if libstdc++ support std::regex

I have an application which uses std::regex,it builds and runs fine on my computer. However, on ubuntu 14.04, it builds but doesn't work, because std::regex is not implemented on that old libstdc++.

I would like to #ifdef some lines in my code to workaround this issue but can seem to find a reliable way to figure out if the libstdc++ supports it or not.

I'm using cmake, so I would also be happy if I was able to get the version of the library (/usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19 -> 6019).

like image 477
Sergio Martins Avatar asked Feb 17 '26 20:02

Sergio Martins


1 Answers

You can write a simple test to check whether it works. First the CMake part:

include(CMakePushCheckState)
include(CheckCXXSourceRuns)
cmake_push_check_state()
set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -std=c++11")
check_cxx_source_runs("
      #include <regex>

      int main() {
        return std::regex_match(\"StackOverflow\", std::regex(\"(stack)(.*)\"));
      }
" HAVE_CXX_11_REGEX)
cmake_pop_check_state()

Then you have to include the variable HAVE_CXX_11_REGEX into you config.h. Then you can test this with #ifdef HAVE_CXX_11_REGEX.

like image 151
usr1234567 Avatar answered Feb 20 '26 15:02

usr1234567