Recent

Author Topic: LAMW: Is is possible to call another shared library (lib.so) directly from LAMW?  (Read 444 times)

Manlio

  • Jr. Member
  • **
  • Posts: 57
  • Pascal dev
Is it possible to call the methods of another shared library from LAMW's Pascal, without having to go through Java?

For example, if I have a shared library (e.g. libexample.so) and I want to use some methods in it, my understanding is that I have to open the library first (in Java):

    System.loadLibrary("example");

and I have to declare the method(s) I want to use:

    public native String myMethodInExample();

and then I can call those methods...but all of the above is to be done in Java, and to be able to call those methods from my Pascal code I will have to add yet another layer, where I access that Java code from my Pascal code (which I don't know how to do, but that's beside the point).

Now, instead of all that, is there a way to call the methods in libexample.so directly?

Of course we can assume that libexample.so is in the same folder as libcontrols.so (which is created by LAMW).

And by the way, being able to do that would be great also for another reason: we could use LAMW to make special libraries with different functionalities, and then call them from the "main" library of the LAMW app, which is libcontrols.so

Thank you!
manlio mazzon gmail

jmpessoa

  • Hero Member
  • *****
  • Posts: 1470
Yes!

[example for an "android-arch-arm"  library]

Just put it  "in the same folder as libcontrols.so" ....

added the ".pas" header to unit "uses".....

Prepare to Linker: put a copy here, too:
 
YourPathToNDK + "\platforms\android-XX\arch-arm\usr\lib\"

get "XX" from project  ".lpi"

<CustomData Count="...">
      <Item... Name="NdkApi" Value="android-21"/>
</CustomData>

that is XX=21

Re-open your project.... and try "Build"...
« Last Edit: June 07, 2019, 10:13:17 pm by jmpessoa »
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

Manlio

  • Jr. Member
  • **
  • Posts: 57
  • Pascal dev
That sounds great, thanks! I never would have though of copying to "so" file to the NDK by myself...Once again I don't know what I would do without your tips :)

And on the same topic, maybe is it also possible to link dynamically? I mean, instead of using a header .pas file, to call LoadLibrary('libexample.so') in Pascal code, and then GetProcAddress ? is there a chance that will work?

In any case I'm going to try both approaches, static linking and dynamic linking, but any kind tips that can save me hours of frustration will be extremely appreciated!

Thanks
manlio mazzon gmail

jmpessoa

  • Hero Member
  • *****
  • Posts: 1470
Quote
is there a chance that will work?

Yes!  [but I dont have a test...]

Another possibility ....

[Update your LAMW from git.... I just do a minor fix to "Use/Import C stuff...."]

>>> Adventure on importing "C" stuff

Save this content as "addc.h" file in some place...
Code: C  [Select]
  1. extern int add(int a, int b);
  2.  

Save this content as "addc.c" file in some place... [can be the same ".h" folder]

Code: C  [Select]
  1. int add(int a, int b) {
  2.     return (a+b);
  3. }
  4.  

1) Open or create a LAMW GUI project

2) Go to lazarus menu "Tools" --> "[LAMW] Android Module Wizard" --> "Use/Import C stuff"
   2.1) edit/select the path to the ".c" folder
   2.2) edit/select the path to the ".h" folder
   2.3) edit the library name... ex. "myaddc" [the "lib" prefix wil be will be added automatically]

3) Go to your project "unit1" and append the generated header "addc" to "uses"
                 [warning: for complex ".h" the generated pascal header may require some adjustments ...]

Code: Pascal  [Select]
  1.         uses
  2.           Classes, SysUtils, AndroidWidget, Laz_And_Controls, addc;
  3.  
4) Go to Lazarus menu  "Run"  --> "Build"
 
  [Success!!!]

5) Using you C "add" stuff
Code: Pascal  [Select]
  1. procedure TAndroidModule1.jButton1Click(Sender: TObject);
  2. var
  3.   sum: longint;
  4. begin
  5.   sum:= add(4,9);  // <--- "add" from your lib C !!
  6.   ShowMessage('[Success!]  sum =' + IntToStr(sum));
  7. end;
  8.  

You can look into your project folders to observe some new stuff generated....

« Last Edit: June 08, 2019, 10:52:17 pm by jmpessoa »
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

Manlio

  • Jr. Member
  • **
  • Posts: 57
  • Pascal dev
Thanks, finally I was able to make some progress.

The library I wanted to use is written in C. I was not able to compile it, but I got a compiled .so file, and I was able to open it and call methods in it, using LoadLibrary and GetProcAddress.

I also used LAMW's utility "Import C stuff", which helped me figure out the methods to call, the "struct" I had to redeclare as record, etc.

So, thanks again!
manlio mazzon gmail