19

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?

derekerdmann
  • 17,696
  • 11
  • 76
  • 110
sivabudh
  • 31,807
  • 63
  • 162
  • 228
  • For people who might want to know more about these macros: http://gcc.gnu.org/onlinedocs/gcc-3.4.0/gcc/Function-Names.html and http://gcc.gnu.org/onlinedocs/cpp/Standard-Predefined-Macros.html – sivabudh Dec 13 '10 at 22:55

8 Answers8

31

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

Leo Davidson
  • 6,093
  • 1
  • 27
  • 29
9

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

__PRETTY_FUNCTION__ is a gcc feature.

aschepler
  • 70,891
  • 9
  • 107
  • 161
7

For more portability in getting the current function name, you can try BOOST_CURRENT_FUNCTION.

Fred Larson
  • 60,987
  • 18
  • 112
  • 174
6

Yes Visual C++ has them or an equivalent. See the responses here:

What's the difference between __PRETTY_FUNCTION__, __FUNCTION__, __func__? function-func/4384860#4384860

Also note that despite the upper case used, they aren't macros. They're variables.

Community
  • 1
  • 1
sashang
  • 11,704
  • 6
  • 44
  • 58
  • 1
    They _are_ macro's. Just check the title of their MSDN page: [Predefined Macros](http://msdn.microsoft.com/nl-nl/library/b0084kay.aspx) – MSalters Dec 14 '10 at 09:44
  • @MSalters: hmmm ... according to the [gcc docs](http://gcc.gnu.org/onlinedocs/gcc-4.5.0/gcc/Function-Names.html) they're variables. Looks like VC++ implements it differently. – sashang Dec 14 '10 at 10:18
3

I know that MSVC offers __FILE__ and __LINE__, both of which are Standard macros. They also offer __FUNCTION__, which I believe is what you're looking for,

Puppy
  • 144,682
  • 38
  • 256
  • 465
3

__FILE__ and __LINE__ are standard since C89. __PRETTY_FUNCTION__ is a GCCism. __func__ is a C99ism which (unlike GCCisms) may well be available in Visual C++; it is not precisely the same as __PRETTY_FUNCTION__ but it may be close enough for your purposes.

zwol
  • 135,547
  • 38
  • 252
  • 361
1
  1. Yes, Microsoft Visual Studio has __FILE__ and __LINE__. Here are more MSVC macros.

  2. Both are ANSI C++.

  3. MSVC has __FUNCTION__, which is Microsoft-specific.

EboMike
  • 76,846
  • 14
  • 164
  • 167
0

Using with constexpr you can use this: WHERE macro.

Based on usage of:

#include "string/ConstexprString.hpp"

#define S1(x) #x
#define S2(x) S1(x)

// WHERE - const char* const should be used as temporary value
#define WHERE (string::make(__PRETTY_FUNCTION__) + ":" + string::make(S2(__LINE__))).get()

// It is safe to store e.g. `constexpr auto where = WHERE_STR;`
#define WHERE_STR (string::make(__PRETTY_FUNCTION__) + ":" + string::make(S2(__LINE__)))

Example usage:

    // Called: void (anonymous namespace)::exampleUseCaseWhere(int):18
    std::cout << "Called: " << WHERE << std::endl;

Full & running example here

See:

Gelldur
  • 11,187
  • 7
  • 57
  • 68