如何在编译时测试libstdc++的版本,而不是GCC

How do I test the version of libstdc++, not GCC, at compile time?

本文关键字:版本 GCC libstdc++ 编译 测试      更新时间:2023-10-16

我正在尝试测试libstdc++的版本,因为std::regex在4.9.0版本之前与GCC一起分发的libstdc++版本中实现了,但很大程度上是破碎的。

注意:

  • 我需要测试 libstdc++的版本,而不是GCC,因为Clang也支持使用libstdc++作为标准库。这就排除了测试__GNUC_PATCHLEVEL____GNUC____GNUC_MINOR__宏的可能性。

  • __GLIBCXX__是一个日期,而不是版本号,并且不单调递增。例如,GCC 4.8.4附带了#define __GLIBCXX__ 20150426,它比GCC 4.9.0的发布日期更新。

是否有任何可移植的方法来测试libstdc++的版本,不依赖于使用GCC我的编译器?

我认为这个问题很小,可以用蛮力解决。

在名为machine.hpp或类似的头文件中,我会测试c++标准的版本至少是我需要的(__cplusplus宏)。然后,我将添加各种宏检查,以拒绝任何我知道有缺陷的库。

换句话说,我将采用黑名单方法而不是白名单方法。例如:

#pragma once
#ifndef MACHINE_HPP_HEADER_GUARDS
#define MACHINE_HPP_HEADER_GUARDS
#if __cplusplus < 201103L
// Library is incompatible if it does not implement at least the C++11
// standard.
#error "I need a library that supports at least C++11."
#else
// Load an arbitrary header to ensure that the pre-processor macro is
// defined. Otherwise you will need to load this header AFTER you
// #include the header you care about.
#include <iosfwd>
#endif
#if __GLIBCXX__ == 20150422
#error "This version of GLIBCXX (20150422) has flaws in it"
#endif
// ...repeat for other versions of GLIBCXX that you know to be flawed
#endif // MACHINE_HPP_HEADER_GUARDS