Compiler Directive Example

Hello,

I want to easily compile two versions of the same program, the difference is some functions and statements will be eliminated in one version.
How can I achive this by using compiler directive ? Please provide a working example.
I am using Delphi 7.

Thanks.
Ray
RaymondAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DarkCore_Commented:
well ... just you can go to Project->Options and Define one conditional : As an example, PROFESIONAL or HOME.

then just you can do something like

procedure TMyForm.OnFormCreate( Sender : Tobject );
Begin
  {$IFDEF HOME}
       Caption := 'My Application : HOME edition';
       FreeAndNil( ExtendedMenu );
       FreeAndNil( ExtendedOptions );
  {$ELSE}
       Caption := 'My Application : PROFESIONAL edition';
  {$ENDIF}
End;

Also, if you want, you can go to form's definition

TMyForm = class( TForm )
     .....
     {$IFDEF PROFESIONAL}
      AMenu : TMenuItem;
      AnAction : TMenuItem;
     {$ENDIF}
      ...
   public
      procedure AProc;
End;

procedure TMyForm.AProc;
begin
   {$IFDEF HOME}
       Some actions
   {$ELSE}
        Other Actions
   {$ENDIF}
End;

Only a comment, everytime you want to generate a version or another, just simply change your define.You must BUILD ALL units. If you not do this, then some units will be still compiled with your old directive.

Edu
0
RaymondAuthor Commented:
- Does this compiler directive work everywhere in the program code ?
{$IFDEF HOME}
  ....
{$ENDIF}

- How about if I don't want to define a conditional in normal version ?  But I will define one when I want to eliminate some codes.

- "You must BUILD ALL units. If you not do this, then some units will be still compiled with your old directive." What do you mean ?  Can you explain more ?

Thanks.
Ray
0
DarkCore_Commented:
a) If you define it in Project -> Options -> Compiler directives, or in the command line ( dcc32 -Ddefines ) yes, it works in _ALL_ the project. If you define it in source code {$DEFINE abc}, it will only work in current unit.

b) well, this is correct to, just define a HOME and you can use
    {$IFDEF HOME}
         Cut some features
     {$ENDIF}

or you can use too IFNDEF ( IF NOT DEFINED )
     {$IFNDEF HOME}
          Enable some features
     {$ENDIF}

c) Yes ;)
     imagine you use compiler directive dependant code in 3 units of your project. You want to generate "normal" version, so you don't have any compiler directive. Well, It will compile all units and generate all DCU's thinking it's "normal" version.
     Now you want to generate "home" version. Then you go to Compiler directives and type "HOME" to define this directive. If you only compile the project, already compiled libraries will not be compiled again, so most of them will remain without "home" directive enabled.
     You must rebuild all the project, to force enable this setting in all units.

Edu
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Wim ten BrinkSelf-employed developerCommented:
Be aware that you will have to do a Rebuild for your project if you want to make two versions! Thus, build project, rename executable, change compiler directive and rebuild again.

Personally, I use an interesting trick to have two versions of a project. I create two projects! (project1.dpr and Project1Home.dpr) Or just two DPR files, to be precise. They both will use the same units and almost all settings will be identical, although I use different DCU folders for them to store the compiled units. In one project I will also add the definition for the compiler directive.

The advantage of using two different project files is that you don't have to change the compiler directive every time you want to create a new version. But you will have to set separate DCU folders for both projects or else the compiled units of one project will cause problems with the other ones...
0
Ivanov_GCommented:
{$DEFINE DEBUG_VERSION}  
// it the line above you define Debug Version with Logging

// YOUR CODE HERE
...
{$IFDEF $DEBUG_VERSION}
  WriteToLog(..............)
{$ENDIF}
...
// YOUR CODE CONTINUE HERE
0
RaymondAuthor Commented:
Hi DarkCore,

> yes, it works in _ALL_ the project.
Do you mean once the conditional is entered in "Project -> Options", this setting will take effect to each project opened afterwards ?

I can't find "Compiler directives" tab in "Project -> Options" ?  I am using Delphi 7.

What is the safest way to ensure I do rebuild all the units when compiling between different version ??

Thanks.

Ray
0
Ivanov_GCommented:
I can't find "Compiler directives" tab in "Project -> Options" ?  I am using Delphi 7.

Project / Options / "directories/conditionals" / Conditionals
0
DarkCore_Commented:
Ivanov is in true, is there in the menu.

And no, compiler directives are only applied in the current project, but setting this directives in ( Project / Options / "directories/conditionals" / Conditionals ) makes them available in the whole active project.

Also, when I'm working with distinct versions, I have a makefile, using dcc32. Makefiles are easy to do, you can rebuild all project, build number is not incremented, and just "one click" if you do a bat which executes this makefile.

And you haven't to modify each time the dpr. Only think that when you add units, you must remeber to add too those units in the other project file.

Edu
0
RaymondAuthor Commented:
Hi DarkCore,

Thanks for your help.
But I still don't know how to rebuild all units reliably.

Ray
0
DarkCore_Commented:
From Delphi Menú :
Project / Compile project : You "compile" only modified files.
Project / Build Project : Recompile all the current project. *This* is the option you want!

Edu
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
BlackTigerXCommented:
I think the best approach is WorkShop_Alex's, is a LOT cleaner

"Personally, I use an interesting trick to have two versions of a project. I create two projects! (project1.dpr and Project1Home.dpr) Or just two DPR files, to be precise. They both will use the same units and almost all settings will be identical, although I use different DCU folders for them to store the compiled units. In one project I will also add the definition for the compiler directive.

The advantage of using two different project files is that you don't have to change the compiler directive every time you want to create a new version. But you will have to set separate DCU folders for both projects or else the compiled units of one project will cause problems with the other ones..."
0
Wim ten BrinkSelf-employed developerCommented:
Yep. As I said before, creating two projects that share the same units is a very clean solution and I've used it several times already. Just remember to use separate folders for the compiled DCU's, though. Otherwise the projects can "contaminate" each other.
0
DarkCore_Commented:
Well, using a makefile is not as diferent. Using compiler flags you can specify the exe's output path ( -E option ), the DCU output path ( -N option ), the object directories ( -O flag ) .... etc ... just thing that a "dpr" is like a makefile, all information it stores can be implemented in command-line.

And also, with a makefile you can do some other things, like compacting with UPX directly, moving the exe to another path, copy the generated exe to the installation path, and regenerate the installation. And, of course, remove all unusefull things generating while compiling.

All of this just one click thru is you do the makefile, a "bat" executing it and a shortcut to that bat.
And, of course, without incrementing build number in project's properties ;)

Edu
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.