2

In my class work, I've done this successfully, but only in very simple programs. Today, I had a more complex program, and Eclipse did horrible borky things after I defined DEBUG as a symbol, and these horrible things did not go away after I removed the definition. I've been told by another source that the symbol DEBUG is in used by the gcc compiler, and that I am interfering with a standard library by defining it myself.

Is it okay to use #define DEBUG myself? Or not?

Lord Dust
  • 157
  • 9

5 Answers5

5

Yes, it is perfectly ok to define the symbol DEBUG yourself. But it is not ok to define the symbol _DEBUG (note the leading underscore): symbols beginning with an underscore and a capital letter, or two underscores, are reserved for use by the implementation (that is, the compiler and toolchain). Since they are reserved, you should not define them yourself, but you can certainly test their existence. See §7.1.3 of the C99 standard.

Another symbol to be aware of is NDEBUG, which you are free to define or not to define, but if you do define it before the inclusion of <assert.h>, it causes all assert statements to be removed.

Adam Rosenfield
  • 390,455
  • 97
  • 512
  • 589
  • Can you point me to some kind of search or keyword I can use to find this implementation-specific stuff? I've been trying to find out for a while which symbols Eclipse uses, but my Google-fu doesn't seem to find the right words. – Lord Dust Feb 24 '11 at 05:39
2

If removing the symbol didn't solve your problem, then the symbol didn't cause your problem.

I've never heard of DEBUG as a symbol, but _DEBUG is used by Visual Studio. As an aside, NDEBUG is used by <assert.h> to turn-off assert statements.

Community
  • 1
  • 1
chrisaycock
  • 36,470
  • 14
  • 88
  • 125
1

DEBUG is a little generic to be used for that sort of thing, simply because every one of the untold millions of code monkeys on the planet will also have the same idea.

You'd be better off using something that's unlikely to clash, like PAX_DEBUG or YOURCOMPANYNAME_BTREEMODULE_DEBUG or something like that.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
0

It may be possible, but you stop trying to find out, it is certainly bad practice.

Paul
  • 544
  • 4
  • 12
  • If it broke your code, then it's clear why. If you are asking where DEBUG is used in standard libraries or compilers, that's a different question. – jmilloy Feb 24 '11 at 05:30
  • Because the word `DEBUG` is so common that it's almost guaranteed to collide with another header/api at some point in the namespace. – jmq Feb 24 '11 at 05:31
0

This is why "newer" languages have namespaces. Don't use that. In our code we would use _DEBUG instead (or you could use _DEBUG_ or __DEBUG__, some people don't like using the single underscore on the front).

EDIT: I was mistaken about using _DEBUG (we don't use that). We use __DEBUG__ (two underscores on the front and back).

jmq
  • 10,110
  • 16
  • 58
  • 71
  • 2
    You probably shouldn't begin the name with a double underscore. – Cody Gray - on strike Feb 24 '11 at 05:30
  • I just looked it up in our code and we're using double underscores on the front AND back (not the single on the front). – jmq Feb 24 '11 at 05:34
  • Names beginning with an underscore and a capital letter, or two underscores, are reserved for use by the implementation; if a program defines a reserved identifier, the behavior is undefined (C99 §7.1.3). – Adam Rosenfield Feb 24 '11 at 05:35
  • 1
    The reason some people don't like using it is because it's reserved :-) So are the double-underscore ones. – paxdiablo Feb 24 '11 at 05:36
  • Thank you for the reference. I need to check that out. It has never caused a problem in our code, but I will need to evaluate that. – jmq Feb 24 '11 at 05:37
  • 1
    It may not cause a problem in your code. If your implementation doesn't use `__DEBUG__`, then you're fine, right up to the point where you switch to a different compiler (or compiler version). The true evilness of undefined behavior is that it sometimes _works,_ leading you to a false sense of security :-) – paxdiablo Feb 24 '11 at 05:41