我用这样的代码一次:
std::string true_cxx =
#ifdef __clang__
"clang++";
#else
"g++";
#endif
std::string true_cxx_ver =
#ifdef __clang__
ver_string(__clang_major__, __clang_minor__, __clang_patchlevel__);
#else
ver_string(__GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__);
#endif
其中ver_string
定义:
std::string ver_string(int a, int b, int c) {
std::ostringstream ss;
ss << a << '.' << b << '.' << c;
return ss.str();
}
还有另一个非常有用的宏(在gcc和铛)本:
__VERSION__
This macro expands to a string constant which describes the version of the compiler in use. You should not rely on its contents having any particular form, but it can be counted on to contain at least the release number.
见gcc online docs。
如果你需要处理MSVC和其他可能性,你将不得不检查他们使用的宏,我不记得他们手头。
有没有标准化的方式来获得在不同的编译器的信息。 –