12

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)

Fabio Gomes
  • 5,914
  • 11
  • 61
  • 77
  • 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 Jun 16 '16 at 12:06
  • @Johan -- this is not solution, not even closer to solution – Zam Jun 17 '16 at 07:14

7 Answers7

9

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.

Allen Bauer
  • 16,657
  • 2
  • 56
  • 74
  • 1
    I removed everything from the dpr, there are no ifdefs, no code, nothing, and it keeps happening! – Fabio Gomes Nov 25 '08 at 17:55
  • @Gomes: Can you post the contents of the DPR? – Bruce McGee Nov 25 '08 at 18:17
  • But it's often extremely desirable to use IFDEFs in the dpr file to control which units are being included. For example you might want to switch memory manager with a conditional. I think it would be a boon if the IDE could cope with this. – David Heffernan Jan 14 '11 at 11:56
  • With the nearly infinite ways in which IFDEFs can be placed, I would like to challenge folks to devise a way in which the IDE can continue to update the uses list as it needs to, and maintain those IFDEFs without "getting it wrong." For instance, do additions/subtractions from the project dive into the IFDEFs or not? What if the current project options enable an IFDEF, or not? What if an $IF (not IFDEF) were used that requires compile-time evaluation? What if the IFDEF includes/excludes the trailing semi-colon? – Allen Bauer Jan 14 '11 at 18:12
9

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.

Z.B.
  • 1,185
  • 9
  • 18
7

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.
Scott W
  • 9,742
  • 2
  • 38
  • 53
  • My {$R *.res} code is bellow the uses, maybe that is the problem. I've changed it here and will test it out. – Fabio Gomes Nov 26 '08 at 13:40
  • Moving the {$R *.res} above the uses statement helped in my case. Now I don't receive any duplicate resource warnings anymore (Delphi XE) – Michael Küller Sep 29 '11 at 11:01
5

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.

Jan Goyvaerts
  • 21,379
  • 7
  • 60
  • 72
4

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.
Uli Gerhardt
  • 13,748
  • 1
  • 45
  • 83
2

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

gabr
  • 26,580
  • 9
  • 75
  • 141
1

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

MtwStark
  • 3,866
  • 1
  • 18
  • 32