Recent

Author Topic: how to gain access to printf ?  (Read 2558 times)

440bx

  • Hero Member
  • *****
  • Posts: 4029
how to gain access to printf ?
« on: July 11, 2018, 02:41:42 pm »
Hello,

I am currently reading the Reference Manual and executing the examples that are presented there.  On page 196, there is an example to show how to use "array of const" with a C function.  The program is as follows:

Code: Pascal  [Select][+][-]
  1. program testaocc;
  2. {$mode objfpc}
  3. Const
  4. P : PChar = ’example’;
  5. Fmt : PChar =
  6. ’This %s uses printf to print numbers (%d) and strings.#10;
  7. // Declaration of standard C function printf:
  8. procedure printf (fm : pchar; args : array of const);cdecl; external ’c’;
  9. begin
  10. printf(Fmt,[P,123]);
  11. end.

The program compiles but won't load/run because "c.dll" is missing.  The complaint that c.dll is missing makes sense but, something tells me the example was meant to work and I may be missing something. 

My question is: what changes are needed in the above example to make it run ?

NOTE: I can get the example to work by modifying the printf declaration to be as follows:
Code: Pascal  [Select][+][-]
  1. const
  2.   user32 = 'user32.dll';
  3.  
  4.   procedure printf (Output,
  5.                     Format : pchar; args : array of const); cdecl;
  6.                     external user32 name 'wsprintfA';
but that seems too far from the original declaration to believe that was its original intent.

From what I've learned about FPC so far, I am under the impression that it statically links with a C library (I could easily be wrong about that).  Was the declaration in the example meant to provide access to the printf function which may be included in that library ?  (I'm just guessing...  just wondering what the intent of the declaration in the example is/was)

Thank you for your help.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11452
  • FPC developer.
Re: how to gain access to printf ?
« Reply #1 on: July 11, 2018, 03:33:38 pm »
That declaration is for *nix, so more the Kylix compatibility thingy. On *nix linking with C runtimes is more common.

this is the first question for it on Windows, what do you need this for ?  In general writeln and format is considered enough.

440bx

  • Hero Member
  • *****
  • Posts: 4029
Re: how to gain access to printf ?
« Reply #2 on: July 11, 2018, 06:50:15 pm »
That declaration is for *nix, so more the Kylix compatibility thingy. On *nix linking with C runtimes is more common.

this is the first question for it on Windows, what do you need this for ?  In general writeln and format is considered enough.

Thank you.  That makes perfect sense.   

I need it only to the extent that I want to be aware of all the compiler's capabilities.    FPC really has a nice set of features of its own.

(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

 

TinyPortal © 2005-2018