Solved

Conditional Compile in Run-Time Package

Posted on 1998-07-14
5
329 Views
Last Modified: 2010-04-04
The Delphi 3 help file has a section that briefly mentions a Conditionals tab in the Packages Option Dialog. Obviously, there's not one there. So how do I put conditionally compiled code into a unit that's compiled into a run-time package?
0
Comment
Question by:chrismo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 12

Accepted Solution

by:
rwilson032697 earned 100 total points
ID: 1357895
chrismo,

Delphi does not give you much control over compiling packages from the IDE. However, there are ways to get around it.

1. If you need to set specific compiler directives for your code then these can be set in the code itself.

2. For conditional compilation the only real work-around you have (at least in v3, I'm not sure about v4) is to have an include file which contains the necessary defines....

eg: MyDefines.Inc
{$DEFINE MYDEFINE1}
{$DEFINE MYDEFINE2}

This can then include this at the top of your unit(s) like this:

{$I MyDefines.Inc}

Does this help?
0
 

Author Comment

by:chrismo
ID: 1357896
So I'd have to have two different include files and manually switch them to do my builds?
0
 
LVL 1

Expert Comment

by:Edo082297
ID: 1357897
Hello

We have an application that is broken into many run-time packages, and all of the units contain conditional compilation because the application must be bilingual. When you include a unit that contains conditional compilation, each time you do a build all Delphi will include only what is called and used, right down to the statement level due to the magic of the smart linker. So, when you do a final build, you will create a version that will depend on what switches you have set.
Why does this pose a problem?

Edo
0
 

Author Comment

by:chrismo
ID: 1357898
I'd forgotten to include my original problem in the first post:

I only have one section of a unit in a run-time pkg right now that has a conditional compile

{$IFDEF Alpha}
// blah blah blah
{$ENDIF}

If the main .dpr includes the Alpha directive -- everything works fine.

If the main .dpr does *not* include the Alpha directive -- the code in the run-time package acts unpredictably. Stepping through the code yields random results (jumps from one method to the middle of another method not even being called -- very bizarre behavior). I put the Alpha directive back in, and everything works fine again.

Oh, and yes, I make sure and rebuild the .dpl after I switch the directive. Should Delphi be rebuilding the .dpl itself when I do a Build All? -- because it doesn't. I do have the package option Rebuild As Needed set.

Wait ... I take that back - Delphi is rebuilding the .dpl when I do a Build All. But this doesn't help.

I do a Build All with Alpha - then step through the code in the run-time unit, everything's fine.

I do a Build All without Alpha - then step through the code in the run-time unit, and I get random stepping behavior. Sometimes the code appears to execute correctly anyway, many times the code doesn't run properly.

The method that contains the conditional compile section is not even executed by the portion I'm stepping through. It's weird.
0
 

Author Comment

by:chrismo
ID: 1357899
Well ... here's the full deal. I've 'fixed' it. Following is the original code:

procedure TTIMObject.LogChanges;
var
  i: integer;
  thisFieldLink: TFieldLink;

  function GetNextTransID: integer;
  begin
    with TIMDM.qryGetNextID do begin
      Prepare;
      ParamByName('strTableName').AsString := 'tblTransaction';
      Open;
      Result := FieldByName('intNextID').AsInteger;
      Close;
    end;

    with TIMDM.qrySetNextID do begin
      Prepare;
      ParamByName('strTableName').AsString := 'tblTransaction';
      ParamByName('intNextID').AsInteger := Result + 1;
      ExecSQL;
      Close;
    end;
  end;
begin
{$IFDEF OFFICE}
  if FLogChanges then begin
    for i := 0 to FFieldLinks.Count - 1 do begin
      thisFieldLink := TFieldLink(FFieldLinks[i]);
      with thisFieldLink do begin
        if Modified then begin
          with TIMDM.qryInsertTransaction do begin
            Prepare;
            ParamByName('cntTransID').AsInteger := GetNextTransID;
            ParamByName('intTransType').AsInteger := 1;
            ParamByName('intColumnID').AsInteger := DBFieldID;
            ParamByName('intObjectID').AsInteger := FID;
            ParamByName('strOldValue').AsString := OldValue;
            ParamByName('strNewValue').AsString := NewValue;
            ParamByName('dtmDateTime').AsDateTime := FTimeStamp;
            ParamByName('intUserID').AsInteger := FUserID;
            ExecSQL;
            Close;
          end;
          Modified := false;
        end;
      end;
    end;
  end;
{$ENDIF}  
  FLogged := true;
end; { LogChanges }

If I compiled with the OFFICE directive ... everything worked fine. If I compiled without the OFFICE directive - stepping into code acted very strange. Keep in mind the above method was not involved at all.

I decided on a whim to remove the GetNextTransID local method and make it a protected method of the object.

Making that changed fixed my glitch. I have no idea why. Now, no matter how I conditionally compile, stepping through the code works just fine. Very bizarre.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…

740 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