如何在预处理器中检测-stdlib = libc ++? [英] How to detect -stdlib=libc++ in the preprocessor?

查看:563
本文介绍了如何在预处理器中检测-stdlib = libc ++?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为这是在LLVM / Clang 。 根据Marshall Clow ,我可以检测到 - stdlib = libc ++ 通过 _LIBCPP_VERSION

I think this is part of the problem at No type named 'unique_ptr' in namespace 'std' when compiling under LLVM/Clang. According to Marshall Clow, I can detect -stdlib=libc++ via _LIBCPP_VERSION:


编写跨平台代码,有时你需要知道你正在使用什么
标准库。在理论上,他们应该提供
等效功能,但这只是理论。有时你只需
需要知道。检查libc ++的最好方法是查找
预处理器符号_LIBCPP_VERSION。如果这是定义,那么你是
使用libc ++。

If you're writing cross-platform code, sometimes you need to know what standard library you are using. In theory, they should all offer equivalent functionality, but that's just theory. Sometimes you just need to know. The best way to check for libc++ is to look for the preprocessor symbol _LIBCPP_VERSION. If that's defined, then you're using libc++.

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


$不幸的是,这从苹果的Clang(3.4-SVN)和Clang(3.6)我从LLVM项目下载后从源代码分解。

Unfortunately, that breaks down with Apple's Clang (3.4-SVN) and the Clang (3.6) I built from sources after downloading from the LLVM project. I'm guessing the test is only valid under Xcode.

如何可靠地检测 -stdlib = libc ++ 预处理程序?

How can I reliably detect -stdlib=libc++ in the preprocessor?

这里是测试用例:

$ 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;
}






Autodesol,Cmake,Boost或其他外部库或框架。


This project does not use Autotools, Cmake, Boost, or other external libraries or frameworks.

推荐答案

唯一的效果 -stdlib = libc ++ 在预处理器上是更改用于查找标准库头文件的包含路径,所以你不能检测到 -stdlib = libc ++ 在命令行本身,你只能检测到哪些标准库头包括。显然你不能检测到没有实际包含一个或多个标准库头。

The only effect -stdlib=libc++ has on the preprocessor is to change the include paths it uses to find standard library headers, so you can't detect the presence of -stdlib=libc++ on the command-line per se, you can only detect which standard library headers get included. Obviously you can't detect that without actually including one or more standard library headers.

如果你包括任何libc ++头,那么 _LIBCPP_VERSION 将被定义,因此检测 -stdlib = libc ++ 的方法是至少包括一个C ++库头并检查 _LIBCPP_VERSION

If you include any libc++ header then _LIBCPP_VERSION will be defined, so the way to detect -stdlib=libc++ is to include at least one C++ library header and check for _LIBCPP_VERSION.

对于libc ++,建议使用 #include< ciso646> 在C ++中并没有声明,但是libc ++定义了 _LIBCPP_VERSION 宏。然而,对于libstdc ++历史上< ciso646> 没有定义任何宏,如 __ GLIBCXX __ 可以用来检测libstdc ++。这改变了GCC 6.1所以现在可以使用< ciso646> ,但是对于较旧的版本,你需要包括一个不同的头来检测libstdc ++。

For libc++ it is recommended to #include <ciso646> which serves no purpose in C++ and declares nothing, but for libc++ does define the _LIBCPP_VERSION macro. However, for libstdc++ historically <ciso646> did not define any macros such as __GLIBCXX__ that can be used to detect libstdc++. That changed with GCC 6.1 so <ciso646> can be used now, but for older releases you need to include a different header to detect libstdc++.

这篇关于如何在预处理器中检测-stdlib = libc ++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆