How to include functions or procedures in Pascal through an existing file?

4

I'm new to Pascal and am looking to call functions or procedures from an existing Pascal file.

There is a function in Julia called include('filename.jl') where I add functions already written in a notepad made earlier so that I can use them while I'm running the compiler.

I wonder if there is such an application in Pascal. If so, how should I write to the text file? Could you give me a clear example of your application? I've been looking for a good few hours to do this, but nothing satisfying.

    
asked by anonymous 05.05.2015 / 05:07

3 answers

0

I'll respond again with a clearer idea of what I think you want.

You can use a libraries file of functions and procedures. For example:

Library - with a single function that extracts a SubStr substring

library subs; 

function SubStr(CString: PChar;FromPos,ToPos: Longint): PChar; cdecl; 

var 
  Length: Integer; 

begin 
  Length := StrLen(CString); 
  SubStr := CString + Length; 
  if (FromPos > 0) and (ToPos >= FromPos) then 
  begin 
    if Length >= FromPos then 
      SubStr := CString + FromPos - 1; 
    if Length > ToPos then 
    CString[ToPos] := #0; 
  end; 
end; 

exports 
  SubStr; 

end.

Using the library

program testsubs; 

function SubStr(const CString: PChar; FromPos, ToPos: longint): PChar; 
  cdecl; external ’subs’; 

var 
  s: PChar; 
  FromPos, ToPos: Integer; 
begin 
  s := ’Test’; 
  FromPos := 2; 
  ToPos := 3; 
  WriteLn(SubStr(s, FromPos, ToPos)); 
end.
    
06.05.2015 / 19:18
4

You can use build directives. Example taken from the Free Pascal documentation.

unit testi;

interface

{$I *}

implementation

end.

In this example, the compiler will look for a file named testi or testi.pp and will "paste" where the {$I} compilation directive is.

The compiler will look for your file in the following places:

  • Location specified in the policy
  • in the current directory
  • all directories specified in the search directory (you can add directories to the search directory using the -Fi option of the command line)

Some precautions to take:

  • The name of unit must be the same as the name of the file.
  • You can include files within other files, but not infinitely. The maximum number is restricted by the number of file descriptors available to the Free Pascal compiler.
  • Unlike Turbo Pascal, you can include cross blocks . That is, a file can open Begin and in another file to be included close the block with End .
05.05.2015 / 07:25
2

An application in Delphi is divided into units . Each unit is a source file in ObjectPascal language (not the default pascal), where several language constructs can be declared, such as isolated types and procedures. In order for a unit to have access to declared constructs in another, there is a session called uses . This section contains a list of which units to query for symbols to be recognized and resolved by the compiler.

So if there are two units declared ( Unit1 and Unit2 ), so that Unit1 makes use of existing symbols in Unit2 , it is done as follows.

Unit1

unit Unit1;

interface

uses
  Unit2;

type
  TClasse1 = class(TClasse2)
  end;

implemetation

end.

Unit2

unit Unit2;

interface

type
  TClasse2 = class
  end;

implementation

end.

In this example, Unit2 declares a type called TClasse2 that will be used in Unit1 to declare another type, called TClasse1 .

Unit1 must exist in a file named Unit1.pas and Unit2 must exist in a file named Unit2.pas .

    
05.05.2015 / 16:10