This article is mainly about compiling a shared library and how to inter-act with other languages.It does not get the key point of the question.
Let me introduce the key point by some pieces of source code on windows.
*************project source begin**********
program libTest;
{$mode objfpc}{$H+}
uses
{$IFDEF UNIX}{$IFDEF UseCThreads}
cthreads,
{$ENDIF}{$ENDIF}
Interfaces, // this includes the LCL widgetset
Forms, main
{ you can add units after this };
{$R *.res}
exports
sayHello;
begin
RequireDerivedFormResource:=True;
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.
*************project source end**********
*************unit source begin**********
....
function sayHello(msg:String):String;export;stdcall;
begin
result:='this is from libTest';
end;
{ TForm1 }
procedure TForm1.Button1Click(Sender: TObject);
type
sayHello = function(msg:String): String;stdcall;
var
MyLib: TLibHandle = dynlibs.NilHandle;
FsayHello:sayHello;
begin
//Please note here that we use Loadlibrary to load the program itself as a library
MyLib := LoadLibrary(forms.Application.ExeName);
FsayHello:=sayHello( GetProcedureAddress(MyLib,'sayHello') );
ShowMessage( FsayHello('njlgg'));
end;
......
*************unit source end**********
NOTE the project type is "program",not "library",the program exports a function "sayHello",and we use Loadlibrary to load the program itself as a library in the unit code.
On windows,it works as expected,but on linux,if we define the project type as "program",we get an executable file,we can't load an executable as library ,or on the other hand ,if we define the project type as "library",we get a shared library,it can inter-act with other languages correctly ,but we can't run the library seperately like an executable program.
So the key point is How to make an "Executable Library" on linux.