How to make an executable shared library with lazarus on linux ? lazarus / 32-Linux ---------------------------------------------------------------------- njlgg · 2017-12-19 01:11 On linux,some special shared libraries can be dynamically loaded by other applications, and they can also be executed . There are some ways of creating such kind of "Executable Library" with C or C++.       Now the question is:Is it possible and how to make an "Executable Library" with Lazarus on linux? ---------------------------------------------------------------------- Phil · 2017-12-19 01:14 Quote from: njlgg on December 19, 2017, 02:11:02 am      On linux,some special shared libraries can be dynamically loaded by other applications, and they can also be executed . There are some ways of creating such kind of "Executable Library" with C or C++.       Now the question is:Is it possible and how to make an "Executable Library" with Lazarus on linux? https://macpgmr.github.io/MacXPlatform/PascalDynLibs.html ---------------------------------------------------------------------- njlgg · 2017-12-19 02:02 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. ---------------------------------------------------------------------- Phil · 2017-12-19 03:05 Quote from: njlgg on December 19, 2017, 03:02:03 amNOTE 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. That works fine here on both Windows and Linux (Ubuntu). On Mac I get a compile error, so it's not a universal thing: /Users/phil/Tools/laztests/testexeclib/project1.lpr(15,1) Warning: (3186) Use of unsupported feature! /Users/phil/Tools/laztests/testexeclib/project1.lpr(15,1) Fatal: (2003) Syntax error, "BEGIN" expected but "EXPORTS" found Not sure why you would want a program executable that could also be loaded as a dynamic library. I would separate out the non-UI code and put it in a dynamic library and leave the UI code in the program. ---------------------------------------------------------------------- njlgg · 2017-12-19 04:58 Quote from: Phil on December 19, 2017, 04:05:30 am That works fine here on both Windows and Linux (Ubuntu). On Mac I get a compile error, so it's not a universal thing:     MyLib := LoadLibrary(forms.Application.ExeName);     FsayHello:=sayHello( GetProcedureAddress(MyLib,'sayHello') );     ShowMessage( FsayHello('njlgg'));     The above code does work fine on Linux(Ubuntu),but it's a special occasion.     Mylib:=LoadLibrary(forms.Application.ExeName) will always get a value "0",that means the executable program not really been loaded as library .So if you try to load the executable program as library in another program,not within itself , error occurs while processing GetProcedureAddress(MyLib,'sayHello').     if we try to load the executable program as library from java code like this:      static {          System.load("/home/njlgg/MyDevelop/SoLibraryTest/libTest");     }      We will get an error:cannot dynamically load executable. The problem is not solved yet. ---------------------------------------------------------------------- Thaddy · 2017-12-19 08:17 If you do not specify PIC there is no chance that is works on Linux. Library code *must* be relocatable. ---------------------------------------------------------------------- njlgg · 2017-12-19 09:07 Quote from: Thaddy on December 19, 2017, 09:17:14 amIf you do not specify PIC there is no chance that is works on Linux. Library code *must* be relocatable. I specify -fpic in Project options->compiler options->custom options,but it doesn't work.Besides, another opition -cd (create also dynamic library is displayed "not supported").Is that the reason a program can't be also compiled into a dynamic library? ---------------------------------------------------------------------- Чебурашка · 2022-08-24 14:39 Hello, for what I understand, the OP question has not been answered yet. Is is possible with fpc to get a linux shared library that is also executable? gcc allows this with the options "-fPIC" "-pie" according to this post https://unix.stackexchange.com/questions/223385/why-and-how-are-some- shared-libraries-runnable-as-though-they-are-executables Thanks ---------------------------------------------------------------------- Fred vS · 2022-08-24 15:01 Quote from: tt on August 24, 2022, 04:39:56 pmHello, for what I understand, the OP question has not been answered yet. Is is possible with fpc to get a linux shared library that is also executable? gcc allows this with the options "-fPIC" "-pie" according to this post https://unix.stackexchange.com/questions/223385/why-and-how-are-some- shared-libraries-runnable-as-though-they-are-executables Thanks Hello. You may "hardening" your executable that will be transformed into a library runnable. Just add those parameters for compilation: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i -Cg -k-pie -znowhttps://wiki.freepascal.org/hardening