17
votes

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?

6

6 Answers

24
votes

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.

7
votes

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.

3
votes

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.

1
votes

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.

0
votes

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.