18

I remember reading an article or post somewhere years ago that suggested including a resource file in a project by referencing the .rc file instead of an already compiled .res file so that the resource is built as part of the project's build process.

I have a glyphs.rc file that I currently compile using the command brcc32 glyphs.rc. In my project file I then have the statement {$R Glyphs.res}.

I'd like to simplify this by changing it to something like

{$R Glyphs.rc} 

but am unsure of the syntax. When I try using {$R Glyphs.rc} I get an error `

[DCC Error] E2161 Error: RLINK32: Unsupported 16bit resource in file "Glyphs.rc". 

Is this approach possible with Delphi 2007?

Toby Allen
  • 10,997
  • 11
  • 73
  • 124
lukeck
  • 4,528
  • 3
  • 27
  • 29

6 Answers6

25

Just add the rc file to your project via the "Project > Add to project" menu item. This creates the {$R 'myres.res' 'myres.rc'} line from the posting that TOndrej links to.

Community
  • 1
  • 1
Uli Gerhardt
  • 13,748
  • 1
  • 45
  • 83
7

The linker can only handle res files, but you can direct the compiler to invoke the resource compiler and compile an rc script to produce a res file and link that, using a variation of the $R/$RESOURCE directive.

In your case (Delphi 2007) you should need only change:

 {$r glyphs.res}

to

 {$r glyphs.res glyphs.rc}

If this doesn't work on its own, try adding the RC to the project. In different versions of Delphi you may need single quotes around the filenames:

 {$r 'glyphs.res' 'glyphs.rc'}

NOTE: You do still need to identify a res file, the difference is in being able to additionally identify the rc file to be compiled in order to produce the required res file in the first place.

Support for this appears to have been subject to some tinkering and in more recent versions adding the RC to the project does not always seem to be "detected" by the project until after you have then saved, closed and re-opened the project (e.g. I found this to be the case in XE4 but may also apply to other versions).

Also in some more recent versions, simply adding such a $R 'file.res' 'file.rc' declaration to the DPR causes the Project Manager to identify the referenced RC file as part of the project, but this does not seem to be the case in older versions. Again, part of the tinkering in this area it seems.

I would also note the XE4 is usually rock solid in terms of stability, but adding/removing RC files seemed to trigger an access violation when closing the IDE, though normal stability seemed to be restored when re-opening the IDE and project. i.e. it is adding/removing RC files that seems to cause a problem, not simply the fact of having the RC file in the project.

UPDATE: In recent versions of Delphi (Delphi 10.2 Berlin) you should include custom resources before {$R *.res} line, otherwise they will not be automatically compiled.

Eugene Mala
  • 1,081
  • 12
  • 25
Deltics
  • 22,162
  • 2
  • 42
  • 70
  • 1
    Jolyon, I believe this stopped working some versions ago (presumably with the switch to MSBuild?). It does not work any more in my copy of Delphi XE2 in any case. – Oliver Giesen Aug 22 '12 at 11:56
  • Merely changing the $R directive doesn't work in XE2. The .rc file has to be added to the project with Project|Add to Project. – Jan Goyvaerts Mar 14 '16 at 07:17
  • @Jan - To complicate matters it appears that support in this area has been subject to tinkering over the years. In earlier versions you didn't need quotes around the filenames (though it still worked if you had them) but the RC file had to be added to the project. Later, the quotes became essential but the RC file didn't need to be added to the project. I don't have XE2 readily to hand at the moment but as of XE4, if you just add quotes around the filenames it should work. If not, try adding the RC to the project as well. – Deltics Mar 14 '16 at 20:25
  • @Jan - scratch that. You do still need the RC added to the project. I was led to think that you didn't by the fact that if you remove the RC file the project will still build unless and until you close the project and re-open it. The project doesn't immediately detect when RC files have been removed (or indeed added!). I've updated my answer with these observations (as apply to XE4 but also potentially explaining why it didn't initially appear to work in XE2) – Deltics Mar 14 '16 at 20:40
3

See an example here: "How do I make a PNG resource?".

Community
  • 1
  • 1
Ondrej Kelle
  • 36,941
  • 2
  • 65
  • 128
3

I had the same problem and found out something new.

{$R glyphs.res glyphs.rc}

this is the code for compiling glyphs.rc to glyphs.res in the pre-build. (Works with Delphi XE4)

But this code ONLY works if it is in the *.dpr file! If you place this code, in a *.pas file as I did the first time, it will simply behave like {$R glyphs.res} and will not compile the RC file. Maybe this is a bug in Delphi.

2

I tried to do this in Delphi 2007 and it didn't work. I had put the line,

"{$R glyphs.res glyphs.rc}"

in my project file right above the "{$R *.res}" line that the IDE puts in there but when I added the rc file using the IDE, it put it above the "uses" line and then it worked.

Koot33
  • 304
  • 2
  • 10
0

I could not get rid from the mainicon in my application, so i made an trapgate.rc file put that file in the src directory, used:

 MAINICON icon ".\Icon\MAINICON.ico"
 5012 icon ".\Icon\5012.ico"

Then used BRCC32 to make from the RC a RES file, did the build and i had the correct icon. you can also put more icons in there and switch thats why i added the line in makeres.bat looks like this :

brcc32 folders.rc -fofolders.res
brcc32 main.rc -fomain.res
brcc32 xOutline.rc -foxOutline.res
brcc32 xSpin.rc -foxSpin.res
brcc32 credits.rc -focredits.res
brcc32 licence.rc -folicence.res
brcc32 trapgate.rc -fotrapgate.res <-- this is my icon file

So whatever you do even if you change the icon in the folder ..\icons of course be sure it has the correct name like mainicon.ico and 5012.ico

Hope that did help for does who can't change the icon in Delphi 7 itself.

Thilina H
  • 5,754
  • 6
  • 26
  • 56
Xantis
  • 1