Really simple #pragma question

Member
Posts: 321
Joined: 2004.10
Post: #1
Ok, before I get yelled at, I've looked at tens of pages explaining the #pragma commands, but not one tells me how to actually activate themMad

Code:
#ifdef DEBUG
     cout << "This is the test version, i=" << i << endl;
   #else
     cout << "This is the production version!" << endl;
   #endif

Say, I have the above code.

How to I "turn on" the DEBUG variable?

Can some one tell me the both the old way using a command line and the new way in Xcode?

I assume you don't do something like #define DEBUG 1
Quote this message in a reply
Sage
Posts: 1,403
Joined: 2005.07
Post: #2
gcc -DDEBUG
or
#define DEBUG

Sir, e^iπ + 1 = 0, hence God exists; reply!
Quote this message in a reply
Oldtimer
Posts: 834
Joined: 2002.09
Post: #3
To be precise, those aren't pragmas. Pragmas are compiler-specific controls (line #pragma once, #pragma optimization_level and so on) while the above is a preprocessor variable.
Quote this message in a reply
Moderator
Posts: 3,571
Joined: 2003.06
Post: #4
Also, unrecognized pramas are ignored by the compiler.

#pragma mark -

is one of my favorites, which puts a line in between functions in the function pop-up menu in Xcode. That pragma might have been in Codewarrior too, I don't recall. You can also do:

#pragma mark These Functions Do Blah Blah Blah

and Xcode will insert a bold label saying, `These Functions Do Blah Blah Blah' in the function pop-up.

What you're doing is called a conditional inclusion (not a pragma, as Fenris pointed out), which allows you to conditionally include code depending upon the outcome of an expression. The lines that begin with # can also be macro substitutions, like #define, and file inclusions like #include, and a couple others. Collectively they are referred to as preprocessor directives. Preprocessor directives (lines that begin with #) are digested by the preprocessor first, before any of the code is compiled.
Quote this message in a reply
Member
Posts: 321
Joined: 2004.10
Post: #5
Thanks!

OK, the #define DEBUG would require an edit and a recompile, right?
So the other way would probably be considered more elegant.

Say, to get the
gcc -Dpreprocessorvariable syntax,

what project information field/parameter would need to be filled out
in Xcode?
Quote this message in a reply
Sage
Posts: 1,066
Joined: 2004.07
Post: #6
In Xcode, right click on your project and choose the show information (or similar) option. Go into the build settings. There is an option in there (not sure exactly where) that allows you to define additional preprocessor commands (I don't have Xcode with me to check the exact name). At the top, there is a pull down menu that lets you choose Debug or Release. Make sure Debug is selected and put DEBUG into the field. With that in place, whenever you build it as a Debug, it'll automatically have the DEBUG preprocessor command defined and when you build it as Release, it won't.

Sorry I can't get all the specific names, but I'm in an airport. The option is in there so hopefully with a little digging you can find it. If not, when I get to Washington, I'll help out a little more. By then, though, I'm sure someone else will give you the precise spot.
Quote this message in a reply
Member
Posts: 114
Joined: 2005.03
Post: #7
You can actually have it even one step easier. In the build settings, under "Preprocessing" (which is under "GCC C/C++ Compiler 4.0) you'll find "Preprocessor Macros" and "Preprocessor Macros Not Used In Precompiled Headers". You'll just enter DEBUG under one of them. The difference for this purpose, as far as I see it, is that the latter means that the complete rebuild of your app (which is necessary in either case) takes far less time.
Quote this message in a reply
Moderator
Posts: 3,571
Joined: 2003.06
Post: #8
I usually just do #define DEBUG 1, wherever I need it (the entire project or just specific files). But I knew there were several other ways (as have already been listed here), so I Googled a bit and found this at Apple, which is about programming with assertions, but should be generally relevant for defining DEBUG:

Quote:To define DEBUG in your project you can do any of the below:

add "#define DEBUG 1" before any of the "#include"'s in your prefix file.

Add "DEBUG=1" to your "extra preprocessor defines" for your Debug target, so assertions are quiet in the Release target.

In Xcode's "Debug" configuration, in the "Preprocessing" collection set the "Preprocessor Macros" value to "DEBUG=1"

In your project or target build settings set "OTHER_CFLAGS" to "$(value) -DDEBUG=1" for the "Debug" configuration.

Add "#define DEBUG 1" to just the source files where you want assertions enabled and then every exception in just those files will cause an assertion.
Quote this message in a reply
Member
Posts: 321
Joined: 2004.10
Post: #9
Thanks! That actually answered a whole slew of questions.

How come whenever I try to find stuff on Apple's web site, I fail miserably
Smile
Quote this message in a reply
Sage
Posts: 1,066
Joined: 2004.07
Post: #10
Cochrane Wrote:You can actually have it even one step easier. In the build settings, under "Preprocessing" (which is under "GCC C/C++ Compiler 4.0) you'll find "Preprocessor Macros" and "Preprocessor Macros Not Used In Precompiled Headers". You'll just enter DEBUG under one of them. The difference for this purpose, as far as I see it, is that the latter means that the complete rebuild of your app (which is necessary in either case) takes far less time.

Ah. That's the one I was talking about. Again, lack of Xcode to check for the actual spot.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  simple question (private) imaumac 9 3,730 Oct 21, 2008 09:37 PM
Last Post: imaumac
  simple Question about movement bonanza 3 2,967 Oct 17, 2007 11:12 AM
Last Post: bonanza
  Simple CarbonEvent Question Abyssal 3 3,095 Jan 1, 2006 09:53 PM
Last Post: radiance
  Simple OpenGL/Xcode Question. loopfick 10 6,406 Sep 8, 2005 09:54 AM
Last Post: Volte
  Simple Animation Question KiroNeem 11 5,439 Nov 20, 2004 09:17 AM
Last Post: DoG