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 my开发者_如何学编程self? Or not?
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.
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.
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.
It may be possible, but you stop trying to find out, it is certainly bad practice.
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).
精彩评论