[fpc-pascal] How do I load external Pascal code?

Andrew Pennebaker andrew.pennebaker at gmail.com
Fri Oct 14 05:02:21 CEST 2011


I do know how to compile a basic Pascal program:

$ cat hello.p
program Hello;
begin
writeln('Hello World!')
end.

$ fpc hello.p

$ ./hello
Hello World!

The problem I'm trying to solve is that it appears Pascal can't build
executables from units, that is units by themselves.

$ cat scriptedmain.p
unit ScriptedMain;
interface

function MeaningOfLife () : integer;

implementation

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

$ fpc scriptedmain.p

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

I can USE the code just fine in a second file, test.p, and compile and run a
test executable. But I'd like to be able to turn my units into
self-sufficient executables (and later add command line options).

Cheers,

Andrew Pennebaker
www.yellosoft.us

On Thu, Oct 13, 2011 at 4:42 PM, Jonas Maebe <jonas.maebe at elis.ugent.be>wrote:

>
> On 13 Oct 2011, at 22:32, Andrew Pennebaker wrote:
>
> > On Thu, Oct 13, 2011 at 3:40 PM, Jonas Maebe <jonas.maebe at elis.ugent.be
> >wrote:
> >
> >> On 13 Oct 2011, at 21:37, Andrew Pennebaker wrote:
> >>
> >>> Is it possible to have a begin/end declaration in a unit a la C's main
> >>> function? I would like my units to double as libraries and executables.
> >>
> >> Yes: http://www.freepascal.org/docs-html/ref/refse72.html
> > Hmm. I tried rewriting begin/end as initialization/end, but
> scriptedmain.p
> > still won't compile to an executable. fpc just creates a .PPU file.
>
>
> As the above-mentioned page says (emphasis mine):
>
> ***
> A unit contains a set of declarations, procedures and functions that can be
> *used by a program* or another unit.
> ...
> The initialization parts of the units are executed in the order that the
> compiler loaded the units *when compiling a program*. They are *executed
> before the first statement of the program* is executed.
> ***
>
> Here's how you write a program:
> http://www.freepascal.org/docs-html/ref/refse71.html
>
> You may also want to go through the entire tutorial I linked to earlier:
> http://www.learn-programming.za.net/programming_pascal_learn01.html
>
>
> Jonas_______________________________________________
> fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org
> http://lists.freepascal.org/mailman/listinfo/fpc-pascal
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20111013/dbdc4d7d/attachment.html>


More information about the fpc-pascal mailing list