GCC 编译器为我提供了以下宏:
__FILE__
这样我就可以打印出文件名+目录了。__LINE__
这样我就可以打印出我正在打印的行号。__PRETTY_FUNCTION__
这样我就可以打印出漂亮的函数名了
Visual C++ 是否有这些宏的等价物?附带的问题是,这些是 C++ 编译器的标准吗?
最佳答案
在 VS2008 中,这个:
struct A
{
bool Test(int iDummyArg)
{
const char *szFile = __FILE__;
int iLine = __LINE__;
const char *szFunc = __FUNCTION__; // Func name
const char *szFunD = __FUNCDNAME__; // Decorated
const char *szFunS = __FUNCSIG__; // Signature
printf("%s\n", szFile);
printf("%d\n", iLine);
printf("%s\n", szFunc);
printf("%s\n", szFunD);
printf("%s\n", szFunS);
return true;
}
};
int wmain(int argc, TCHAR *lpszArgv[])
{
A a;
a.Test(10);
}
将打印这个:
c:\source\test_projects\blah\blah.cpp
14
A::Test
?Test@A@@QAE_NH@Z
bool __thiscall A::Test(int)
(行号是“错误的”,因为我的文件顶部确实有一些额外的东西。)
关于c++ - __FILE__ 、 __LINE__ 和 __PRETTY_FUNCTION__ 的 Visual C++ 等效项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4434282/