Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2018-02

uses in 'filename'

8 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Mattias Gaertner

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?

Mattias

Michael Van Canneyt
to Mattias Gaertner
Mattias Gaertner wrote 9 lines
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.

Mattias Gaertner
to Michael Van Canneyt
[...] First of all, normally it won't work either way, because you must use "unit foo;" and "unit bar;" in the files.

No, I can use normal units. This works here:

---test1.pas---
uses foo in 'unit1.pas', unit1, bar in 'sub/unit1.pas';
Begin
  i:=3; // from unit1.pas
  foo.i:=foo.i+1; // from unit1.pas
  bar.j:=27; // from sub/unit1.pas
  writeln(foo.i,' ',unit1.i,' ',bar.j);
end.
---
---unit1.pas---
unit unit1;
interface
uses foo in 'sub/unit1.pas'; // unit1 uses another unit1
var i: integer;
implementation
end.
---
---sub/unit1.pas---
unit unit1;
interface
var j: integer;
implementation
end.
---

Mattias

Michael Van Canneyt
to Mattias Gaertner
Mattias Gaertner wrote 7 lines
On Thu, 22 Feb 2018 12:48:21 +0100 (CET) Michael Van Canneyt wrote: > [...] > First of all, normally it won't work either way, because you must use "unit foo;" and "unit bar;" in the files. No, I can use normal units. This works here:

Strange. I get the error I showed if I try that.

Michael.

Unknown author
to Michael Van Canneyt
Michael Van Canneyt wrote 8 lines
On Thu, 22 Feb 2018, Mattias Gaertner wrote: > On Thu, 22 Feb 2018 12:48:21 +0100 (CET) > Michael Van Canneyt wrote: >> First of all, normally it won't work either way, because you must use >> "unit foo;" and "unit bar;" in the files. >> > No, I can use normal units. This works here: Strange. I get the error I showed if I try that.

both of you are using the same mode? delphi or whatever? neither set of sample code shows a specific mode to use...

NOTE: No off-list assistance is given without prior approval. *Please keep mailing list traffic on the list unless* *a signed and pre-paid contract is in effect with us.*
Mattias Gaertner
to Unknown author
Show quoted text 13 lines
On 02/22/2018 09:49 AM, Michael Van Canneyt wrote: > On Thu, 22 Feb 2018, Mattias Gaertner wrote: >> On Thu, 22 Feb 2018 12:48:21 +0100 (CET) >> Michael Van Canneyt wrote: >>> First of all, normally it won't work either way, because you must use >>> "unit foo;" and "unit bar;" in the files. >> >> No, I can use normal units. This works here: > > Strange. I get the error I showed if I try that. both of you are using the same mode? delphi or whatever? neither set of sample code shows a specific mode to use...

It works here with both -Mdelphi and -Mobjfpc

Free Pascal Compiler version 3.0.4

Compiler date : 2017/08/05
Compiler CPU target: x86_64

Mattias

Mattias Gaertner
to Mattias Gaertner
Show quoted text 23 lines
On Thu, 22 Feb 2018 10:41:29 -0500 wrote: > On 02/22/2018 09:49 AM, Michael Van Canneyt wrote: > > On Thu, 22 Feb 2018, Mattias Gaertner wrote: > >> On Thu, 22 Feb 2018 12:48:21 +0100 (CET) > >> Michael Van Canneyt wrote: > >>> First of all, normally it won't work either way, because you must use > >>> "unit foo;" and "unit bar;" in the files. >> > >> No, I can use normal units. This works here: > > > > Strange. I get the error I showed if I try that. > > > both of you are using the same mode? delphi or whatever? neither set of sample > code shows a specific mode to use... It works here with both -Mdelphi and -Mobjfpc Free Pascal Compiler version 3.0.4 Compiler date : 2017/08/05 Compiler CPU target: x86_64

And it works with

Free Pascal Compiler version 3.1.1

Compiler date : 2018/01/15
Compiler CPU target: x86_64

Mattias

Mattias Gaertner
to Michael Van Canneyt: A feature. First of all, normally it won't work either way, because you must use "unit…
Show quoted text 18 lines
[...] 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:

This does not fit: Unit1.pp and unit1.pas.

Mattias