Recent

Author Topic: Compiling Basic Library Project in Linux Fails  (Read 10316 times)

aurawin

  • New Member
  • *
  • Posts: 18
    • http://www.aurawin.com/
Compiling Basic Library Project in Linux Fails
« on: December 14, 2008, 06:23:58 pm »
Is there a special thing I need to do to get Lazarus to build an SO on Linux?  

Installed Laz build : Current (SVN)
Installed FPC version : 2.3.1

Sample Code Generates Error

MatrixModule.lpr(21) Error: Can't call the linker, switching to external linking

---- Sample Code below ----

library MatrixModule;

{$mode objfpc}{$H+}

uses
  Classes
  { you can add units after this };

{$IFDEF WINDOWS}{$R MatrixModule.rc}{$ENDIF}

  procedure MyTest; export;
  begin
  end;

  exports MyTest;

begin

end.

aurawin

  • New Member
  • *
  • Posts: 18
    • http://www.aurawin.com/
Re: Compiling Basic Library Project in Linux Fails
« Reply #1 on: December 15, 2008, 01:29:03 am »
Quote from: "aurawin"
Is there a special thing I need to do to get Lazarus to build an SO on Linux?  


I got the SO to compile with the Compiler Options / Linker stuff all set to "smart" mode.  And it built.

TCBMyTest=function(Var Output:TCharArray):boolean;
Var
  MyTest:TCBMyTest;
  MyBuffer:TCharArray;

procedure TForm1.OnFormCreate();
begin
  FHandle:=LoadLibrary(/complete/path/test.so);
  Pointer(MyTest):=DynLibs.GetProcAddress(FLibHandle,'MyTest');
end;

procedure TForm1.Button1Click();
begin
  MyTest(MyBuffer);
  Label1.Caption:=toString(MyBuffer);
end;

procedure TForm1.Button2Click();
begin
  DynLibs.UnloadLibrary(FHandle);  // <--- Raises an External Exception
  //and Crashes Lazarus IDE and Application.
  // Exception Dialog Project "" raised exception class 'External:SIGSEGV'.
end;


This Exception is thrown even if I don't access MyTest method in the SO.

Any calling conventions that I am supposed to use?  Delphi would not have this problem  in Windoze.

aurawin

  • New Member
  • *
  • Posts: 18
    • http://www.aurawin.com/
Re: Compiling Basic Library Project in Linux Fails
« Reply #2 on: December 15, 2008, 02:58:10 am »
Quote from: "aurawin"
Quote from: "aurawin"

procedure TForm1.Button2Click();
begin
  DynLibs.UnloadLibrary(FHandle);  // <--- Raises an External Exception
  //and Crashes Lazarus IDE and Application.
  // Exception Dialog Project "" raised exception class 'External:SIGSEGV'.
end;


The reason the UnloadLibrary fails is because I'm using Dynamic Arrays.  Is there anyone who can tell me the unit I can include that will allow Dynamic Arrays to pass between Libraries and the Main Application?

Phil

  • Hero Member
  • *****
  • Posts: 2737
Re: Compiling Basic Library Project in Linux Fails
« Reply #3 on: December 15, 2008, 05:00:04 am »
Quote from: "aurawin"
Quote from: "aurawin"
Quote from: "aurawin"

procedure TForm1.Button2Click();
begin
  DynLibs.UnloadLibrary(FHandle);  // <--- Raises an External Exception
  //and Crashes Lazarus IDE and Application.
  // Exception Dialog Project "" raised exception class 'External:SIGSEGV'.
end;


The reason the UnloadLibrary fails is because I'm using Dynamic Arrays.  Is there anyone who can tell me the unit I can include that will allow Dynamic Arrays to pass between Libraries and the Main Application?


In general, you don't pass dynamic structures to and from a library. Stick to simple types like Integer, PChar, etc. Or rather, you can pass dynamic structures (for example, an object cast to an Integer or Pointer), but only work with it on one side of the divide.

If you follow this restriction, then you shouldn't have any trouble using a library compiled with Free Pascal, Delphi or C with an app compiled with any of these.

Thanks.

-Phil

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1927

aurawin

  • New Member
  • *
  • Posts: 18
    • http://www.aurawin.com/
Re: RE: Re: Compiling Basic Library Project in Linux Fails
« Reply #5 on: December 16, 2008, 05:42:35 am »
Quote from: "theo"
http://wiki.freepascal.org/shared_library
http://courses.cs.vt.edu/~cs3304/FreePascal/doc/prog/node13.html
And try with cdecl as calling convention.


I tried declaring both the SO and the App functions as cdecl and the load library works but GetProcAddress can't find the methods.  So I am forced to use stdcall - unless it's just a compiler option I need.

aurawin

  • New Member
  • *
  • Posts: 18
    • http://www.aurawin.com/
Can't Unload DLL - Bug ?
« Reply #6 on: December 16, 2008, 05:44:38 am »
Every time i use the Uses Classes in my Library project file I can load and Use the Library, but I can't unload it without the application crashing.

Is there something I need to set or call in my library?  Does it have to do with Initialize/Finalize?

I'm not using anything but bare function():boolean Test methods like Startup():boolean and Shutdown():boolean that do nothing but return the boolean value of true.

What's the problem?  Any ideas?  The SO unloads provided I don't use any uses in my uses clauses.

The only message I received from my console window was Segmentation fault right before the app blew out.

aurawin

  • New Member
  • *
  • Posts: 18
    • http://www.aurawin.com/
Re: [fpc-pascal] Problems with Dynlibs.UnloadLibrary on Linu
« Reply #7 on: December 19, 2008, 03:31:51 am »
For those who happen on the issue and are following it

As of today my understanding is that FPC's finalization method does something with the classes unit that causes this issue.  

I posted a bug report to see if I can get some help

http://mantis.freepascal.org/view.php?id=12814

I'm just trying to use classes in a library that I need to unload and possibly re-load if necessary.

 

TinyPortal © 2005-2018