Wie erkennt man -stdlib = libc ++ im Präprozessor?

Ich denke, das ist ein Teil des Problems beiNo type named 'unique_ptr' im Namespace 'std' beim Kompilieren unter LLVM / Clang. aut Marshall Cl, Ich kann @ erkenn-stdlib=libc++ via_LIBCPP_VERSION:

Wenn Sie plattformübergreifenden Code schreiben, müssen Sie manchmal wissen, welche Standardbibliothek Sie verwenden. Theoretisch sollten sie alle die gleiche Funktionalität bieten, aber das ist nur Theorie. Manchmal muss man es einfach wissen. Die beste Möglichkeit, nach libc ++ zu suchen, besteht darin, nach dem Präprozessorsymbol _LIBCPP_VERSION zu suchen. Wenn dies definiert ist, verwenden Sie libc ++.

#ifdef  _LIBCPP_VERSION
//  libc++ specific code here
#else
//  generic code here
#endif

Das funktioniert leider nicht mit Apples Clang (3.4-SVN) und dem Clang (3.6), die ich nach dem Herunterladen aus dem LLVM-Projekt aus Quellen erstellt habe. Ich vermute, der Test ist nur unter Xcode gültig.

ie kann ich @ zuverlässig erkenne-stdlib=libc++ im Präprozessor?

Hier ist der Testfall:

$ cat test-clapple.cxx

// Need to test {C++03,C++11} x {libc++, no libc++}

// c++ -c test-clapple.cxx
//     - OK
// c++ -stdlib=libc++ -c test-clapple.cxx
//     - OK
// c++ -std=c++11 -c test-clapple.cxx
//     - FAILS, no type named 'unique_ptr' in namespace 'std'
// c++ -std=c++11 -stdlib=libc++ -c test-clapple.cxx
//     - OK

#include <ciso646>

#if (__cplusplus >= 201103L) || (_MSC_VER >= 1600)
# pragma message "C++11"
#elif (__cplusplus >= 199711L)
# pragma message "C++03"
#endif

#if (_LIBCPP_VERSION)
# pragma message "libc++"
#else
# pragma message "no libc++"
#endif

#if defined(__apple_build_version__)
# pragma message "Apple build"
#else
# pragma message "non-Apple build"
#endif

#if (__cplusplus >= 201103L) || (_MSC_VER >= 1600) // C++11
# include <memory>
#else
# include <tr1/memory>
#endif

// Manage auto_ptr warnings and deprecation in C++11
#if (__cplusplus >= 201103L) || (_MSC_VER >= 1600)
  template<typename T>
    using auto_ptr = std::unique_ptr<T>;
#else
  using std::auto_ptr;
#endif // C++11

int main(int argc, char* argv[])
{
    return argc;
}

Dieses Projekt verwendet keine Autotools, Cmake, Boost oder andere externe Bibliotheken oder Frameworks.

Antworten auf die Frage(2)

Ihre Antwort auf die Frage