Recent

Author Topic: Create Lazarus share library in android  (Read 5185 times)

aradeonas

  • Hero Member
  • *****
  • Posts: 824
Create Lazarus share library in android
« on: November 11, 2015, 05:44:28 pm »
Hi,

How can I make a share library (.so) in Lazarus that do a simple task like sum two number and can be used in android?
Any one has an example or tutorial?

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1228
Re: Create Lazarus share library in android
« Reply #1 on: November 11, 2015, 11:38:21 pm »
hello,
i don't know if the following code works under android but it works under ubuntu :   

example
1 - create the shared library
Code: Pascal  [Select][+][-]
  1. library mySumlib;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   Classes;
  7.  
  8. function mySum(int1,int2 :integer): integer; cdecl;
  9. begin
  10.     result := int1 + int2 ;
  11. end;
  12.  
  13. exports
  14.   mySum;
  15. end.
compile :  a .so is created.
create a project to use the shared lib (my example use statically load library but you can use also l library dynamically load).
Code: Pascal  [Select][+][-]
  1. program tst_Sharedlib;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   Classes,sysutils;
  7.  
  8. function mySum(int1,int2: integer): integer; external 'mysumlib';
  9.  
  10. begin
  11.   writeln('sharedlib test');
  12.   writeln('the result of 323 + 593 is : ' + inttoStr(mySum(323,593)));
  13. end.
don't forget the option :
Compiler Options /Compilation and Linking

Enable setting: "Pass options to linker with "-k", delimiter is space" with this option   : -R ./    to indicate where is the library (path).
Compile and run
result   : 
Quote
sharedlib test
the result of 323 + 593 is : 916
 
« Last Edit: November 11, 2015, 11:41:56 pm by Jurassic Pork »
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

balazsszekely

  • Guest
Re: Create Lazarus share library in android
« Reply #2 on: November 12, 2015, 10:36:22 am »
In case you want to load it dynamically:
Code: Pascal  [Select][+][-]
  1. uses dynlibs, LCLType;
  2.  
  3. type
  4.   TmySum = function(Int1, Int2: Integer): Integer; cdecl;
  5.  
  6. var
  7.   LibHandle: TLibHandle = NilHandle;
  8.   mySum: TmySum = nil;
  9.  
  10.  
  11. function Load(const ALibName, AFuncName: string): Boolean;
  12. var
  13.  FarProc: TFarProc;
  14. begin
  15.   Result := False;
  16.   LibHandle := LoadLibrary(ALibName);
  17.   if LibHandle <> NilHandle then
  18.   begin
  19.     FarProc := GetProcedureAddress(LibHandle, AFuncName);
  20.     if FarProc <> nil then
  21.     begin
  22.       MySum := TMySum(FarProc);
  23.       Result := mySum <> nil;
  24.     end;
  25.   end;
  26. end;                
  27.  
  28. function UnLoad: Boolean;
  29. begin
  30.   Result := True;
  31.   if LibHandle <> NilHandle then
  32.   begin
  33.     Result := UnloadLibrary(LibHandle);
  34.     if Result then
  35.     begin
  36.       LibHandle := NilHandle;
  37.       mySum := nil;
  38.    end;
  39.   end;
  40. end;

Usage:
Code: Pascal  [Select][+][-]
  1. var
  2.  LibName: string;
  3. begin
  4.   LibName := ExtractFilePath(ParamStr(0)) + 'mySumlib.' + SharedSuffix; //you can change the path if necessary
  5.   if Load(LibName, 'mySum') then
  6.     ShowMessage(IntToStr(mySum(3, 4)));
  7.   UnLoad;
  8. end;

jmpessoa

  • Hero Member
  • *****
  • Posts: 2301
Re: Create Lazarus share library in android
« Reply #3 on: November 13, 2015, 01:09:58 am »

Hello aradeonas!

Here is a simple and complete  "Lamw: Lazarus Android Wizard"  NoGUI project...

ref.:  https://od.lk/f/Ml8xMDUyNjY3NDlf

Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

aradeonas

  • Hero Member
  • *****
  • Posts: 824
Re: Create Lazarus share library in android
« Reply #4 on: November 13, 2015, 11:23:37 am »
Hi jmpessoa and thanks.

Sadly I cant download it,can you upload this here?

jmpessoa

  • Hero Member
  • *****
  • Posts: 2301
Re: Create Lazarus share library in android
« Reply #5 on: November 13, 2015, 03:28:45 pm »

Sorry....

See Attachment ....



Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

aradeonas

  • Hero Member
  • *****
  • Posts: 824
Re: Create Lazarus share library in android
« Reply #6 on: November 13, 2015, 06:07:00 pm »
Thanks.
But one problem is that I cant make the app in Lazarus and I just want to write some stuff in a share library and use it in the Java Android project.

jmpessoa

  • Hero Member
  • *****
  • Posts: 2301
Re: Create Lazarus share library in android
« Reply #7 on: November 13, 2015, 07:16:57 pm »
Yes, is just about! You can learn from my project. Folder  ..\jni

1. From "*.lpi"  cross-compile settings... 

[change according to your system and "build"  to get the ".so" -->  ..\libs\armeabi ]

2. From "*.lpr"  JNI pascal side

3. From "*.java"  how to call/use your lib

NOTE:  you need a Lazarus prepared to cross-compile...

For windows there is  "Laz4Android":

ref:  http://sourceforge.net/projects/laz4android/files/?source=navbar

FPC: 3.0.0 rc2 (win32/arm-android/i386-android/jvm-android)
Lazarus:1.4.4
Android NDK: r10e (arm-linux-androideabi-4.9 + x86-4.9)

« Last Edit: November 14, 2015, 10:47:54 am by jmpessoa »
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

 

TinyPortal © 2005-2018