11
votes

Delphi sometimes adds {$R *.res} in front of the unit path in the .dpr file uses clauses, then I get a duplicated resources warning when trying to compile.

Anyone knows why the hell Delphi does that? I'm using Delphi 2009 but this happens since Delphi 2007 (maybe 2006 too)

7
What helps for me is to fix the dpr file manually and then declare it read-only so that the IDE does not alter it.Johan
@Johan -- this is not solution, not even closer to solutionZam

7 Answers

8
votes

It depends on what else you've done to the .dpr file. Delphi expects that file to be layed out in a certain way, and if you've modified it in such a way that the internal IDE parsers are unable to correctly find certain things, it can guess wrong. Originally, the .dpr file was never intended for the user to modify at will, and so it can get confused. IFDEFS are the most common culprits which can confuse the IDE parser.

7
votes

The issue why the Delphi is adding you these "wrong" {$R} and {$R *.res} texts is hidden in the DPROJ file. Just open the DPROJ file with a text editor and search for the $R *.res and remove these tags:

<DCCReference Include="..\..\..\Core\IF.Common\uTranslation.Types.pas">
  <Form>$R *.res</Form>
</DCCReference>

Change that to

<DCCReference Include="..\..\..\Core\IF.Common\uTranslation.Types.pas"/>

Now you will not get the crappy text in your project (until next time IDE fails adding such thing to DPROJ file.

6
votes

Perhaps posting your .dpr would help illustrate your problem. My project files look like this and give me no problem:

program Example;

{$R *.res}

uses
  Unit1 in 'Unit1.pas' {frmUnit1};

begin
  Application.Initialize;
  Application.CreateForm(TfrmUnit1, frmUnit1);
  Application.Run;
end.
5
votes

Delphi adds {$R *.res} to your .dpr file to link the .res file that it generates into your application. E.g. if you save your project as MyProject.dpr, Delphi will create a file MyProject.res that contains your application icon and version information that you specify in Project Options in Delphi. Without this .res file, your .exe won't have an icon or version info.

If you get a duplicate resource warning, you probably have another {$R} compiler directive elsewhere in your code that also links in MyProject.res. It could be a duplicate {$R *.res} in your .dpr file, or a {$R MyProject.res} in another .pas file. Delete the other compiler directive instead of the one that Delphi generates automatically, and your project will compile just fine.

4
votes

I have acquired some "heuristics" to deal with fact that the IDE messes with the dpr:

  1. No "complicated" code in the main begin-end-block (i.e. stuff with variables or ifs :-)). Everything I need to be done there is swapped out into separate routines. This seems to make the IDE parser happier.
  2. If I need $IFDEFs in the uses clause I make a "proxy unit" which contains the $IFDEFed units and put that in the dpr's uses clause.
2
votes

It is very annoying, happens without any obvious reasons and cannot be prevented from happening - as far as I know :(

1
votes

the default {$R *.res} in .dpr should be between uses and var clauses.

sometimes project files gets corrupted..
just delete any file in your source directory except *.dpr, *.pas, *.dfm
delphi will rebuild other files, included *.res

thats'it