former worked code does not work - delphi

There are code below and worked well before
unit Unit1;
{$DEFINE _Full}
// {$DEFINE _Trial}
interface
uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms;
{$ifdef _Trial}
_programname='abc';
{$endif}
{$ifdef _Full}
_programname='abc';
{$endif}
but today I run Delphi and try to compile, it reported
Identifier redeclared: '_programname'
it looks like
{$DEFINE _Full}
does not work
your comment welcome

Both conditionals are defined. That can be inferred from the compiler error.
Either single line // comments do not comment out defines. Or you are defining _Trial at the project level. To the very best of my knowledge, a single line // comment will comment out a directive. So I presume that _Trial is defined at the project level.
The idiomatic way to comment out defines is like this:
{.$DEFINE _Trial}
For an either or condition it might be simpler with a single conditional:
{$IFDEF _Trial}
.... stuff for trial version
{$ELSE}
.... stuff for full version
{$END}
All that said, perhaps your actual problem is different because the code you show does not match the error message you reported. I'd expect an error saying that a keyword was expected, but identifier _programname found. The code in the question appears to omit the const keyword before the declaration of _programname.

I am submitting this under the assumption that _programname is supposed to be a constant, as that is most consistent with t
he code you are showing. Your constant declarations need to be declared in a CONST section block. Try this:
unit Unit1;
{$DEFINE _Full}
// {$DEFINE _Trial}
interface
uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms;
CONST //<< Changed Code
{$ifdef _Trial}
_programname='abc';
{$endif}
{$ifdef _Full}
_programname='abc';
{$endif}

Related

is it possible via {$define xx} to define an constant value (like CompilerVersion)

is it possible via {$define xx} to define an constant value (like CompilerVersion). For example I would like to do something like
{$define FrogCount=25}
but seam to not work :(
I need to do this inside a myincfile.inc file because I need to do stuff like
interface
{$I myincfile.inc}
uses
{$IF FrogCount = 25}
toto;
{$else}
lala;
{$endif}
and you understand that here I can't do stuff like
const FrogCount = 25;
inside myincfile.inc else I have a compiler error :(
In Delphi, you cannot define a constant value in a {$DEFINE ident} statement, you can define only the ident name by itself, which is then usable only with {$IF(N)DEF ident} and {$IF (NOT) DEFINED(ident)} statements.
In FreePascal, you can define a constant value in a {$DEFINE ident:=expr} statement, but only when {$MACRO ON} is enabled. See Macros in FreePascal's documentation.

How to avoid insert namespace in Delphi uses

I manage a huge project in Delphi 2007. The target is to upgrade it to Delphi 10.1 Berlin this year. So in the meantime the source is compiled in both versions.
If there is a problem with the new Delphi we want the old version as backup.
My problem in unit dmActions.pas that is a unit inherited from TDataModule.
uses
// VCL
ActnList,
ActnMan,
Classes,
Controls,
Forms,
Graphics,
ImgList,
Menus,
SysUtils,
XPStyleActnCtrls,
Variants,
{$IFDEF BOLD_DELPHI16_OR_LATER}
System.ImageList,
System.Actions,
{$ENDIF}
BusinessClasses;
Delphi IDE don't understand my IFDEF so it automatically insert missing units to this
uses
// VCL
ActnList,
ActnMan,
Classes,
Controls,
Forms,
Graphics,
ImgList,
Menus,
SysUtils,
XPStyleActnCtrls,
Variants,
{$IFDEF BOLD_DELPHI16_OR_LATER}
System.ImageList,
System.Actions,
{$ENDIF}
BusinessClasses, System.ImageList, System.Actions;
But this don't compile in Berlin with this message
[dcc32 Error] dmActions.pas(36): E2004 Identifier redeclared: 'System.ImageList'
[dcc32 Error] dmActions.pas(36): E2004 Identifier redeclared: 'System.Actions'
And of course "System.ImageList, System.Actions" don't compile in D2007.
So what is my best action to solve this ?
You can make use of the Unit Aliases feature of Delphi here - at least as your Delphi 2007 supports dotted unit names in the first place. This allows to use the new unit names like System.SysUtils from Delphi 10.1 Berlin and still compile that project with Delphi 2007.
For this you have to add mappings to the Unit Aliases of the Delphi 2007 project like this:
System.SysUtils=SysUtils
System.Classes=Classes
For units that don't exist in Delphi 2007, like the ones you mention in your post, simply map to an existing unit:
System.Actions=ActnList
System.ImageList=ImgList
As a benefit you end up with uses clauses free of IFDEFs.
As https://stackoverflow.com/users/2916756/nolaspeaker said it works by test compiler version directly. I used an inc-file and that don't work well in this case
But in my case I check Berlin so:
{$IFDEF VER310}
System.ImageList,
System.Actions,
{$ENDIF}

Why doesn't code from "The Tomes of Delphi" compile?

I'm trying to use the TDRecLst and TDSplyCm units from the code included with The Tomes of Delphi, but I get a compiler error in TDBasics.pas:
I get a similar error in TDStrRes.inc:
What's wrong, and how do I fix it?
The code is available from the author.
You're evidently using a Delphi version that's newer than Delphi 6. Despite being updated in 2005, the code from that book only detects up to that version of Delphi. TDDefine.inc defines a number of compiler symbols based on the version it detects, but when the version you're using isn't anything it recognizes, it defines no symbols. That eventually leads to problems later when the compiler encounters code like this in TDBasics.pas;
implementation
uses
{$IFDEF Delphi1}
WinTypes, WinProcs;
{$ENDIF}
{$IFDEF Delphi2Plus}
Windows;
{$ENDIF}
{$IFDEF Kylix1Plus}
Types, Libc;
{$ENDIF}
{$IFDEF Delphi1}
{$R TDStrRes.r16}
{$ENDIF}
{$IFDEF Delphi2Plus}
{$R TDStrRes.r32}
{$ENDIF}
{$IFDEF Kylix1Plus}
{$R TDStrRes.r32}
{$ENDIF}
const
UnitName = 'TDBasics';
Since none of Delphi1, Delphi2Plus, or Kylix1Plus is defined, the uses clause is empty. When we ignore all the compiler directives and inactive code blocks, the compiler ultimately sees code like this:
implementation
uses
const
UnitName = 'TDBasics';
That's why the compiler complains about expecting an identifier instead of const.
To fix it, you need to teach TDDefine.inc to recognize your version of Delphi. Easier, though, might be to ignore all the version-detection code and hard-code all the symbols that apply to the version you're using. As long as you never use any version older than Delphi 6, all the symbols will apply to all your versions.
Find the following block of code in TDDefine.pas:
{$IFDEF VER140}
{$DEFINE Delphi6}
{$DEFINE Delphi1Plus}
{$DEFINE Delphi2Plus}
{$DEFINE Delphi3Plus}
{$DEFINE Delphi4Plus}
{$DEFINE Delphi5Plus}
{$DEFINE Delphi6Plus}
{$DEFINE HasAssert}
{$ENDIF}
Remove the first and last lines so that the remaining $DEFINE instructions are processed unconditionally.

What unit scope do I need for this?

I am trying to install a 3rd party package and I get a compile error:
[DCC Error] fiile/line : E2003 Undeclared identifier: 'Windows'
which refers to this line:
wnd := Windows.GetFocus;
It seems fairly obvious that I don't have my Unit Scopes right - but which do I need (and is there a general approach to find which use clause I need)?
I currently have
Data.Win;Datasnap.Win;Web.Win;Soap.Win;Xml.Win;Bde;System;Xml;Data;Datasnap;Web;
Soap;Vcl;Vcl.Imaging;Vcl.Touch;Vcl.Samples;Vcl.Shell;Winapi;System.Win
[Update]
interface
uses
SysUtils, winapi.windows, Classes, Controls, ExtCtrls, Graphics, StdCtrls,
Dialogs, IniFiles, winapi.messages, Forms, Math
{$IFDEF DELPHI6_LVL}
, Variants
{$ENDIF}
;
No uses in the impementation section.
[Upate]
I forgot to mention. I failed (in the same way) to install it on one laptop. Then I succeeded on a second. The trouble is that I'd rather have it on my desktop and after a fresh install of XE2 starter I get these problems.
Assuming that your uses names the Windows unit at all, it would appear to do so by naming the unit as Winapi.Windows. And so your code must also do so and be written as
wnd := Winapi.Windows.GetFocus;
When you use a unit by naming the fully scoped unit name, you must also use the fully scoped name in subsequent code in that unit.
Now, if you want to use the name Windows then you must name the unit as Windows in the uses clause and let the unit alias setting do its job. If you imported the unit by naming it Windows then your original code will work.
To be very clear:
uses
Winapi.Windows;
is what you have now but you would need:
uses
Windows;
for your code to compile.
You unit scope looks fine, so try these two options
declare in your uses section Windows instead of Winapi.Windows
or modify your code like so
wnd := Winapi.Windows.GetFocus;

Delphi XE2 cannot find ComObj.dcu where did it go?

I'm trying to install the jvcl from source, but I'm getting an error in
line #1267 of unit JvInterpreter;
uses
TypInfo,
{$IFDEF JvInterpreter_OLEAUTO}
OleConst, ActiveX, ComObj,
So I removed ComObj from the uses and waited for the error further down the line:
There's an error concerning EOLEError, which is part of OleAuto I added that and hoped for the best, but....
I get an error on this line #1799:
DispatchInvoke(IDispatch(Dispatch), CallDesc, PDispIDList(#DispIDs[0]), ParamTypes, Result);
So the question is: what happened to ComObj and what unit do I need for DispatchInvoke in XE2?
The solution is to change the uses to use a fully qualified name:
uses
TypInfo,
{$IFDEF JvInterpreter_OLEAUTO}
OleConst, ActiveX,
{$IFDEF VER230} system.win.ComObj, {$ELSE} ComObj, {$ENDIF}
Now it compiles without error.
See: What is the compiler version for Delphi 2010?
For a list of compiler defines.
Simply add system.win i.e. instead of comobj use system.win.comobj

Resources