Include In Pascal?

Programming, for all ages and all languages.
Post Reply
embrance

Include In Pascal?

Post by embrance »

Is there a function to include files within Pascal code?
Example:
In C/C++ we use "#include"
Is there any similar function on Pascal?
I want to have the main file clear,and have the commands etc in others.
srg

Re:Include In Pascal?

Post by srg »

embrance wrote: Is there a function to include files within Pascal code?
Example:
In C/C++ we use "#include"
Is there any similar function on Pascal?
I want to have the main file clear,and have the commands etc in others.
From what I can remember (I havn't used Delphi/Pascal in a while)

In the Main file of a pascal program, you'd have:

program myprog;

Uses MyUnit1, MyUnit2, MyUnit3;

(There may not be the commas, I can't remember.)

The uses keywork is the pascal equivalent to #include.

BTW What pascal compiler are you using? Turbo, Delphi or FreePascal?

I'd recommend you to read this:
http://www.marcocantu.com/epascal/EssentialPascal.pdf

It's centered arround the Object Pascal in Delphi, but that's basically today's standard for Pascal.

srg
Adek336

Re:Include In Pascal?

Post by Adek336 »

Hm not sure about the format, but there is certainly a {$I} switch.

Code: Select all

#include "batteries.c"

{$I batteries.pas}
Note that this way, you will most likely include the file as a whole, will all functions whereas in the C style include, you only include the prototypes not the functions themselves. I am not sure but perhaps it may be done similarly under Pascal (procedure SaveTheWorld; extern; or sumfin).

Now the other, cleaner way are units.

Code: Select all

{batteries.pas}
unit batteries;

interface

{all the data you may want to be public, insert here}

var TimeToLive: longint;
procedure SaveTheWorld;

implementation

var TimeBeforeDeath: longing; {now this is private}

procedure SaveTheWorld;
var sockfd: integer;
   sin: sockaddr_in;
 begin
   sockfd:= socket(AF_INET, SOCK_STREAM, 0);
   sin.sin_port:=htons(666);
.....
 end;

end;{ end implementation}
begin
 writeln('here the code to be executed as the initialisation of the unit');
end. {batteries.pas}


{program.pas}
program prog1;
uses batteries;
 begin
  SaveTheWorld;
 end.
HTH & CCW
Schol-R-LEA

Re:Include In Pascal?

Post by Schol-R-LEA »

Keep in mind that the original Pascal standard didn't have any method for adding including external code at all; therefore, any of the techniques described are at least to some degree non-standard. That having been said, many extensions added by major implementations such as UCSD Pascal or Turbo Pascal are now suported by the majority of compilers.

Object Pascal is a different language, and has approximately the same relationship to Pascal that C++ has to C.

The [tt]{+I "filename"}[/tt] pragma, which IIRC was first added in UCSD Pascal (they didn't want to change the actual language, they designed the pragma features to work inside of comments, hence the brackets), performs a textual include in the same manner as C's [tt]#include[/tt] preprocessor directive. However, since there was at the time no standard support for separate compilation of subprograms, Pascal programmers never developed the convention of using includes only for function prototypes and type declarations - and it is a convention, not a feature of the C directive; technically, you can put anything in a C header file you want, it's simply a poor idea because it can cause multiple-definition problems at link time. See reply #3 in this thread, reply #13 in this thread, reply #10 in thread (discusses NASM's %include directive, but it works the same way), and reply #4 in this thread for more details.

The [tt]unit[/tt] and [tt]uses[/tt] syntax in Object Pascal (originally from later versions of Turbo Pascal, which heavily influences OP) works differently; AFAICT, the [tt]Interface[/tt] section is used to generate a definition file (or definition fields in the object files) which the compiler and linker use to verify the external references when a [tt]uses[/tt] call references the unit. This is analogous to the way [tt]EXPORT[/tt] and [tt]IMPORT[/tt] work in Modula-2, or package specifications in Ada (in Java, the same information is generated automatically from the class definition, and is part of the .CLASS file header data, which the [tt]import[/tt] directive references).

BTW, if you want a Wirth-type language that has these features as part of the standard language, you might want to take a look at Modula-2 and Oberon. Ada is similar in many ways, as well, but much more verbose and complex.
Post Reply