PDA

View Full Version : Problems with dynamic linking



Ñuño Martínez
03-10-2008, 09:06 AM
Hello.

I'm trying to link a DLL dynamicly but I get some errors compiling with FPC, Delphi seems to work right. First I tried this (simplified):UNIT example;

INTERFACE
the_function: FUNCTION (arg1, arg2: INTEGER): INTEGER; CDECL;

IMPLEMENTATION

USES
UnitBase, { This unit loads the library at INITIALIZATION section. }
dynlibs;

INITIALIZATION
IF the_shared_library <> NilHandler THEN
the_function := GetProcedureAddress (the_shared_library, 'the_function');

FINALIZATION

END. This compiles on Delphi (with some hacks at "UnitBase" to keep compatibility) but on FPC it shows an error message telling that "have POINTER but FUNCTION (blah blah) expected" or similar.

Then I look at the GL.pas sources and I modified the INITIALIZATION section to
INITIALIZATION
IF the_shared_library <> NilHandler THEN
@the_function := GetProcedureAddress (the_shared_library, 'the_function'); which also compiles on Delphi but FPC returns "Can't assign a value to an address" but my OpenGL programs compiles and runs right both Linux and Windows. :?

How do I load a DLL/so/dylib dynamicaly?

Note that I haven't Internet connection at home so I did wrote all this by memory.

arthurprs
03-10-2008, 01:31 PM
i think freepascal requires a "@" before the function name

JSoftware
03-10-2008, 06:27 PM
FPC's gl.pas probably sets the mode to delphi compatibility. Either ways, to use your method just do:
pointer(the_function) := GetProcedureAddress (the_shared_library, 'the_function');

Ñuño Martínez
08-10-2008, 11:15 AM
FPC's gl.pas probably sets the mode to delphi compatibility. That was the problem. Just added {$MODE DELPHI} and it worked (I want to keep Delphi compatibility too).

Thanks. :)