2

I would like to add the following code in the first line of every function defenition

printf(__FUNCTION__);

how to do this using the preprocessor?

AstroCB
  • 12,337
  • 20
  • 57
  • 73
Dinesh
  • 1,825
  • 5
  • 31
  • 40
  • 5
    You're not going to be able to do this, without replacing every function definition with some macro. At that point, you might as well just insert that code yourself, or realize that it's a *horrible* idea, and not do it. – Jonathon Reinhart Dec 16 '13 at 06:59
  • 2
    Use a debugger instead and step through the code, it will be much easier. – Jeff Dec 16 '13 at 06:59
  • 1
    If you persist with this, you probably need to ensure there's a newline at the end, which you could do with `printf("%s\n", __func__)` (to use the C99 standard notation) or `puts(__func__)`. This avoids using a GCC specific construct. Were it me, I'd make it into a function call that can be turned on or off as needed. Note that the preprocessor cannot do this for you; it has no clue about where function boundaries are. So you'll have to write the macros in the correct place yourself. – Jonathan Leffler Dec 16 '13 at 07:19
  • 2
    Why the preprocessor? What do you want to achieve? If it is just to achieve tracing of your functions and you are using gcc anyhow, it has extensions to instrument functions. – Jens Gustedt Dec 16 '13 at 07:43
  • 2
    If you're using GCC, you may be able to use `-finstrument-functions` along with a map file lookup to do you want (http://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html). – Michael Burr Dec 16 '13 at 07:48
  • Pre-processor alone cannot achieve this; pre-processor plus a lot of manual edits can. There are [compiler-specific](http://stackoverflow.com/questions/1472769/is-there-a-compiler-feature-to-inject-custom-function-entry-and-exit-code) solutions. [Functors](http://stackoverflow.com/questions/5081123/how-to-add-code-at-the-entry-of-every-function) can also be used. – Petr Vepřek May 10 '14 at 20:45

0 Answers0