如何将Sun Pascal的模块编译为Free Pascal中的等效模块?

时间:2016-09-28 13:07:31

标签: pascal porting freepascal

我有一个用Sun Pascal编写的程序,它由一个程序单元和几个模块单元组成,我想现在将它转换为Free Pascal。 所以我开始测试Sun Pascal 3.0.2用户指南中的示例(第52页,https://docs.oracle.com/cd/E19957-01/801-5054/801-5054.pdf):

程序单位:

program program_unit (output);
procedure say_hello; extern;
begin
say_hello
end.

模块单位:

module module_unit;
procedure say_hello;
begin
writeln ('Hello, world.')
end;

我对源文件做了一些修改:在program_unit中,我添加一行“{$ link program_unit.p}”,然后将修饰符“extern”更改为“external”。

然后我尝试使用fpc编译它:

fpc program_unit.p

但失败了:

Free Pascal Compiler version 2.6.2-8 [2014/01/22] for x86_64
Copyright (c) 1993-2012 by Florian Klaempfl and others
Target OS: Linux for x86-64
Compiling program_unit.p
Linking program_unit
/usr/bin/ld.bfd: warning: link.res contains output sections; did you forget -T?
module_unit.p: file not recognized: File format not recognized
program_unit.p(6,1) Error: Error while linking
program_unit.p(6,1) Fatal: There were 1 errors compiling module, stopping
Fatal: Compilation aborted
Error: /usr/bin/ppcx64 returned an error exitcode (normal if you did not specify a source file to be compiled)

我应该做些什么修改才能使编译工作?

1 个答案:

答案 0 :(得分:1)

大卫·赫弗南在评论中写道,你应该学习这门语言,但是,这就是为了让你开始学习。

模块的FreePascal等价物是一个单位。主程序不是一个单元,它是,程序(主项目文件)。所以让你的程序看起来像:

program myprogram; // (output) is not required.

uses
  module_unit;

begin
  say_hello
end.

和你的module_unit一样:

unit module_unit;

{$mode objfpc}{$H+}

interface

procedure say_hello;

implementation

procedure say_hello;
begin
  Writeln('Hello, world.')
end;

end.

现在只需将module_unit添加到myprogram项目并进行构建即可。然后,您可以从控制台/ shell运行该程序。导航(使用cd)到编译它的目录,然后在Linux或OS X上输入./myprogram,或在Windows中输入myprogrammyprogram.exe

FWIW,我建议您使用Lazarus IDE,而不是命令行。它使添加单位,编辑程序和许多其他事情变得更加容易。