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

Asked

Viewed 648 times

4

I’m new to Pascal and I’m looking for how to call functions or procedures from an existing archive in Pascal.

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

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

  • Using directives? {$I nomearquivo} or {$INCLUDE nomearquivo}?

  • I saw an example on the Internet, but I didn’t understand it very well. Could you explain to me what the command does (or give me a link that explains) and how to use and when to use? And its functionality?

  • I think I get it now. What you want is to use a library of external functions to your file. You want to make a beautiful module that performs a task, but you want to use external functions and procedures to your file, that’s it?

  • @Gabrieloshiro Yes!! Exactly what Julia does. = ) It is not necessary to program the functions in the terminal to use them, but

  • So I guess what you want is described in my second answer. Try running the example I posted the way it is, if it works, try to adapt to your program.

3 answers

4

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

unit testi;

interface

{$I *}

implementation

end.

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

The compiler will search your file in the following places:

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

Some care to be taken:

  • The name of unit has to be the same file name.
  • 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 the Turbo Pascal, you can include cross blocks. I mean, a file can open a Begin and in another file to be included close the block with End.
  • I made and gave the error of image. Here’s a image of my file I’m adding. Have any problems? Which ones? I did as I said and gave several errors.

  • Ah! I changed the folder to the Desktop, but presented the error, as you can see.

  • @Renan What is your file name? and what is the file name you want to include?

  • The name is Func.pas. I’m trying to compile with a Pascal compiler and not Delphi compiler. I do not want in my main file the procedures and functions that I will use in my program. I wish, then, to have a file with these functions (which in this case is Func.pas, but can be a totally meaningless file, because I never saw it done in Pascal, but only in Julia and I did the same or similar to Julia). I’ve been trying, somehow, to do what you and the little fellow have said, but I’m completely lost, since I’m talking about Pascal and not Delphi (I don’t understand).

  • @Renan: I saw the image you placed. The file name is Func.pas, but Unit’s name is just Func. The error says the symbol . (of .pas) is not valid there and in fact is not. Change the first line of Unit to unit Func;

  • I tried to do it and it didn’t work. I have Geany and FPC (by terminal) to compile. I don’t mind using either of them to compile, but I find Geany clearer, although there are no significant differences between them. Func.pas was an attempt by me to try to leave the file in a Delphi format, but I don’t think it will work. I’ll post the file I’m using and if you can fix it, I think better, but I don’t know. '-' Func.pas Func. . These are the program files.

  • I turned the file upside down and nothing. = S Still giving error in name. I think it may even be the compiler who does not understand Objectpascal (which is what you said is the language of Delphi, if I’m not mistaken).

  • I copied the entire programming from link and not yet compiled as much in the terminal as in Geany.

Show 3 more comments

2

A Delphi application is divided into Units. Each Unit is a language source code file Objectpascal (is not the default pascal), where several language constructs can be declared, such as isolated types and procedures. So that a Unit can have access to constructions declared in another, there is a session called uses. This session contains the list of which Units must be consulted so that the symbols are recognised and resolved by the compiler.

So if there are two Units declared (Unit1 and Unit2), why Unit1 make use of existing symbols in Unit2, is as set forth below.

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 guy named TClasse2 which will be used in Unit1 to declare another type, called TClasse1.

Unit1 needs to exist in a file called Unit1.pas and Unit2 needs to exist in a file called Unit2.pas.

  • @Renan on second thought, I think what you want is what Alexsc answered... you create an external module and use uses to call the reference. + 1 for you Alex ;-)

  • Okay... I don’t understand anything, because I don’t know where to put this in my code. I know nothing of Delphi, I thought to put Delphi tags, because I thought to draw the attention of those who know Delphi that maybe could answer my question about Pascal. I’m struggling here, but is this in Delphi right? Can this be implemented in an Pascal compiler? If so, how? What should I do?

  • @Renan: whether or not it can be implemented in a Pascal compiler will depend on this compiler. For example, if it’s a Turbo Pascal or a Freepascal, yes, the?

0


I’ll answer again with an idea clearer than me think that you want.

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

Library - with a single function that extracts a substring SubStr

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.
  • If that answer is more plausible, let me know that I delete the other.

  • I mysteriously managed to make it work. I’m glad I don’t have to be a "computer theorist". rs Using what you did I needed, in addition to defining the library, I needed to take the library file ("libNomeDaBiblioteca.so) and put it in the library of my operating system (which in this case is : /usr/lib). Vide link. I also managed to do it in a simpler way, using the Units you made at the beginning, but following the link.

  • Good luck in the continuation of your project ;-)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.