Forum > General
Universal Library Headers
Fred vS:
Hello.
Because of the maturity and recent optimization of fpc, there is no more reason that nearly all the good shared libraries are in C.
fpc libraries can do all what c libraries do, but even more...
For example, a C library can not dynamically load other libraries.
fpc can.
C libraries are system-specific while fpc libraries can be used in lot of systems, with the same code.
I went to several foreign-languge fora asking help to translate a Pascal header-type but with no luck... (C,C++, Java, Python,... no answers...)
So i will try here... :-[
That piece of code is the "classical-simple" way to dynamically load a external library:
--- Code: ---unit mylib_h /// the pascal header
interface
uses
DynLibs;
type
TProc = procedure ;
var
MyFunctionStr: function(Str: String): String; cdecl;
MyFunctionInt: function(Int: Integer): Integer; cdecl;
MyFunctionCar: function(Car: Cardinal): Cardinal; cdecl;
MyFunctionMulti : function(Car: cardinal; Str: String; Proc: Tproc): Integer; cdecl;
MyProc: procedure() ; cdecl;
MyProcArg: procedure(Car: cardinal); cdecl;
MyProcMulti : procedure(Car: cardinal; Str: String; Proc: Tproc); cdecl;
MyFunctionInit : function(str : String) : boolean ;
MyFunctionEnd : function() : boolean ;
function MyLibLoad(MylibFileName: String ; Ainit: string): integer;
procedure MyLibUnload() ;
implementation
function MyLibLoad(MylibFileName: String; Ainit : string): boolean;
begin
Result := False;
LibHandle := DynLibs.LoadLibrary(MylibFileName);
if LibHandle <> DynLibs.NilHandle then
begin
Pointer(MyFunctionStr) := GetProcAddress(LibHandle, 'MyFunctionStr');
Pointer(MyFunctionInt) := GetProcAddress(LibHandle, 'MyFunctionInt');
Pointer(MyFunctionCar) := GetProcAddress(LibHandle, 'MyFunctionCar');
Pointer(MyFunctionMulti) := GetProcAddress(LibHandle, 'MyFunctionStr');
Pointer(MyProc) := GetProcAddress(LibHandle, 'MyProc');
Pointer(MyProcArg) := GetProcAddress(LibHandle, 'MyProcArg');
Pointer(MyProcMulti) := GetProcAddress(LibHandle, 'MyProcMulti');
Pointer(MyFunctionInit) := GetProcAddress(LibHandle, 'MyFunctionInit');
Pointer(MyFunctionEnd) := GetProcAddress(LibHandle, 'MyFunctionEnd');
result := MyFunctionInit(Ainit) ;
end;
end;
procedure MyLibUnload();
begin
MyFunctionEnd();
if LibHandle <> DynLibs.NilHandle then
begin
DynLibs.UnloadLibrary(LibHandle);
LibHandle := DynLibs.NilHandle;
end;
end;
--- End code ---
I will be very happy is somebody could translate that code into C, Java, Python, Basic,...
And it will be very useful for other developers too.
Many thanks.
Leledumbo:
--- Quote ---For example, a C library can not dynamically load other libraries.
fpc can.
--- End quote ---
Actually, C can. But C doesn't have platform abstraction for this functionality in its standard libraries (neither standard Pascal does, but our beloved Modern Pascal implementation does have it).
--- Quote ---C libraries are system-specific while fpc libraries can be used in lot of systems, with the same code.
--- End quote ---
If you count #ifdef-#elif-#else-#endif, then C libraries can also be built on many platforms with the same code :P
--- Quote ---I will be very happy is somebody could translate that code into C, Java, Python, Basic,...
--- End quote ---
It can't. You have procedural variables returning and accepting Pascal strings. Other languages don't have any idea about its structure and there's a reference counting issue here. It won't even work correctly when called by Pascal programs, because string managers exist on both sides: the program and the library.
Fred vS:
@ Leledumbo => many thanks. ;)
--- Quote ---It can't. You have procedural variables returning and accepting Pascal strings
--- End quote ---
Aaaarg... :'(
But im sure It can't does not exist for fpc... :-X
So, to resume, procedural variables returning and accepting integer, float,... can do it.
Problem is with strings. And with characters or pointers ?
Leledumbo:
--- Quote ---But im sure It can't does not exist for fpc... :-X
--- End quote ---
Every language implementations do have limitations, we have to accept the facts.
--- Quote ---So, to resume, procedural variables returning and accepting integer, float,... can do it.
Problem is with strings. And with characters or pointers ?
--- End quote ---
With the automatic memory management. Most if not all language implementations understand and can call C functions, so if you want your library to be accessible by as many languages as possible, limit the interface to what C understands. It's however, perfectly possible and legal to export opaque pointer (and all required functionalities, especially (de)allocation) to be used by external code. Here's the relevant wiki article.
Fred vS:
@ Leledumbo, one more time you make my day... ;)
Excellent news and lets conquer the world... ;D
Navigation
[0] Message Index
[#] Next page