How to statically link C to Delphi?

How can I statically link C with Delphi? Delphi threw out an error saying:

[dcc32 Error] Project1.dpr (15): E2065 Unsatisfactory forward or external declaration: 'Test'

The C compiler is Visual C ++ with the COFF object file.

Delphi:

program Project1;

{$L C:\Source.obj}

function Test(): Integer; cdecl; external;

begin
  WriteLn(Test);
end.

FROM

extern "C" int __cdecl Test()
{
    return 12;
}

int main()
{
    return 0;
}
+4
source share
1 answer

It depends on the decoration of the name used by any C compiler that you use. For example, the 32-bit bcc32 compiler will decorate Testhow _Test. Therefore, the Delphi code to link to it should be:

function Test(): Integer; cdecl; external name '_Test';

, , . , C obj .

, ++, C.

extern "C" 

C. C. .cpp .c , C.

C, malloc , System.Win.Crtl Delphi.

, , , , main C-. C- C, , , . . C Delphi. main Delphi, .

C main

int main(void)

, C ​​:

int __cdecl Test(void)

, __cdecl , :

int Test(void)

:

int Test(void)
{
    return 12;
}

, C ++. , , MSVC, :

cl /c source.c

Delphi

{$APPTYPE CONSOLE}

{$L Source.obj}

function Test: Integer; cdecl; external name '_Test';

begin
  WriteLn(Test);
end.

12
+5

Source: https://habr.com/ru/post/1612602/


All Articles