Delphi pre-build event not executing BEFORE compile

▼魔方 西西 提交于 2019-11-27 20:46:30

Try using

{$R aVersionInfo.res}

and calling brcc32 aVersionInfo.rc manually from your batch file (after you're done with creating the .rc file). That way your .res file should be excluded from the normal IDE build.

May be is missing a ["]
ECHO. VALUE "ProductName","APP\000" >> aVersionInfo.rc
ECHO. VALUE "ProductVersion,"1\000" >> aVersionInfo.rc
------- HERE -----------------------^
ECHO. VALUE "Comments","Compiled on %date% by %username%\000" >> aVersionInfo.rc Bye

Add a line to your batch file that modifies the Delphi source file that includes the resource:

touch VersionInfo.pas

Then the source file should get recompiled. If the source file isn't modified, then there's no reason for the compiler to re-link that portion of the program, and so the updated resource file won't be noticed.

You could call brcc32 inside the batch file so it will always update the .res file. I do something similar and it works in a compile as well as a build.

Jan Doggen

TL;DR:

Place the line {$R 'aVersionInfo.res' 'aVersionInfo.rc'} directly below the Program statement once, then build. Or use BRCC32 to force the first time build of the .RES file

Long version:

Your guess that the line:

{$R 'aVersionInfo.res' 'aVersionInfo.rc'}

is in the wrong place is partially correct.

When I initially set up using an .RC file under Delphi XE2, I had the same trouble with slightly different code, sometimes compiling and sometimes not. I tried variations like:

{$R 'aVersionInfo.res' 'aVersionInfo.rc'}
{$R '.\aVersionInfo.res' '.\aVersionInfo.rc'}

but the XE2 compiler kept complaining about the RES file not being found, if indeed it was not there (note that this was my initial build).

It turns out you first have to place that line directly below the Program statement:

program TTClient;
{$R 'VersionInfo.res' 'VersionInfo.rc'}

... and not in the vicinity of your already present

{$R *.res}

Then you build your program once.

After that you can move the line back to a more logical place:

{$R *.res}
{$R 'VersionInfo.res' 'VersionInfo.rc'}

For some weird reason, once Delphi 'knows' that the .rc file is part of the project, it no longer matters if you either:

  • place the line anywhere else
  • have a .res file present or not

No need for a precompile step. If the .RC file is modified, the compiler will rebuild the .RES file, whether an earlier version existed or not.

This weird behavior does not really help when you set up this system initially ;-(

There are other strange things going on with the parsing of the project source and the construction of the .dproj file which brought me to this solution, notably:

If you rename the .rc file, this may get you into trouble again: There are remnants in the .dproj file still pointing to the old .rc file and the compiler will complain about not finding it. You have to edit this old name out of the .dproj file to fix this.

Note this was all under XE2, under other version YMMV.


Edited to add: You may still have to battle with XE2 Version Info Not Working issue.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!