GCC compiler gives me the following macros:
__FILE__
so that I can print out the file name + directory.__LINE__
so that I can print out the line number of where I'm printing from.__PRETTY_FUNCTION__
so that I can print out the pretty function nameDoes Visual C++ have the equivalent of these macros? A side question is, are these standard for C++ compilers?
__FILE__ This macro expands to the name of the current input file, in the form of a C string constant. This is the path by which the preprocessor opened the file, not the short name specified in ' #include ' or as the input file name argument. For example, "/usr/local/include/myheader.
__LINE__ is a preprocessor macro that expands to current line number in the source file, as an integer. __LINE__ is useful when generating log statements, error messages intended for programmers, when throwing exceptions, or when writing debugging code.
The identifier __PRETTY_FUNCTION__ holds the name of the function pretty printed in a language specific fashion. These names are always the same in a C function, but in a C++ function they may be different. For example, this program: extern "C" { extern int printf (char *, ...
The __FILE__ macro expands to a string whose contents are the filename, surrounded by double quotation marks ( " " ). If you change the line number and filename, the compiler ignores the previous values and continues processing with the new values. The #line directive is typically used by program generators.
In VS2008, this:
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);
}
will print this:
c:\source\test_projects\blah\blah.cpp
14
A::Test
?Test@A@@QAE_NH@Z
bool __thiscall A::Test(int)
(The line number is "wrong" since there was really some extra stuff at the top of my file.)
__FILE__
and __LINE__
are standard, and I'm rather certain Microsoft compilers have essentially always had them.
__PRETTY_FUNCTION__
is a gcc feature.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With