c++预编译宏
1.预编译宏概念
一段微软官方文字:
The Visual C++ compiler predefines certain preprocessor macros, depending on the language (C or C++), the compilation target, and the chosen compiler options.
VC++编译器预先定义某些预处理器宏,其具体取决于语言,编译目标和所选的编辑器选项
预编译宏一般以"__
“结尾并且以”__
“结尾,中间的单词全部大写,如果由多个单词组成,则单词之间使用”_
"连接。
2.__FILE__
__FILE__
:显示当前源文件的名称。
当这个宏放在函数中和放在宏中所产生的会是不一样的结果。
头文件:
#include<iostream>
using namespace std;
#define HR(){cout<<__FILE__<<":in #define"<<endl;}
void TestFile();
源文件
#include"Test.h"
void TestFile() {
cout << __FILE__<<":in function"<<endl;
}
测试文件
int main()
{
TestFile();
HR();
return 0;
}
测试结果
结论:宏中的__FILE__
表示调用的文件名,函数中则表示实现的文件名
3.__LINE__
__LINE__
:显示当前文件执行的行
当这个宏放在函数中和放在宏中所产生的会是不一样的结果。
可以参考__FILE__
的用法,接下来给出三个文件
#include<iostream>
using namespace std;
#define HR(){cout<<__LINE__<<":in #define"<<endl;}
void TestFile();
#include"Test.h"
void TestFile() {
cout << __LINE__<<":in function"<<endl;
}
int main()
{
TestFile();
HR();
return 0;
}
结论:宏中的__LINE__
表示调用的文件名,函数中则表示实现的文件名