Skip to content

Duplicate external function is called incorrectly

The following program triggered a Runtime Error 207. While troubleshooting I came across the following: SDL_log exists twice in SDL3. One time it is used as log information output, the other time as a math function. The only subtle difference is on the C side, log is sometimes written in font and sometimes in lower case. But FPC calls the wrong function.

uses
  //  SDL3,
  ctypes;

  {$LinkLib 'SDL3'}

  // Mathe Funktion
  function SDL_log(x: cdouble): cdouble; cdecl; external;
  function SDL_cos(x: cdouble): cdouble; cdecl; external;

  // Log Info Ausgabe
  procedure SDL_Log(fmt: PChar); varargs; cdecl; external;

begin
  WriteLn(SDL_log(12.45));  // geht

  SDL_Log('%ix%i', 123, 456);  // Runtime 207
  SDL_Log('log 1');

  SDL_cos('abc');             // Error: Incompatible type for arg no. 1: Got "Constant String", expected "Double"
  SDL_cos(PChar('abc'));      // Error: Incompatible type for arg no. 1: Got "PChar", expected "Double"
  SDL_cos('%ix%i', 123, 456); // Error: Wrong number of parameters specified for call to "SDL_cos"
end.   

Once I remove the external math function it works as expected. So my guess is the FPC at "SDL_Log('%ix%i', 123, 456);" calls the math function. But this shouldn't actually be the case, since I don't have a cdouble but a PChar. I also have more than 1 argument, so it should actually be clear to the compiler which sdl_log it has to use

As a test I tried SDL_cos, and as expected the compiler asks who I want to give a PChar to. If I remove the line with SDL_Log and the Pchar, it sounds like SDL_cos.

More info: https://www.lazarusforum.de/viewtopic.php?f=19&t=15554

To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information