How to Link object file in Delphi over Linux Platform?

Viewed 172

I am using object file in my Delphi application over Linux Platform and linked them as shown in below piece of code and getting error:

"E2065 Unsatisfied forward or external declaration: 'add'"

Delphi:

    program ITP_Test;
    
    {$APPTYPE CONSOLE}
    
    {$R *.res}
    
    uses
      System.SysUtils;
    
    {$LINK 'hello.o'}
    
    function add(a ,b : Integer): Integer; cdecl; external;
    
    begin
      Writeln(add(2,7));
    end.

hello.c
    
    #include<stdio.h>
    #ifdef __cplusplus
    extern "C"
    {
    #endif
    int add(int a, int b)
    {
        return a + b;
    }
    #ifdef __cplusplus
    }
    #endif

compiled hello.c using command:

'gcc -c hello.c'

gcc version : 7.5.0 Linux version: Ubuntu 18.04

1 Answers

I have found the solution in Delphi documentation.

The syntax for your example is:

function add(a, b : Integer): Integer; cdecl; external object 'hello.o' name 'add';

There is no need to use the $LINK compiler directive.

So the full code for your example becomes:

program LinuxExternalHello;

{$APPTYPE CONSOLE}
{$R *.res}

uses
  System.SysUtils;

function add(a, b : Integer): Integer; cdecl; external object 'hello.o' name 'add';

begin
  Writeln(add(2,7));
end.

You hello.c and the way to compile it is perfectly correct.

The alternative described by Marion Candau in his YouTube video is still valid but requires one more step to build the archive file.

Related