Sign in Join

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

Mailing lists / fpc-pascal / 2023-07

Documentation or compiler bug?

6 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

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

Hairy Pixels

I get the error "Procedure directive "CDECL" cannot be used with "EXTERNAL"" when I use "cdecl" with external modifiers yet the docs at https://www.freepascal.org/docs-html/ref/refsu73.html say":

When compiling this, and linking to the C-library, the strlen function can be called throughout the program. The external directive tells the compiler that the function resides in an external object file or library with the “strlen” name (see 14.7).

Suggesting they can be used together, so which is right? Compiler is: Free Pascal Compiler version 3.3.1 [2023/03/03] for aarch64

Regards,
Ryan Joseph

Michael Van Canneyt
to Hairy Pixels
Hairy Pixels wrote 5 lines
I get the error "Procedure directive "CDECL" cannot be used with "EXTERNAL"" when I use "cdecl" with external modifiers yet the docs at https://www.freepascal.org/docs-html/ref/refsu73.html say": When compiling this, and linking to the C-library, the strlen function can be called throughout the program. The external directive tells the compiler that the function resides in an external object file or library with the “strlen” name (see 14.7). Suggesting they can be used together, so which is right? Compiler is: Free Pascal Compiler version 3.3.1 [2023/03/03] for aarch64

They are used together. The FPC sources are full of them, most import libraries work like that:

function PQconnectStart(conninfo:Pchar):PPGconn;cdecl;external External_library name 'PQconnectStart';

It would be helpful if you would give the exact statement you are writing.

Michael.

Hairy Pixels
to Michael Van Canneyt

In fact I filed a bug today which was related. Notice how I didn't use cdecl in the program but another user tried to compile and had problems with cdecl also.

https://gitlab.com/freepascal.org/fpc/source/-/issues/40342 <https://gitlab.com/freepascal.org/fpc/source/-/issues/40342#note_1462402401>

Basically this program:

{$mode objfpc}
program test;
function printf(format: PChar): Integer; external; cdecl; varargs;
begin
end.
Show quoted text 8 lines
On Jul 8, 2023, at 2:12 PM, Michael Van Canneyt via fpc-pascal wrote: They are used together. The FPC sources are full of them, most import libraries work like that: function PQconnectStart(conninfo:Pchar):PPGconn;cdecl;external External_library name 'PQconnectStart'; It would be helpful if you would give the exact statement you are writing.

Regards,
Ryan Joseph

Michael Van Canneyt
to Hairy Pixels
Hairy Pixels wrote 11 lines
In fact I filed a bug today which was related. Notice how I didn't use cdecl in the program but another user tried to compile and had problems with cdecl also. https://gitlab.com/freepascal.org/fpc/source/-/issues/40342 <https://gitlab.com/freepascal.org/fpc/source/-/issues/40342#note_1462402401> Basically this program: {$mode objfpc} program test; function printf(format: PChar): Integer; external; cdecl; varargs;

You must put the cdecl (which is the calling convention) before the external:

{$linklib c}
function printf(format: PChar): Integer; cdecl; external; varargs;
begin
   printf('%d is the answer'#10,42)
end.

Will work and will print

42 is the answer

Michael.

Michael Van Canneyt
to Michael Van Canneyt
Michael Van Canneyt wrote 10 lines
On Sat, 8 Jul 2023, Hairy Pixels via fpc-pascal wrote: > In fact I filed a bug today which was related. Notice how I didn't use > cdecl in the program but another user tried to compile and had problems > with cdecl also. > > https://gitlab.com/freepascal.org/fpc/source/-/issues/40342 > <https://gitlab.com/freepascal.org/fpc/source/-/issues/40342#note_1462402401>

Small addition:

He did not have problems with cdecl, but with the fact that the c library was not linked. But this was intended: he wrote it explicitly 'as intended'. So all is well, except the internal error (AFAIK you cannot pass a record to printf())

Just so you have a correct picture of what is happening.

Michael.

Hairy Pixels
to Michael Van Canneyt: You must put the cdecl (which is the calling convention) before the external: {$linklib c}
On Jul 8, 2023, at 3:57 PM, Michael Van Canneyt via fpc-pascal wrote: You must put the cdecl (which is the calling convention) before the external:

ohhhh so the order is the problem. That error message is misleading and should be changed. I know c library functions are external and cdecl but that error really confused me.

As for the internal error that still should't happen but I would have gotten the wrong result anyway if it had run. Thanks for clarifying.

Regards,
Ryan Joseph