c++中有许多预定义宏,这些宏在程序编译时由编译器自动定义,并可以在代码中使用。预定义宏通常用于调试、条件编译、文件信息、平台特定配置等方面。以下是一些常见的预定义宏及其具体使用方法和示例。
1. __file__
说明:__file__
是一个字符串宏,表示当前源代码文件的名称(包括路径)。
使用方法:通常用于调试或日志记录,以显示当前文件的名称。
示例:
#include <iostream> int main() { std::cout << "this code is in file: " << __file__ << std::endl; return 0; }
输出:
this code is in file: /path/to/your/sourcefile.cpp
2. __line__
说明:__line__
是一个整数宏,表示当前源代码的行号。
使用方法:常用于调试或错误报告,帮助开发者快速定位问题发生的源代码行号。
示例:
#include <iostream> int main() { std::cout << "this is line: " << __line__ << std::endl; return 0; }
输出:
this is line: 6
3. __date__
说明:__date__
是一个字符串宏,表示编译源代码时的日期(格式:mmm dd yyyy
,例如:sep 3 2024
)。
使用方法:可以用于生成代码的编译日期,常见于版本控制和构建信息。
示例:
#include <iostream> int main() { std::cout << "compiled on: " << __date__ << std::endl; return 0; }
输出:
compiled on: sep 3 2024
4. __time__
说明:__time__
是一个字符串宏,表示编译源代码时的时间(格式:hh:mm:ss
,例如:14:30:15
)。
使用方法:用于记录代码编译的时间,通常在日志或调试输出中使用。
示例:
#include <iostream> int main() { std::cout << "compiled at: " << __time__ << std::endl; return 0; }
输出:
compiled at: 14:30:15
5. __stdc__
说明:__stdc__
是一个宏,当代码是严格符合标准的 c 语言程序时(即不使用任何扩展),这个宏会被定义。对于 c++ 编译器,它表示是否遵循 c 标准。
使用方法:主要用于条件编译,确定是否使用标准 c 语言的一些特性。
示例:
#include <iostream> int main() { #ifdef __stdc__ std::cout << "the program is compiled with a standard c compiler!" << std::endl; #else std::cout << "the program is compiled with a non-standard compiler!" << std::endl; #endif return 0; }
输出:
the program is compiled with a standard c compiler!
6. __cplusplus
说明:__cplusplus
是一个宏,用于表示当前编译环境支持的 c++ 标准版本。它的值是一个整数,代表 c++ 标准的年份和修订版本。
常见值:
- 199711l:c++98。
- 201103l:c++11。
- 201402l:c++14。
- 201703l:c++17。
- 202002l:c++20。
使用方法:通常用于判断当前编译环境使用的 c++ 标准版本。
示例:
#include <iostream> int main() { #if __cplusplus == 199711l std::cout << "c++98 standard" << std::endl; #elif __cplusplus == 201103l std::cout << "c++11 standard" << std::endl; #elif __cplusplus == 201402l std::cout << "c++14 standard" << std::endl; #elif __cplusplus == 201703l std::cout << "c++17 standard" << std::endl; #elif __cplusplus == 202002l std::cout << "c++20 standard" << std::endl; #else std::cout << "unknown c++ standard" << std::endl; #endif return 0; }
输出:
c++17 standard
7. __gnuc__, __gnug__
说明:__gnuc__
和 __gnug__
是 gnu 编译器(gcc)所定义的宏。__gnuc__
表示 gcc 的主版本号,__gnug__
用于判断是否是 gnu c++ 编译器。
使用方法:通常用于平台特定的编译选项或特性,以判断是否在 gcc 编译器下编译代码。
示例:
#include <iostream> int main() { #ifdef __gnuc__ std::cout << "compiled with gcc, version " << __gnuc__ << "." << __gnuc_minor__ << std::endl; #endif return 0; }
输出:
compiled with gcc, version 10.2
8. __declspec (microsoft特定)
说明:__declspec
是 microsoft 编译器特有的一个关键字,用于指定函数、变量或类型的属性。例如,用于声明 dll 导入或导出符号。
使用方法:在 windows 编程中常用于定义函数的导入导出。
示例:
#include <iostream> __declspec(dllexport) void hello() { std::cout << "hello, world!" << std::endl; } int main() { hello(); return 0; }
说明:
__declspec(dllexport)
是 microsoft 的扩展,用来声明函数为 dll 的导出函数。
9. __va_args__
说明:__va_args__
是用于宏定义中的可变参数的宏,它代表宏调用时传递给宏的所有额外参数。
使用方法:常用于实现日志记录、调试信息等功能,可以支持可变参数宏。
示例:
#include <iostream> #define log(msg, ...) std::cout << "log: " << msg << __va_args__ << std::endl int main() { log("error: ", "file not found\n"); return 0; }
输出:
log: error: file not found
10. #ifdef / #ifndef / #endif
这些并非直接的预定义宏,但它们与预定义宏结合使用来控制条件编译:
示例:
#include <iostream> #define debug int main() { #ifdef debug std::cout << "debug mode is enabled." << std::endl; #else std::cout << "debug mode is disabled." << std::endl; #endif return 0; }
输出:
debug mode is enabled.
总结
- 这些预定义宏提供了关于文件、日期、时间、编译器、标准版本等的信息,并常用于调试、日志记录和条件编译。
- 它们在跨平台开发、库开发和调试中尤为重要,帮助开发者在不同环境下编写更具可移植性的代码。
到此这篇关于c++中预定义宏的文章就介绍到这了,更多相关c++ 预定义宏内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论