Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual C++ equivalent of __FILE__ , __LINE__ and __PRETTY_FUNCTION__

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 name

Does Visual C++ have the equivalent of these macros? A side question is, are these standard for C++ compilers?

like image 861
sivabudh Avatar asked Dec 13 '10 22:12

sivabudh


People also ask

What does __ file __ mean in C?

__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.

What is __ LINE __ in C?

__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.

What is __ Pretty_function __ C++?

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 *, ...

What is __ file __ in C++?

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.


2 Answers

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.)

like image 193
Leo Davidson Avatar answered Oct 15 '22 20:10

Leo Davidson


__FILE__ and __LINE__ are standard, and I'm rather certain Microsoft compilers have essentially always had them.

__PRETTY_FUNCTION__ is a gcc feature.

like image 43
aschepler Avatar answered Oct 15 '22 21:10

aschepler