2

I want to have a structure like /project/functions /project/src. I would have a unit /project/functions/helloUnit.pas similar to:

unit helloUnit;
interface
implementation
uses classes;
procedure helloWorld(output);
begin
  writeln('Hello, World');
end.

And a file /project/src/main.pas like:

uses helloUnit;
Begin
  helloWorld;
End.

I've been trying to make it work but haven't found the way. I'm using fpc compiler on linux command line in case I'm not defining the path or something.

LU RD
  • 34,438
  • 5
  • 88
  • 296
  • Sorry, maybe I close with not good link, please check https://stackoverflow.com/questions/30813088/how-to-get-scalar-value-on-a-cell-using-conditional-indexing – jezrael Nov 27 '20 at 12:06

1 Answers1

2

There are some things wrong with your example code, which it would be helpful to fix before attempting to compile it from the command line.

Firstly, you need a project file for fpc to compile to produce your program. For this, I created this file in the projects folder and named it hello.lpr:

program Hello;

uses
  hellounit,
  main;

begin
  HelloWorld;
end.

Then correct Main.Pas as follows:

unit main;

interface

uses helloUnit;

procedure SayHello;

implementation

procedure SayHello;
Begin
  helloWorld;
End;
End.

Note that procedure SayHello; has been added to the interface section and that End; has been added to the end of the declaration of the procedure. The End. tells the compiler that it has reached the end of the source code in the unit.

Next correct helloUnit as follows

unit helloUnit;

interface

procedure helloWorld;

implementation

uses classes;

procedure helloWorld;
begin
  writeln('Hello, World');
end; {added}

end.

Now write a batch file CompileHello.Bat (assuming Windows) in the projects folder containing

D:\Lazarus2\fpc\3.0.4\bin\i386-win32\fpc -Fufunctions;src hello.lpr

and then run it from a CMD prompt in the projects folder. The -Fu compiler switch tells fpc where to find units which are not located in the same folder as the project file. The paths after -Fu can be relative to the project folder. If you have followed the steps above, it should comile successfully.

If you were using Lazarus, the companion IDE to fpc there would be at least two ways of doing what you asked.

  1. With HelloUnit open in the IDE, use View | Add Editor File to Project from the IDE's menu to add HelloUnit to the project. This is probably the best way because it unambiguously identifies HelloUnit for the project.

  2. Add the folder in which HelloUnit.Pas is located to the Project's Source Paths using Project | Options and, in the pop-up, under Compiler Options, add the folder to the Other Unit Files box on the RHS.

MartynA
  • 30,454
  • 4
  • 32
  • 73
  • 1
    I don't know what IDE you are referring to. I want to know how to do it with fpc from the command line. –  Apr 04 '20 at 20:42
  • 1
    Oh sorry, I assumed you would have Lazarus, the free and very good IDE for FPC. Not using it is avoidably making like difficult for yourself. Anyway, your choice. Follow this link: https://www.freepascal.org/docs-html/current/user/usersu7.html to see how to add HelloUnit's folder to the Project search path. – MartynA Apr 04 '20 at 20:50
  • Well this was actually for a script of a program that uses pascal syntax and I can't change the compiler path from the program so I guess I'll have to flatten the directory structure to be able to access the settings file. I expected to be able to indicate the path to import modules something like `use this from ../functions' inside the same script. I'll try the IDE for debugging anyways. –  Apr 05 '20 at 10:34