Pascal 单位可以编译为可执行文件吗?

发布于 2024-12-09 17:51:30 字数 1266 浏览 4 评论 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 时,没有创建可执行文件,因为 Pascal 检测到它是一个单元。但我希望它除了库之外还可以是可执行文件。

$ ./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.

当我使用 fpc test.p 编译 test.p 时,生成的可执行文件结合了两个 begin/end 声明(而不是期望的行为)。

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

I like my libraries to double as executables. The desired behavior is:

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

How can I:

  • Get scriptedmain.p to compile into a scriptedmain binary?
  • Prevent test.p from running the code that's in scriptedmain.p's begin/end section?

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.

When I compile scriptedmain.p with fpc scriptedmain.p, no executable is created, because Pascal detects that it's a unit. But I want it to be an executable in addition to a library.

$ ./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.

When I compile test.p with fpc test.p, the resulting executable combines the two begin/end declarations (NOT the desired behavior).

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

╰つ倒转 2024-12-16 17:51:30

我不知道您使用的 Pascal 是什么风格,但某些变体支持使用 {$IFC condition} ... {$ENDC} 进行条件编译。您也许可以将其与编译时定义结合使用,以包含/排除给定版本中需要或不需要的代码。

I don't know what flavour of Pascal you're using but some variants support conditional compilation with {$IFC condition} ... {$ENDC}. You could perhaps use this in conjunction with a compile time define to include/exclude code that you need or don't need in a given version.

你的笑 2024-12-16 17:51:30

感谢 Free Pascal 邮件列表中的 Ager 和 Zirov,我能够用最少的技巧构建一个工作脚本化的主要示例。还发布在 RosettaCode 上。

Makefile:

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

Thanks to Ager and Zhirov in the Free Pascal mailing list, I was able to construct a working scripted main example with minimal hacks. Also posted on RosettaCode.

Makefile:

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.

Example:

$ make
$ ./scriptedmain 
Main: The meaning of life is: 42
$ make test
$ ./test 
Test: The meaning of life is: 42
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文