?
Solved

Compiler Directive Example

Posted on 2004-11-24
13
Medium Priority
?
1,179 Views
Last Modified: 2008-01-09
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
0
Comment
Question by:Raymond
  • 5
  • 3
  • 2
  • +2
13 Comments
 
LVL 2

Expert Comment

by:DarkCore_
ID: 12663508
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
 

Author Comment

by:Raymond
ID: 12663600
- 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
 
LVL 2

Expert Comment

by:DarkCore_
ID: 12664076
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 12664179
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
 
LVL 12

Expert Comment

by:Ivanov_G
ID: 12664460
{$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
 

Author Comment

by:Raymond
ID: 12665015
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
 
LVL 12

Expert Comment

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

Project / Options / "directories/conditionals" / Conditionals
0
 
LVL 2

Expert Comment

by:DarkCore_
ID: 12666559
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
 

Author Comment

by:Raymond
ID: 12666911
Hi DarkCore,

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

Ray
0
 
LVL 2

Accepted Solution

by:
DarkCore_ earned 1000 total points
ID: 12670326
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
 
LVL 13

Expert Comment

by:BlackTigerX
ID: 12677746
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
 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 12682936
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
 
LVL 2

Expert Comment

by:DarkCore_
ID: 12684474
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

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses
Course of the Month16 days, 10 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question