Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2008-02

Externalization and inclusion of .PAS files (with GTK+) ?

3 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

T. Guilleminot

Hi,

I'd like to simplify my programs by creating a short MAIN.PAS file and several additional .PAS files called inside.
So I'd like to deport most of my code (procedures, functions...) to these external .pas files and include them into the main.pas, a kind of "include"-like in PHP.

I browsed the doc but between the "{$INCLUDE myfile1.pas}", the "uses" clause (...) I'm quite lost and it has never worked for me yet. As I code with GTK+ binding I also get GTK objects problem (eg : "unknown object"...).

Does anyone able to provide me a simple example to achieve this ? Thanks for any answer.
Tom

ik
to T. Guilleminot

Please don't think in PHP or C when you use Pascal... in C the compiler passes multiple times to be able to map between every line of code and locate things on other includes.

The pascal's compiler does not include full units inside a big unit. it includes sub implementations:

types.inc

type
  MyInt = type longint;
// end of  include file

main.pas

unit main;
interface
...
{$include types.inc}
...
implementation
...
end.

Please note that if you place things out of order, that is include/declear a function before you included types, and that function uses MyInt, the compiler will not be able to find the type you wish to use.

Ido

T. Guilleminot wrote 22 lines
Hi, I'd like to simplify my programs by creating a short MAIN.PAS file and several additional .PAS files called inside. So I'd like to deport most of my code (procedures, functions...) to these external .pas files and include them into the main.pas, a kind of "include"-like in PHP. I browsed the doc but between the "{$INCLUDE myfile1.pas}", the "uses" clause (...) I'm quite lost and it has never worked for me yet. As I code with GTK+ binding I also get GTK objects problem (eg : "unknown object"...). Does anyone able to provide me a simple example to achieve this ? Thanks for any answer. Tom _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Artur Stuczynski
to T. Guilleminot: I'd like to simplify my programs by creating a short MAIN.PAS file and several additional…

T. Guilleminot wites:

Show quoted text 6 lines
So I'd like to deport most of my code (procedures, functions...) to these external .pas files and include them into the main.pas, a kind of "include"-like in PHP. (...) Does anyone able to provide me a simple example to achieve this ? Thanks for any answer.

Probably most elegant (though not fully equivalent to PHP) solution I've seen is in fpc's units:

unit main;
interface
uses ... {all units required by main.pas and included.pas in interace part}
{$undef read_implementation}
{$define read_interface}
{$i included.pas}
{$undef read_interface}

implementation
uses ... {all units required by main.pas and included.pas in implementation part}

{$define read_implementation}
{$i included.pas}
{$undef read_implementation}
begin
end.

...

unit included;
{$ifdef read_interface}
//interface part goes here
{$endif read_interface}
{$ifdef read_implementation}
//implementation part goes here
{$endif read_implementation}

Hope this helps.