[英]How do I test the version of libstdc++, not GCC, at compile time?
我正在嘗試測試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的發布日期更新。
是否有任何可移植的方法來測試不依賴於使用GCC我的編譯器的libstdc++
版本?
在我看來,問題足夠小,無法用蠻力解決。
在名為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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.