3

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

Sergio Martins
  • 897
  • 2
  • 8
  • 18

1 Answers1

3

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.

arrowd
  • 33,231
  • 8
  • 79
  • 110
usr1234567
  • 21,601
  • 16
  • 108
  • 128
  • thanks, I ended up cmake's TRY_RUN, which is similar – Sergio Martins Oct 31 '15 at 20:06
  • 1
    This almost works, but will abort on unhandled `std::regex_error`, which g++-4.8-compiled program would throw (due to unusable implementation of `std::regex` in GCC 4.8). In some cases this is OK, but not very good if user saves core dumps somewhere. This script would litter that location then. – Ruslan Jun 29 '16 at 12:47