DelphiFAQ Home Search:

Handling of compiler definitions for a project

 

commentsThis article has not been rated yet. After reading, feel free to leave comments and rate it.

Question:

What is the best way to handle the compiler definitions in a project?

Answer:

This is certainly a matter of preference. One way is to rely on the project's options and hope that the compiler always picks up these options. They're saved in the project's .DOF file in the [Compiler] section - it looks like this:

[Compiler]
A=1
B=0
C=1
D=1
E=0


I personally don't like to rely on the .DOF file for a variety of reasons. See the other tip on project files why.

Several projects of mine use conditional compilation to create a variety of different products from a single source base, e.g. 'debug', 'internal', 'shipping', 'webserver' versions.

I like to express dependencies between these conditions as shown in the example DEFS.INC file below. All .PAS files in any project start with line
{$include DEFS.INC}

I tend to build those releases with batch files that invoke the Delphi commandline compiler. These batch files also invoke perl scripts which will edit this file DEFS.INC e.g. they will insert or remove the dot (.) in selected {.$DEFINE } statements. (The purpose is that the dot turns it into a regular comment.)

If you need to quickly insert the current compiler definitions while in the Delphi IDE, you can simply press Ctrl + O + O (twice). This works in all versions Delphi 1 through Kylix. Heck, it was even available in Turbo Pascal for DOS.

// definitions for SCPS

// DEBUG     defined --> see debug info
// FULLDEBUG defined --> log procedure execution (slows down)
// (nothing) defined --> shipping application

{.$DEFINE RELEASE} // this means it is for the customers!

{$UNDEF DEBUG}
{$DEFINE DEBUG}
{.$DEFINE FULLDEBUG}

{.$DEFINE DEMOVERSION}

{$UNDEF WEBSERVER}
{.$DEFINE WEBSERVER}


//--------------------------------------------------------------------------

{$IFDEF DEMOVERSION}
  {.$DEFINE RELEASE} // 'Demo' means it is for the customers!
{$ENDIF}

{$IFDEF RELEASE} // the customer never gets debug code!
  {$UNDEF DEBUG}
  {$UNDEF FULLDEBUG}
  {$UNDEF WEBSERVER}
{$ENDIF}


//--------------------------------------------------------------------------
{$IFDEF DEBUG}

  {$RANGECHECKS ON}
  {$IOCHECKS ON}
  {$OVERFLOWCHECKS ON}
  {$STACKCHECKS ON}
  {$STACKFRAMES ON}
  {$OPTIMIZATION OFF}
  {$ASSERTIONS ON}
  {$DEBUGINFO ON}
  {$LOCALSYMBOLS ON}
  {$REFERENCEINFO ON}

{$else}

  {$RANGECHECKS OFF}
  {$IOCHECKS OFF}
  {$OVERFLOWCHECKS OFF}
  {$STACKCHECKS OFF}
  {$STACKFRAMES OFF}
  {$OPTIMIZATION ON}
  {$ASSERTIONS OFF}
  {$DEBUGINFO OFF}
  {$LOCALSYMBOLS OFF}
  {$REFERENCEINFO OFF}
  
{$ENDIF}

{$IFNDEF FULLDEBUG}
{$UNDEF DEBUG}
{$ENDIF}
You don't like the formatting? Check out SourceCoder then!

Comments:

 

 

Email address (not necessary):

Rate as
Hide my email when showing my comment.
Please notify me once a day about new comments on this topic.
Please provide a valid email address if you select this option.
 
It seems that you are
from Los Angeles, US .

Info/ Feedback on this

Show city and country
Show country only
Hide my location
You can mark text as 'quoted' by putting [quote] .. [/quote] around it.
Please type in the code:
photo Add a picture:

Please do not post inappropriate pictures. Inappropriate pictures include pictures of minors and nudity. The owner of this web site reserves the right to delete such material.