Sign in Join

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

Mailing lists / fpc-pascal / 2016-01

Problem with varargs directive

5 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

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

silvioprog

Hello,

The problem seems more related to Delphi, but I want to understand why it compiles only in FPC.

The declaration is just:

=== begin code ===
var
  FmtStr: function(const fmt: PAnsiChar): PAnsiChar; cdecl; varargs;
=== end code ===

And the full console project to test it in attachment.

It compiles only in FPC, in Delphi I got:

E2029 ',' or ':' expected but ';' found

Why? I don't know if it's a bug in Delphi or a breach in FPC.

ps. it compiles fine in FPC even using `$mode delphi`.

Silvio Clécio

Reply by email

Anthony Walter
to silvioprog

I believe in Delphi only external functions may use varargs. In your case you are declaring a variable, which could be assigned to a local function.

function FmtStr(const fmt: PAnsiChar): PAnsiChar; varargs; cdecl; external
yourlibname;

Note: varargs comes before cdecl, though I am not sure if it matters.

Reply by email

silvioprog
to Anthony Walter
Anthony Walter wrote
I believe in Delphi only external functions may use varargs. In your case you are declaring a variable, which could be assigned to a local function.

You are right:

http://docwiki.embarcadero.com/RADStudio/XE8/en/E2591_Only_cdecl_functions_may_use_varargs_(Delphi)

http://docwiki.embarcadero.com/RADStudio/XE8/en/E2277_Only_external_cdecl_functions_may_use_varargs_(Delphi)

So it seems that I can't use `varargs` loading libraries dynamically on Delphi. :-(

function FmtStr(const fmt: PAnsiChar): PAnsiChar; varargs; cdecl; external
yourlibname;

I need to use it in variables because I wanna load my library dynamically. :-/

Note: varargs comes before cdecl, though I am not sure if it matters.

It seems irrelevant, but I saw it in some Delphi units declaring it as `... cdecl; varargs; ...`.

Silvio Clécio

Reply by email

silvioprog
to silvioprog
silvioprog wrote 8 lines
On Mon, Jan 11, 2016 at 3:41 PM, Anthony Walter wrote: > I believe in Delphi only external functions may use varargs. In your case > you are declaring a variable, which could be assigned to a local function. > You are right:

[...]

Fixed. I just removed the first `;`:

=== begin code ===
var
  FmtStr: function(const fmt: PAnsiChar): PAnsiChar; cdecl varargs;
=== end code ===

Now it compiles fine in an original copy of Delphi Seattle and in the FPC from trunk (3.1.1).

I'm glad to compile that, but it is very strange, according EMB docs:

"The varargs directive works only with external routines and only with the cdecl calling convention.":

http://docwiki.embarcadero.com/RADStudio/Seattle/en/Procedures_and_Functions#External_Declarations

But I saw many programmers using it in funcs declared as variable:

http://www.swissdelphicenter.ch/torry/showcode.php?id=2114 https://github.com/e-tobi/Delphi-Libxml2-XmlTextReader/blob/master/libxml2.pas

So I'm going to use it too hehe! :-)

Silvio Clécio

Reply by email