?
Solved

Using "Conditional Compilation"

Posted on 2014-03-15
9
Medium Priority
?
607 Views
Last Modified: 2014-03-16
Hi, I got a little info googling around on "Conditional Compilation" for Delphi, and I now know just enough to be dangerous with it. It seems like it will be very convenient and save me from having to keep two versions of my source code - one for demo, and one for full version. My question is - when I turn on a conditional compiler directive like this from the FormCreate procedure of my main form:

{$Define DEMOMODEON}

... is it "global" over all the forms and units of my app? Just doing a quick test here, it seems not. Is there some way where I only have to make my 'Define' statement once (like in the project .dpr file maybe?) and it will be global for all the forms in my project... or do I have to explicitly specify the {$Define DEMOMODEON} directive in the FormCreate event for each of my forms/units?

Thanks!
    Shawn
0
Comment
Question by:shawn857
  • 4
  • 3
  • 2
9 Comments
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 39931863
No, it isn't global, you have to define your condition in each form.
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 39931871
But you don't have to define that in the FormCreate event. Just put it in your implementation section after the uses clause
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, ... ;

type

  TGetVersion = function (AppName: PChar): PChar;


  TForm1 = class(TForm)
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm;


implementation

uses someunit;

{$Define DEMOMODEON}

Open in new window

0
 
LVL 28

Accepted Solution

by:
Sinisa Vuk earned 1200 total points
ID: 39931904
You can set global for all forms in different .dproj (one for release, demo, ...)
Add  DEMOMODEON in Projects Options/Directories-Conditionals/Conditional defines...
(separate it with ';')
0
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

 

Author Comment

by:shawn857
ID: 39932111
Thank you both. Sinisa - I use Delphi 7, I don't think I have this capability for .dproj files? This is only for D2007 and upwards?

Cheers
   Shawn
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 39932236
You have for .dpr files in the same Project Options panel: Main menu Project -> Options ->Directorues/Conditionals/Conditional defines

@sinisav The beauty to be here in EE is that I always learn somthing more: I every used writing define in the code... And now I have a doubt: could I put gobal define writing directly in the project source?
0
 

Author Comment

by:shawn857
ID: 39932240
Yes I would like to know this too Marco! How is it done?

Ciao
   Shawn
0
 
LVL 31

Assisted Solution

by:Marco Gasi
Marco Gasi earned 800 total points
ID: 39932246
If you click Project menu, you can choose View source: there you can write code to manage splashes creens, for instance. Well, there I think you can write your global defines immediately after the first line with the project name, project Project1.

I'm not sure, but you can give it a try to see if a define there is global and saw by all forms...
0
 
LVL 28

Expert Comment

by:Sinisa Vuk
ID: 39932970
Common usage is to use "include" file where those "defines" are defined. Then include this file ( {$I MyincFile.inc} ) to all forms/units where you want to use conditionals.
0
 

Author Closing Comment

by:shawn857
ID: 39933500
Thank you both Sinisa and Marco for your responses. Indeed, in the Project|Options|Directories/Conditionals tab next to "Conditional Defines", a few simple tests revealed a very nice solution for me: I have two conditions I must have for my app:

DEMOMODEON   (demo version of my app, some features crippled)
SECURITYON        (version of my app where I add the 3rd party security anti-cracking module... undebuggable from the IDE in this state)

Using these two switches, I can have 4 different EXE files:

(1) Non-final version of my app (ie. debuggable) in full-feature mode (no switches turned on)
(2) Non-final version of my app (ie. debuggable) in DEMO mode (only DEMOMODEON turned on)
(3) Final version of my app (ie. not debuggable, with security module) in full-feature mode (only SECURITYON turned on)
(4) Final version of my app (ie. not debuggable, with security module) in DEMO mode (both SECURITYON and DEMOMODEON turned on).

This is fantastic! I don't need to maintain 4 different source codes - only one! All I needed to do was add 3 items in the "Conditional Defines" area mentioned above:

DEMOMODEON
SECURITYON
DEMOMODEON;SECURITYON

Then I can choose one, or none of these for whatever appropriate "mode" I choose to compile... fantastic!

Thanks!
   Shawn
0

Featured Post

Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…
How to fix display issue, screen flickering issue when I plug in power cord to the machine. Before I start explaining the solution lets check out once the issue how it looks like after I connect the power cord. most of you also have faced this…

601 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