Is there anyway to get the calling function name from called function in c++ without modifying the code of calling function ?
-
I don't think that can be done, not at least in the Standard C++. – Nawaz Mar 21 '13 at 07:10
-
3See: http://stackoverflow.com/questions/353180/how-to-i-find-the-calling-function-name – Tushar Mar 21 '13 at 07:12
-
The code is built in debug mode. – asit_dhal Mar 21 '13 at 07:13
-
Have you considered the Debug Help Library? http://msdn.microsoft.com/en-gb/library/windows/desktop/ms679309(v=vs.85).aspx – Roger Rowland Mar 21 '13 at 07:23
-
1[Yes, it's possible](http://stackoverflow.com/a/15373451/179910). – Jerry Coffin Mar 21 '13 at 07:44
2 Answers
No there isn't. Once code is compiled, all function names and variable names are lost.
There may be debugging info that's kept for debugging purposes that could have that information. But it's not part of the code itself and cannot be accessed through standard C++ methods.

- 1,839
- 1
- 13
- 7
No, not in standard C++.
Though most compilers have special private functions to get the call-stack though, if you're lucky in a form that allows you to typecast the previous functions address to a callable. However if it's possible, then you have to be careful if the previous call was a member function or not, as member functions need an extra argument for this
. If you don't have a reference to that object, then you can't call the member function. And of course, you also have to know all the other arguments types and positions.
In short, it may be possible, but it will not be easy and it will most likely be dangerous.

- 400,186
- 35
- 402
- 621