Ignoring or redefining GCC Standard Predefined Macros
Asked Answered
B

1

6

Is it possible to tell GCC to compile the source code, and ignore macros like __FILE__, __LINE__ , etc, etc, or redefine them to expand into let's say - an empty string?

Bridge answered 15/12, 2013 at 12:47 Comment(2)
really a bad idea, why would you want this so? A lot of code (including system headers) will depend on these to do the right thing. This is just looking for trouble.Neckar
Why would you want to do this? because: 1. C standard prohibits redefining/undefining them 2. Replacing __LINE__ with like 0 or -1 would look awkward (It's an integer value and you have to give something, unlike empty string for __FILE__) 3. Their primary purpose is to help debug (If you don't want them, why use them in the first place?). So it seems the only way is to re-write your code if you don't want them or better keep them!Prestissimo
J
5

As with any macro you can just use:

#undef __LINE__
#undef __FILE__

and then you can redefine them.

You can also pass -U macroname to undef a macro name and -D macroname=definition to define a macro name to the gcc options.

Note that, as indicated in another answer, undefining or redefining __LINE__ or __FILE__ in C invokes undefined behavior.

Joanejoanie answered 15/12, 2013 at 12:52 Comment(7)
I suspect doing so provokes UB. Please see my answer on this.Pyramidal
@Pyramidal but I think the relevant quote is this one: (c99, 6.10.8p4) "None of these macro names, nor the identifier defined, shall be the subject of a #define or a #undef preprocessing directive.Joanejoanie
Hm, yes, found this too .. but UB isn't mentioned in this context.Pyramidal
@Pyramidal violation of a shall requirement that is not a constraint in C invokes UB, see C99, 4.p2Joanejoanie
It would probably be better if it'd be a constraint violation. Implementing a check for that at compilation time should not be big deal.Neckar
@Pyramidal I see that the current C standard is C11. Is C99 accurate/up-to-date enough to be used in most cases? P.S. I don't want to spend $30 USD on the new standard.Diu
@BitFiddlingCodeMonkey: I doubt there are many essential changes (not additions) from C99 to C11 which aren't backward compatible. However this is an assumption of mine ... - Details: en.wikipedia.org/wiki/C11_%28C_standard_revision%29Pyramidal

© 2022 - 2024 — McMap. All rights reserved.