简体   繁体   中英

delphi XE7 firemonkey android shared object / library to be dynamically loaded - template

Is there a template to create a .so file that can be loaded by another delphi ape file - I have tried starting a blank fire monkey project and changing program to library and build it but the .so file that it produces won't load with dlopen within another delphi project. I have read that in other development environments there is a islibrary setting. I guess more to the point is there an example .so library built with fire monkey - I have found the bare bones link without fire monkey but it uses just jni not androidapi.jni - thanks

If you start a blank Firemonkey project and change Unit to Library , you'll get this compiler error:

[DCC Error] myfunnylib.pas(1): E2029 'UNIT' expected but 'LIBRARY' found


Trying to add an existing library project to the project group will separate said project from the rest of the build and assign unique build targets and platforms to it. Which will leave you with options to compile for Windows and OS X.

The only way I've heard of so far is to pre-compile your library with another compiler. FPC has been mentioned elsewhere. I haven't tried that yet but its next on the list.

http://wiki.freepascal.org/Android

Don't get confused by the fact that every Android app is in fact a shared object with the extension .so (for shared object ). However, this is not the same thing as a shared library. Because a library exports its functions while an application does not. To a compiler, that IS quite a difference though you wouldn't see that by looking at the file extension (but its prefix lib instead).

If you restrict your question to XE and Firemonkey, my only suggestion here would be to look into Android services. A bound local service might offer similar capabilities you'd expect from a library:

http://docwiki.embarcadero.com/RADStudio/Seattle/en/Android_Service

It's also important to note that Android N will not allow dynamic linking anymore so many of the methods you'll find elsewhere as a solution won't work.

suat dmk 's code example in the answer below, though up-voted, is misleading.
It cannot be compiled for Android or iOS in XE 10.1 or any of its predecessors.

UPDATE:
There's a definite statement from Embarcadero staff regarding this issue.
It took a bit of patience in explaining the question, but the reply is clear enough:

[..] wants Delphi to have a project type of Shared Library(.so),
in this case then he is right, Delphi does not have it right now. [..]

quod erat demonstrandum

Hence there can be no such template. Also answers this question.

the following codes may be useful.

//-- Code for libMylib.so (MyLib.dproj) --- 

    library MyLib; 
//    uses  SysUtils;


    function GetSum(a, b: integer ) : integer; cdecl; 
    begin 
      Result := a + b; 
    end;

    exports

    GetSum        name 'GetSum'; 

    begin 
    end. 

    //-- Code for using libMylib.so (TestMyLib.dproj) --- 

    var 
    DocDir, LibFN: string; 
    GetSum: function(a, b: integer ) : integer; cdecl; 
    hLib: HMODULE; 

    begin 
      DocDir := IncludeTrailingPathDelimiter(System.IOUtils.TPath.GetLibrary`enter code here`Path);
      LibFN:=DocDir + 'libMyLib.so';
      hLib := LoadLibrary(LibFN); //ERROR (Segmentation fault (11)) 
      if hLib<>0 then 
      begin 
        GetSum := GetProcAddress(hLib, 'GetSum');//It works
        ShowMessage(IntToStr(GetSum(3, 8)));//It works 
      end; 
    end; 

PS: you must add compiled libMyLib.so file to Deployment of TestMyLib.dproj.

P.S2: it gives an error while executing "LoadLibrary", but it Works.

I couldn't find a solution. Probably it is related with compiler/linker options of MyLib.dproj. Because when I test another '.so' file which is compiled with C++, no problem occurs while calling LoadLibrary.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM