Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I imitate the Microsoft version of __FUNCTION__ using gcc?

When I use the __FUNCTION__ macro/variable to print out debugging information, there seems to be a difference in what it outputs when using the Microsoft C++ compiler and gcc. For example, using the following trivial code:

class Foo 
{
    public:
       void Bar(int a, int b, int c)
       {
           printf ("__FUNCTION__ = %s\n", __FUNCTION__);
       }
};

int main (void)
{
    Foo MyFoo;

    MyFoo.Bar();

    return 0;
}

Using the Microsoft Visual C++ compiler, I get

__FUNCTION__ = Foo::Bar

whereas when compiling using gcc (in this case on the Mac), I get

__FUNCTION__ = Bar

The second example is not ideal because I quite often have several classes with, say, Init() and Uninit() methods and in a debug output trace its virtually impossible to tell which one of these has been called as the class name will be missing. Now, I know you can use the __PRETTY_FUNCTION__ in place of __FUNCTION__ to get something like

__PRETTY_FUNCTION__ = void Foo::Bar(int, int, int)

Which is fine, but its a bit too verbose for what I need and gets a bit long for functions with a lot of parameters.

So my question is (at last), is there any way to get the output to look like simply Foo::Bar using gcc, as in the example above?

like image 586
binarybob Avatar asked Oct 04 '11 15:10

binarybob


People also ask

What is __ function __ in C++?

(C++11) The predefined identifier __func__ is implicitly defined as a string that contains the unqualified and unadorned name of the enclosing function. __func__ is mandated by the C++ standard and is not a Microsoft extension.

Can you use GCC on Windows?

Install GCC on Windows To install GCC, click the GCC and G++ package to mark GNU C and C++ compiler for installation. To complete the process, select Apply Changes from the Installation menu in the top-left corner of the mingw-get window.

What is_ msc_ ver?

_MSC_VER Defined as an integer literal that encodes the major and minor number elements of the compiler's version number. The major number is the first element of the period-delimited version number and the minor number is the second element. For example, if the version number of the Microsoft C/C++ compiler is 17.00.

What is G ++ compiler?

GNU C++ Compiler ( g++ ) is a compiler in Linux which is used to compile C++ programs. It compiles both files with extension . c and . cpp as C++ files. The following is the compiler command to compile C++ program.


2 Answers

If you are using it for tracing, you can always use typeid(T).name() and just conditionally compile per platform. Certainly not as convenient as the macro, but it could work.

Vaguely similar to __CLASS__ macro in C++

like image 182
Tom Kerr Avatar answered Oct 26 '22 22:10

Tom Kerr


The function-name sanctioned by the standard is defined as follows:

static const char __func__[] = "function-name ";

Example:

#include <iostream>

namespace meh {
    void foobar() { std::cout << __func__ << std::endl; }
};

struct Frob {
    void foobar() { std::cout << __func__ << std::endl; }
    static void barfoo() { std::cout << __func__ << std::endl; }
};

int main () {
    std::cout << __func__ << std::endl;
    meh::foobar();
    Frob().foobar();
    Frob::barfoo();
}

However, output with g++:

main
foobar
foobar
barfoo

However, that is valid C++ behaviour:

§ 8.4.1, 8: The function-local predefined variable __func__ is defined as if a definition of the form static const char __func__[] = "function-name "; had been provided, where function-name is an implementation-defined string. It is unspecified whether such a variable has an address distinct from that of any other object in the program

I.e., you may not trust in its value. If you want to use non-portable extensions, have a look at a similar question: What's the difference between __PRETTY_FUNCTION__, __FUNCTION__, __func__? .

like image 38
Sebastian Mach Avatar answered Oct 26 '22 23:10

Sebastian Mach