Determine if clang compiles using the standard libstdc ++ c ++ 11 library or the old libstdc ++ library

I have an Xcode project that I am porting for use with the clang option -stdlib libc++to enable C ++ 11 support. Some of my source files need to know which library is used, for example, so that I do things like this:

#ifdef HAVE_CPP11_LIB_SUPPORT
  #include <memory>
#else 
  #include <tr1/memory>
#endif

#ifdef HAVE_CPP11_LIB_SUPPORT
  vector.emplace_back(newValue);
#else 
  vector.push_back(newValue);
#endif

I am having trouble finding preprocessor macros (if any) that are installed for this option. I tried resetting the outputs of clang with:

clang -x c++ -std=c++11 -stdlib=libc++ -dM -E - < /dev/null

for comparison:

clang -x c++ -std=c++11 -stdlib=libstdc++ -dM -E - < /dev/null

but it gives the same results. Please note that I don’t want to include the C ++ 11 language, but do we use the C ++ 11 library. Is there a reliable way to detect this in the code?

+4
4

, , :

// libc++ detected:     _LIBCPP_VERSION
// libstdc++ detected:  __GLIBCXX__
#if defined(__clang__)
#   if __has_include(<__config>) // defines _LIBCPP_VERSION
#       include <__config>
#   elif __has_include(<bits/c++config.h>) // defines __GLIBCXX__
#       include <bits/c++config.h>
#   else
#       include <ios>
#   endif
#elif defined(__GNUC__) // gcc does not have __has_include
#   include <ios> // ios should include the c++config.h which defines __GLIBCXX__
#endif

, .

libc++ _LIBCPP_VERSION stdc++ __GLIBCXX__ , , , . , , .

: stdc++ __GLIBCPP__ . ++ 11, .

Clang (Edit: Standard ++ 17) __has_include , , , , , ., <ios> , . , - ( gcc linux):

grep -Rl '#include <bits/c++config.h>' /usr/include/c++

, , , .

/ , , , :

#ifdef __GLIBCXX__
    std::set_terminate(__gnu_cxx::__verbose_terminate_handler);
#endif
+6
#ifdef __has_include
# if __has_include(<ciso646>)
#  include <ciso646>
#  if defined(_LIBCPP_VERSION)
#   define USING_LIBCPP 1
#  endif
# endif
#endif

#if !USING_LIBCPP
# define USING_LIBSTDCXX 1
#endif
+1

If you are writing these types of checks, I suggest you select a compiler / library version and require it or a newer one. It makes no sense to use the C ++ 11 library libraries. On Mac OS X, it just needs to be compiled with clang++ -stdlib=libc++.

0
source

I am using the following code:

#include <cstddef> // for __GLIBCXX__

#ifdef __GLIBCXX__
#  include <tr1/memory>
#else
#  include <memory>
#endif

source

0
source

Source: https://habr.com/ru/post/1532643/


All Articles