Search code examples
resourcesembedded-resourcelazarus

Lazarus resource - Using a resource leads to an error in linking


I searched around but found not any solution, so I try here:

I want to integrate images (I did it with delphi before in this way) that are used by my program and load them at runtime.

My idea was to 1.) create a resource file with

lazres <resourcename> <imagename>

2.) include the resource file in the source

{$R <resourcename>}

3.) compile Result:

Unable to find file "Debug: Trying to open file /medi/media/work/src.hg/ylazlib/test/test.src/libtest6.lpr".

The filename above is the project source and should be found. I found out that this error almost alwas results from invalid resource files. But the message should be explaining the problem in a better manner.

If I rename the resource file ( so it can not be found by linker) I get a 'correct' error message. In this case the unit that requires the resource is opened and the message states that the resource file can not be opened.

So what am I doing wrong here? (I also tried to insert images in different format to the resource - so the image itself may not the problem) any help apreciated - Thanks in advance Note: OS: debian sid [fpc 2.6.4] Lazarus SVN


Solution

  • The procedure to follow is a bit different than in Delphi. As explained here, you have to

    • add LResources to the uses clause.
    • include the res file produced by Lazres in the initialization section.
    • uses {$I myRes} rather than {$R myRes}.

    a quick test with

    console lzares cmd:

    C:\Users\me\Desktop\temp>lazres res.res project1.lpr=blah
    

    source code: unit Unit1;

    {$mode objfpc}{$H+}
    
    interface
    
    uses
      Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs,LResources;
    
    type
      TForm1 = class(TForm)
      private
        { private declarations }
      public
        { public declarations }
      end;
    
    var
      Form1: TForm1;
    
    implementation
    {$R *.lfm}
    
    initialization
    {$I res.res}
    
    end.
    

    compiles and runs fine. Then later this can be a bit tricky to load the res if they are raw data but for pictures it's quite straightforward, cf the example in the previous link.