如何将FPC .o
从库链接到Delphi可执行文件。当我尝试链接以下代码时,我得到了一堆不满意的前向或外部声明。
library project1;
{$mode objfpc}{$H+}
uses
Classes
{ you can add units after this };
function Test: Integer;
begin
Result := -1;
end;
begin
end.
[dcc64 Error] Project2.dpr(170): E2065 Unsatisfied forward or external declaration: 'INIT$_$SYSTEM'
[dcc64 Error] Project2.dpr(170): E2065 Unsatisfied forward or external declaration: 'FINALIZE$_$OBJPAS'
[dcc64 Error] Project2.dpr(170): E2065 Unsatisfied forward or external declaration: 'INIT$_$LNFODWRF'
[dcc64 Error] Project2.dpr(170): E2065 Unsatisfied forward or external declaration: 'FINALIZE$_$LNFODWRF'
[dcc64 Error] Project2.dpr(170): E2065 Unsatisfied forward or external declaration: 'INIT$_$FPINTRES'
[dcc64 Error] Project2.dpr(170): E2065 Unsatisfied forward or external declaration: 'FINALIZE$_$WINDIRS'
[dcc64 Error] Project2.dpr(170): E2065 Unsatisfied forward or external declaration: 'SYSUTILS$_$TENCODING_$__$$_create'
[dcc64 Error] Project2.dpr(170): E2065 Unsatisfied forward or external declaration: 'SYSUTILS$_$TENCODING_$__$$_destroy'
[dcc64 Error] Project2.dpr(170): E2065 Unsatisfied forward or external declaration: 'INIT$_$SYSUTILS'
[dcc64 Error] Project2.dpr(170): E2065 Unsatisfied forward or external declaration: 'FINALIZE$_$SYSUTILS'
[dcc64 Error] Project2.dpr(170): E2065 Unsatisfied forward or external declaration: 'INIT$_$TYPINFO'
[dcc64 Error] Project2.dpr(170): E2065 Unsatisfied forward or external declaration: 'FINALIZE$_$TYPINFO'
[dcc64 Error] Project2.dpr(170): E2065 Unsatisfied forward or external declaration: 'INIT$_$CLASSES'
[dcc64 Error] Project2.dpr(170): E2065 Unsatisfied forward or external declaration: 'FINALIZE$_$CLASSES'
[dcc64 Error] Project2.dpr(170): E2065 Unsatisfied forward or external declaration: 'THREADVARLIST_$SYSTEM'
[dcc64 Error] Project2.dpr(170): E2065 Unsatisfied forward or external declaration: 'THREADVARLIST_$CLASSES'
[dcc64 Error] Project2.dpr(170): E2065 Unsatisfied forward or external declaration: 'RESSTR_$RTLCONSTS_$$_START'
[dcc64 Error] Project2.dpr(170): E2065 Unsatisfied forward or external declaration: 'RESSTR_$RTLCONSTS_$$_END'
[dcc64 Error] Project2.dpr(170): E2065 Unsatisfied forward or external declaration: 'RESSTR_$SYSCONST_$$_START'
[dcc64 Error] Project2.dpr(170): E2065 Unsatisfied forward or external declaration: 'RESSTR_$SYSCONST_$$_END'
[dcc64 Error] Project2.dpr(170): E2065 Unsatisfied forward or external declaration: 'FPC_LIBINITIALIZEUNITS'
答案 0 :(得分:6)
至少在写完之前,你很可能无法完成这项工作。未满足的声明来自FPC运行时。您还需要链接它,或者在Delphi中重新实现它。这两种选择都不太可行。
当然,如果您删除了对Classes
单元的引用,并将此简单函数放在单独的代码单元而不是库单元中,那么可能没有不满意的声明是合理的。也就是说,你肯定在探索这个,因为你想使用实际做某事的FPC代码。一旦你这样做,那么你将立即回到原点。
解决此问题的方法是动态链接到FPC代码。将FPC代码编译到库中并动态链接到该库。
为了好玩,我试图将FPC对象链接到Delphi程序。 FPC单位:
unit unit1;
interface
implementation
function Test(i: Integer): Integer; cdecl;
begin
Test := i*42;
end;
end.
我编译了这个:
fpc unit1.pp
然后我编写了以下Delphi程序来链接它:
{$APPTYPE CONSOLE}
{$L 'unit1.o'}
function Test(i: Integer): Integer; cdecl;
external name 'UNIT1_TEST$SMALLINT$$SMALLINT';
begin
Writeln(Test(666));
end.
输出:
27972
请注意,函数名称已修饰。为了找到我使用objdump
的名称:
>objdump -d unit1.o unit1.o: file format pe-i386 Disassembly of section .text.n_unit1_test$smallint$$smallint: 00000000 : 0: 55 push %ebp 1: 89 e5 mov %esp,%ebp 3: 83 ec 04 sub $0x4,%esp 6: 0f bf 45 08 movswl 0x8(%ebp),%eax a: 6b c0 2a imul $0x2a,%eax,%eax d: 66 89 45 fc mov %ax,-0x4(%ebp) 11: 66 8b 45 fc mov -0x4(%ebp),%ax 15: c9 leave 16: c3 ret ...
我使用x86版本的编译器完成了这项工作。我希望它在x64下也可行。
所以你确实可以链接FPC对象文件,只要它们足够简单。但是,如果您需要任何FPC运行时和标准单元,那么我预计它会变得太难。