[fpc-pascal] uses in 'filename'

Michael Van Canneyt michael at freepascal.org
Thu Feb 22 12:48:21 CET 2018



On Thu, 22 Feb 2018, Mattias Gaertner wrote:

> Hi,
>
> FPC allows to use two different units with the same name:
>
> uses foo in 'unit1.pas', bar in 'sub/unit1.pas';
>
> Which might fail with linking errors.
>
> Is this a feature or a bug?

A feature.

First of all, normally it won't work either way, because you must use "unit foo;" and "unit bar;" in the
files. A quick test confirms this:

home:~/source/testsub> fpc a.pp
unit1.pas(1,9) Error: Illegal unit name: foo (expecting FOO)
unit1.pas(7,1) Fatal: There were 1 errors compiling module, stopping
Fatal: Compilation aborted

Where

a.pp:
-----
uses foo in 'unit1.pas', bar in 'sub/unit1.pas';

begin
   dofoo;
   dobar;
end.

Unit1.pp:
---------

unit foo;

interface

procedure dofoo;

implementation

procedure dofoo;

begin
   writeln('foo');
end;

end.


sub/unit1.pp:
-------------
unit bar;

interface

procedure dobar;

implementation

procedure dobar;

begin
   writeln('bar');
end;

end.

But if you tell the compiler to ignore the unit filenames (-Un) then you get

home:~/source/testsub> fpc -Un a.pp
home:~/source/testsub> ./a 
foo
bar

And then it works perfectly.

All assembler is prefixed using the declared
unit name:
         .file "unit1.pas"
# Begin asmlist al_procedures

.section .text.n_foo_$$_dofoo

etc.


Michael.




More information about the fpc-pascal mailing list