2016-09-28 53 views
-2

我有一个用Sun Pascal编写的程序,它由一个程序单元和几个模块单元组成,我现在想将它转换为Free Pascal。 于是我开始通过测试孙帕斯卡尔3.0.2用户指南的例子(第52页,https://docs.oracle.com/cd/E19957-01/801-5054/801-5054.pdf):如何将Sun Pascal的模块编译为Free Pascal的等效模块?

程序单元:

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) 

什么更多的修改我应该做的工作,编制?

+0

尝试移植代码之前,您需要学习两种语言。这是你的下一步。 –

+0

$链接是用于目标文件而非源文件的 –

+0

并且由于缺少任何形式的声明部分(接口或实现),所示模块不是任何标准语法(既不是TP也不是ISO/Extended Pascal)。我查了太阳手册,它似乎是它的语法,但这不会被其他任何东西支持,因为它违反了基本的Pascal在使用原则之前声明。 –

回答

1

正如David Heffernan在他的评论中写道,你应该学习语言,但是,在这里,以及让你开始。

模块的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_unitmyprogram项目和建设。然后你可以从控制台/ shell运行程序。导航(使用cd)到编译目录,然后在Linux或OS X上输入./myprogram,或在Windows中输入myprogrammyprogram.exe

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