C++, get the name of the function

34,250

Solution 1

If you just want to log the current function name, most of the compilers have __FUNCTION__ macro, which will give you the current function name at compile time.

You may also look for stack walking techniques (here is an example for Windows), which can provide you more information about the current call stack and function names at runtime.

Solution 2

C++, get the name of the calling function via a pointer:

Option 1: roll your own function name recorder

If you want to resolve a "pointer to a function" to a "function name", you will need to create your own lookup table of all available functions, then compare your pointer address to the key in the lookup table, and return the name.

An implementation described here: https://stackoverflow.com/a/8752173/445131

Option 2: Use __func__

GCC provides this magic variable that holds the name of the current function, as a string. It is part of the C99 standard:

#include <iostream>
using namespace std;
void foobar_function(){
    cout << "the name of this function is: " << __func__ << endl;
}
int main(int argc, char** argv) {
    cout << "the name of this function is: " << __func__ << endl;
    foobar_function();
    return 0;
}

Output:

the name of this function is: main
the name of this function is: foobar_function

Notes:

__FUNCTION__ is another name for __func__. Older versions of GCC recognize only this name. However, it is not standardized. If maximum portability is required, we recommend you use __func__, but provide a fallback definition with the preprocessor, to define it if it is undefined:

 #if __STDC_VERSION__ < 199901L
 # if __GNUC__ >= 2
 #  define __func__ __FUNCTION__
 # else
 #  define __func__ "<unknown>"
 # endif
 #endif

Source: http://gcc.gnu.org/onlinedocs/gcc/Function-Names.html

Solution 3

There's no way you can get the name of the function. Just because it doesn't reside inside the executable. It vanishes completely after your code is compiled & linked.

You may try renaming your functions/variables, and your executable will be the same (apart from mutable things the compiler may put, like build date/time, debug information ID, etc.)

Also try to open the executable file with some editor and look for the function name. Most likely you'll not find it.

You may however put some programmatic "decorations" that'll help you discover your function name at the runtime.

Solution 4

You cannot get the name of the function in C++, but you can print the pointer and later check the binary (if not stripped) for the function name. The signature can be printed exactly as you are doing, just that the type name is not really 'human readable'. Check your compiler documentation for what the output of your code means. In g++ the output will be PFvfE, which I don't understand completely, but identifies a pointer (P) to a function (F) returning void (v) and taking a float (f) as single argument. Don't ask me what the E is...

(I don't have time to check the docs now, I just played with a sample program to guess that: print different function signatures)

Share:
34,250
Hanut
Author by

Hanut

Updated on July 09, 2020

Comments

  • Hanut
    Hanut almost 4 years

    In C++, is there a way to get the function signature/name from it's pointer like this?

    void test(float data) {}
    cout << typeid(&test).name();
    

    I want to use this data for logging.

  • stefan
    stefan almost 11 years
    Isn't it __func__? gcc.gnu.org/onlinedocs/cpp/Standard-Predefined-Macros.html states that this is technically not a macro.
  • Griwes
    Griwes almost 11 years
    __func__ is a standard one and it's required to be there; __FUNCTION__ is probably some non-standard one. __PRETTY_FUNCTION__ is worth mentioning, too.
  • Jayhello
    Jayhello almost 6 years
    Thank you, The method 2 can also be used in C++11(GCC), I am using ubuntu16.
  • FalcoGer
    FalcoGer almost 4 years
    this is partly incorrect. Function names do reside in the executable/library if they're public. Otherwise they couldn't be called externally. Those names are called 'symbols'
  • FalcoGer
    FalcoGer almost 4 years
    Is there also a way to get the signature? (i.e. int main (int, char**) or similar?)