2016-09-28 35 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。它使得添加單位,編輯程序和許多其他事情變得更容易。