网站首页 > 博客文章 正文
在 C++ 中,除了标准预定义宏和编译器特定宏外,还有一些常见的预设变量和宏用于特定的编译环境和平台检测:
标准预定义宏
__FILE__:表示当前文件名的字符串字面量。
__LINE__:表示当前行号的整数。
__DATE__:表示编译日期的字符串字面量(格式如 "Sep 29 2024")。
__TIME__:表示编译时间的字符串字面量(格式如 "16:30:19")。
__TIMESTAMP__:表示源文件最后一次修改的日期和时间的字符串字面量。
__cplusplus:表示当前 C++ 标准的版本号。例如,199711L 表示 C++98,201103L 表示 C++11,201402L 表示 C++14,201703L 表示 C++17,202002L 表示 C++20。
__STDC__:如果编译器遵循 ANSI C 标准,则定义此宏。
__STDC_HOSTED__:如果编译环境是一个完整的宿主环境,则定义为 1,否则为 0。
__STDC_VERSION__:表示 C 标准版本号(如 C11 为 201112L)。
编译器特定宏(扩展)
GCC/Clang
__GNUC__:GCC 版本号。
__GNUG__:G++ 版本号。
__clang__:Clang 编译器标识。
__VERSION__:表示编译器版本信息的字符串。
__OPTIMIZE__:如果启用了优化选项,则定义此宏。
__unix__ / __unix:标识 Unix 系统。
__APPLE__:标识 Apple 平台。
__MACH__:标识 macOS 系统。
MSVC
_MSC_VER:微软编译器版本号。
_WIN32:标识 Windows 平台。
_WIN64:标识 64 位 Windows 平台。
_DEBUG:在调试模式下定义。
_MT:多线程编译时定义。
_DLL:使用 DLL 运行时库时定义。
平台和架构检测宏
__x86_64__ / _M_X64:标识 x86_64 架构。
__i386__ / _M_IX86:标识 x86 架构。
__arm__ / _M_ARM:标识 ARM 架构。
C++ 标准库相关宏
__cpp_exceptions:如果支持异常处理,则定义此宏。
__cpp_rtti:如果支持运行时类型识别(RTTI),则定义此宏。
条件编译使用的宏
可以通过 #define 和 #undef 自定义宏,用于条件编译。
这些宏和变量帮助开发者在不同的编译器和平台上编写可移植的代码,通过条件编译来适应不同的编译环境和配置。
示例用法:
以下是这些预处理宏常量的详细用法介绍:
__FILE__ 和 __LINE__
用途:用于调试和日志记录,帮助定位代码中的位置。
示例:
#include <iostream>
void logError(const char* message) {
std::cout << "Error: " << message << " in file " << __FILE__ << " at line " << __LINE__ << std::endl;
}
int main() {
logError("Something went wrong");
return 0;
}
__DATE__ 和 __TIME__
用途:用于生成编译时信息,帮助识别编译的时间点。
示例:
#include <iostream>
int main() {
std::cout << "Compiled on " << __DATE__ << " at " << __TIME__ << std::endl;
return 0;
}
__TIMESTAMP__
用途:提供源文件最后修改的日期和时间,可以用于版本控制和调试。
注意:并非所有编译器都支持。
示例:
#include <iostream>
int main() {
std::cout << "Last modified: " << __TIMESTAMP__ << std::endl;
return 0;
}
__cplusplus
用途:用于检测当前 C++ 标准版本,以便根据版本调整代码。
示例:
#include <iostream>
int main() {
std::cout << "C++ Standard version: " << __cplusplus << std::endl;
return 0;
}
__STDC__
用途:用于检查编译器是否遵循 ANSI C 标准。
示例:
#include <stdio.h>
int main() {
#ifdef __STDC__
printf("ANSI C compliant\n");
#else
printf("Non-ANSI C compliant\n");
#endif
return 0;
}
__STDC_HOSTED__
用途:用于检查编译环境是否是完整的宿主环境。
示例:
#include <stdio.h>
int main() {
#if __STDC_HOSTED__ == 1
printf("Hosted environment\n");
#else
printf("Freestanding environment\n");
#endif
return 0;
}
__STDC_VERSION__
用途:用于检测当前 C 标准版本。
示例:
#include <stdio.h>
int main() {
#ifdef __STDC_VERSION__
printf("C Standard version: %ld\n", __STDC_VERSION__);
#else
printf("C89 or earlier\n");
#endif
return 0;
}
__GNUC__ 和 __GNUG__
用途:用于检测 GCC 和 G++ 的版本号,以便根据不同的编译器版本调整代码。
示例:
#include <iostream>
int main() {
#ifdef __GNUC__
std::cout << "GCC version: " << __GNUC__ << std::endl;
#endif
#ifdef __GNUG__
std::cout << "G++ version: " << __GNUG__ << std::endl;
#endif
return 0;
}
__clang__
用途:用于检测是否使用 Clang 编译器。
示例:
#include <iostream>
int main() {
#ifdef __clang__
std::cout << "Compiled with Clang" << std::endl;
#else
std::cout << "Not compiled with Clang" << std::endl;
#endif
return 0;
}
__VERSION__
用途:用于获取编译器的版本信息。
示例:
#include <iostream>
int main() {
std::cout << "Compiler version: " << __VERSION__ << std::endl;
return 0;
}
__OPTIMIZE__
用途:用于检测是否启用了编译器优化选项。
示例:
#include <iostream>
int main() {
#ifdef __OPTIMIZE__
std::cout << "Optimizations are enabled" << std::endl;
#else
std::cout << "Optimizations are not enabled" << std::endl;
#endif
return 0;
}
__unix__ / __unix
用途:用于检测 Unix 系统。
示例:
#include <iostream>
int main() {
#if defined(__unix__) || defined(__unix)
std::cout << "Running on Unix" << std::endl;
#else
std::cout << "Not running on Unix" << std::endl;
#endif
return 0;
}
__APPLE__
用途:用于检测 Apple 平台。
示例:
#include <iostream>
int main() {
#ifdef __APPLE__
std::cout << "Running on Apple platform" << std::endl;
#else
std::cout << "Not running on Apple platform" << std::endl;
#endif
return 0;
}
__MACH__
用途:用于检测 macOS 系统。
示例:
#include <iostream>
int main() {
#if defined(__APPLE__) && defined(__MACH__)
std::cout << "Running on macOS" << std::endl;
#else
std::cout << "Not running on macOS" << std::endl;
#endif
return 0;
}
_MSC_VER
用途:用于检测 Microsoft 编译器的版本号。
示例:
#include <iostream>
int main() {
#ifdef _MSC_VER
std::cout << "Microsoft compiler version: " << _MSC_VER << std::endl;
#else
std::cout << "Not using Microsoft compiler" << std::endl;
#endif
return 0;
}
_WIN32
用途:用于检测 Windows 平台(包括 32 位和 64 位)。
示例:
#include <iostream>
int main() {
#ifdef _WIN32
std::cout << "Running on Windows" << std::endl;
#else
std::cout << "Not running on Windows" << std::endl;
#endif
return 0;
}
_WIN64
用途:用于检测 64 位 Windows 平台。
示例:
#include <iostream>
int main() {
#ifdef _WIN64
std::cout << "Running on 64-bit Windows" << std::endl;
#else
std::cout << "Not running on 64-bit Windows" << std::endl;
#endif
return 0;
}
_DEBUG
用途:在调试模式下编译时定义,用于启用调试相关的代码。
示例:
#include <iostream>
int main() {
#ifdef _DEBUG
std::cout << "Debug mode is enabled" << std::endl;
#else
std::cout << "Debug mode is not enabled" << std::endl;
#endif
return 0;
}
_MT
用途:用于检测多线程编译。
示例:
#include <iostream>
int main() {
#ifdef _MT
std::cout << "Compiled with multithreading support" << std::endl;
#else
std::cout << "Compiled without multithreading support" << std::endl;
#endif
return 0;
}
_DLL
用途:用于检测是否使用 DLL 运行时库。
示例:
#include <iostream>
int main() {
#ifdef _DLL
std::cout << "Using DLL runtime library" << std::endl;
#else
std::cout << "Not using DLL runtime library" << std::endl;
#endif
return 0;
}
__x86_64__ / _M_X64
用途:用于检测 x86_64 架构。
示例:
#include <iostream>
int main() {
#if defined(__x86_64__) || defined(_M_X64)
std::cout << "Running on x86_64 architecture" << std::endl;
#else
std::cout << "Not running on x86_64 architecture" << std::endl;
#endif
return 0;
}
__i386__ / _M_IX86
用途:用于检测 x86 架构(32 位)。
示例:
#include <iostream>
int main() {
#if defined(__i386__) || defined(_M_IX86)
std::cout << "Running on x86 architecture" << std::endl;
#else
std::cout << "Not running on x86 architecture" << std::endl;
#endif
return 0;
}
__arm__ / _M_ARM
用途:用于检测 ARM 架构。
示例:
#include <iostream>
int main() {
#if defined(__arm__) || defined(_M_ARM)
std::cout << "Running on ARM architecture" << std::endl;
#else
std::cout << "Not running on ARM architecture" << std::endl;
#endif
return 0;
}
__cpp_exceptions
用途:用于检测编译器是否支持异常处理。
说明:如果异常处理被启用,编译器会定义此宏,通常其值为启用该特性的年份。
示例:
#include <iostream>
int main() {
#ifdef __cpp_exceptions
std::cout << "Exceptions are supported" << std::endl;
#else
std::cout << "Exceptions are not supported" << std::endl;
#endif
return 0;
}
__cpp_rtti
用途:用于检测编译器是否支持运行时类型识别(RTTI)。
说明:如果 RTTI 被启用,编译器会定义此宏,通常其值为启用该特性的年份。
示例:
#include <iostream>
#include <typeinfo>
class Base {};
class Derived : public Base {};
int main() {
#ifdef __cpp_rtti
Base* b = new Derived();
std::cout << "RTTI is supported: " << typeid(*b).name() << std::endl;
#else
std::cout << "RTTI is not supported" << std::endl;
#endif
return 0;
}
这两个宏可以帮助开发者在编写跨平台或需要特定功能
猜你喜欢
- 2024-10-02 以轻便和强性能,颠覆AI创作!XPS 14值得入手
- 2024-10-02 C++用于Linux内核开发,曾被Linus强烈反对,现在时机终成熟?
- 2024-10-02 谷歌安卓14为平板引入新特性,增强笔记体验
- 2024-10-02 【紫微斗数】排盘技巧004之十四主星的基本特性与影响
- 2024-10-02 曾遭 Linus 炮轰“很烂”的 C++,现受开发者支持:Linux 内核应从 C 转到 C++!
- 2024-10-02 C++类型转换全解析:安全与效率的平衡
- 2024-10-02 iPhone 15脱胎换骨!14系列上的9大特性在15上会消失
- 2024-10-02 ColorOS 14正式版今日起推送,OPPO Find N2/ 一加11等首发
- 2024-10-02 首发酷睿Ultra平台!华硕灵耀14 2024 AI超轻薄本上手体验
- 2024-10-02 苹果iOS 17、macOS 14新特性:可在任意文本框内自动填充内容
你 发表评论:
欢迎- 07-08Google Cloud Platform 加入支持 Docker 的容器引擎
- 07-08日本KDDI与Google Cloud 签署合作备忘录,共探AI未来
- 07-08美国Infoblox与Google Cloud合作推出云原生网络和安全解决方案
- 07-08GoogleCloud为Spanner数据库引入HDD层,将冷存储成本降低80%
- 07-08谷歌推出Cloud Dataproc,缩短集群启动时间
- 07-08Infovista与Google Cloud携手推进射频网络规划革新
- 07-08比利时Odoo与Google Cloud建立增强合作,扩大全球影响力
- 07-08BT 和 Google Cloud 通过 Global Fabric 加速 AI 网络
- 最近发表
-
- Google Cloud Platform 加入支持 Docker 的容器引擎
- 日本KDDI与Google Cloud 签署合作备忘录,共探AI未来
- 美国Infoblox与Google Cloud合作推出云原生网络和安全解决方案
- GoogleCloud为Spanner数据库引入HDD层,将冷存储成本降低80%
- 谷歌推出Cloud Dataproc,缩短集群启动时间
- Infovista与Google Cloud携手推进射频网络规划革新
- 比利时Odoo与Google Cloud建立增强合作,扩大全球影响力
- BT 和 Google Cloud 通过 Global Fabric 加速 AI 网络
- NCSA和Google Cloud合作开发AI驱动的网络防御系统,加强泰国网络空间的安全性
- SAP将在沙特阿拉伯 Google Cloud 上推出BTP服务
- 标签列表
-
- ifneq (61)
- 字符串长度在线 (61)
- googlecloud (64)
- messagesource (56)
- promise.race (63)
- 2019cad序列号和密钥激活码 (62)
- window.performance (66)
- qt删除文件夹 (72)
- mysqlcaching_sha2_password (64)
- ubuntu升级gcc (58)
- nacos启动失败 (64)
- ssh-add (70)
- jwt漏洞 (58)
- macos14下载 (58)
- yarnnode (62)
- abstractqueuedsynchronizer (64)
- source~/.bashrc没有那个文件或目录 (65)
- springboot整合activiti工作流 (70)
- jmeter插件下载 (61)
- 抓包分析 (60)
- idea创建mavenweb项目 (65)
- vue回到顶部 (57)
- qcombobox样式表 (68)
- tomcatundertow (58)
- pastemac (61)
本文暂时没有评论,来添加一个吧(●'◡'●)