2011-10-13 88 views
0

我喜歡我的圖書館作爲可執行文件加倍。期望的行爲是:帕斯卡單位可以編譯爲可執行文件嗎?

$ ./scriptedmain 
Main: The meaning of life is: 42 
$ ./test 
Test: The meaning of life is: 42 

如何我:

  • 獲取scriptedmain.p編譯成二進制scriptedmain
  • 阻止test.p運行scriptedmain.pbegin/end節中的代碼?

scriptedmain.p:

unit ScriptedMain; 
    interface 

    function MeaningOfLife() : integer; 

    implementation 

    function MeaningOfLife() : integer; 
    begin 
     MeaningOfLife := 42 
    end; 
begin 
    write('Main: The meaning of life is: '); 
    writeln(MeaningOfLife()) 
end. 

當我編譯fpc scriptedmain.p scriptedmain.p,沒有可執行文件被創建,因爲帕斯卡爾檢測到它是一個單元。但我希望它是一個除了庫之外的可執行文件。

$ ./scriptedmain 
-bash: ./scriptedmain: No such file or directory 

test.p:

program Test; 
uses 
    ScriptedMain; 
begin 
    write('Test: The meaning of life is: '); 
    writeln(MeaningOfLife()) 
end. 

當我編譯test.p與fpc test.p,生成的可執行文件組合這兩個begin/end聲明(不是所需的行爲)。

$ ./test 
Main: The meaning of life is: 42 
Test: The meaning of life is: 42 
+1

那麼,使用一個空的主程序? –

回答

0

感謝Ager和Zhirov在免費Pascal mailing list中,我能夠構建一個工作腳本主要示例,只需很少的黑客操作。也發佈在RosettaCode

生成文件:

all: scriptedmain 

scriptedmain: scriptedmain.pas 
    fpc -dscriptedmain scriptedmain.pas 

test: test.pas scriptedmain 
    fpc test.pas 

clean: 
    -rm test 
    -rm scriptedmain 
    -rm *.o 
    -rm *.ppu 

scriptedmain.pas:

{$IFDEF scriptedmain} 
program ScriptedMain; 
{$ELSE} 
unit ScriptedMain; 
interface 
function MeaningOfLife() : integer; 
implementation 
{$ENDIF} 
    function MeaningOfLife() : integer; 
    begin 
     MeaningOfLife := 42 
    end; 
{$IFDEF scriptedmain} 
begin 
    write('Main: The meaning of life is: '); 
    writeln(MeaningOfLife()) 
{$ENDIF} 
end. 

test.pas:

program Test; 
uses 
    ScriptedMain; 
begin 
    write('Test: The meaning of life is: '); 
    writeln(MeaningOfLife()) 
end. 

實施例:

$ make 
$ ./scriptedmain 
Main: The meaning of life is: 42 
$ make test 
$ ./test 
Test: The meaning of life is: 42 
0

我不知道你使用的是什麼味道帕斯卡,但一些變種支持與{$IFC condition} ... {$ENDC}條件編譯。您可以將此與編譯時定義一起使用,以包含/排除您在給定版本中需要或不需要的代碼。

+0

如果有幫助,我正在使用[Free Pascal](http://www.freepascal.org/)。 – mcandre

+0

我對Free Pascal並不熟悉,但您應該能夠檢查文檔並查看它是否支持條件編譯。 –

+0

它,甚至多種類型。 Turbo Pascal,Delphi,Mac Pascal/Codewarrior,你想要什麼。 –

相關問題