FPC编译为静态库

时间:2016-07-11 04:33:32

标签: gcc linker freepascal

我有以下Pascal代码:

Library foo;

uses
  ctypes;

procedure Dummy; cdecl;
begin
end;

exports
  Dummy;

begin
end.

要将其编译为.o文件,我这样做:

ppcrossx64.exe -Cn -CcCDECL -O2 -Xs -XS -Xt foo.pas

它会创建一个foo.o和一个link.res文件。

然后我ar -q foo.a foo.o link.res创建foo.a。但是,如果我使用GCC链接到该文件(与我的C ++程序链接),则找不到Dummy符号。

FPC表示它与gcc的链接兼容。为什么我找不到符号?我究竟做错了什么?如果我没有指定-Cn,则会将其编译为有效的.dll。但是,我需要一个静态库。

编辑:它还会生成此批处理文件:

@echo off
SET THEFILE=C:\Users\Brandon\Desktop\foo.dll
echo Linking %THEFILE%
ld.exe -b pei-x86-64  --gc-sections  -s --dll  --entry _DLLMainCRTStartup   --base-file base.$$$ -o C:\Users\Brandon\Desktop\foo.dll link.res
if errorlevel 1 goto linkend
dlltool.exe -S as.exe -D C:\Users\Brandon\Desktop\foo.dll -e exp.$$$ --base-file base.$$$ 
if errorlevel 1 goto linkend
ld.exe -b pei-x86-64  -s --dll  --entry _DLLMainCRTStartup   -o C:\Users\Brandon\Desktop\foo.dll link.res exp.$$$
if errorlevel 1 goto linkend
goto end
:asmend
echo An error occured while assembling %THEFILE%
goto end
:linkend
echo An error occured while linking %THEFILE%
:end

双击它会创建一个有效的.dll文件。

1 个答案:

答案 0 :(得分:0)

我玩了一下,显然你需要声明它export或我们public name(或公共别名,如果你想要破损和非破坏的符号)

unit xx;
interface

uses
  ctypes;

procedure Dummy; cdecl;export;

implementation
procedure Dummy; cdecl; 

begin
end;


begin
end.
相关问题