티스토리 뷰

728x90

출처 : Mobile Tutorial: Using InterBase ToGo (iOS and Android)
http://www.pclviewer.com/android/
Linux Certif - dlopen
Getting the Battery Level on Android With Delphi
XE4 Mobile Tip #2 ? Loading local HTML content
Android/SQLite 공유 라이브러리를 이용하는 C로 프로그램 작성
Delphi XE4 with iOS / #08. Hello World without Firemonkey
Thread: What is the right procedure to add the additional framework on iOS ?
Unable to dlopen(libsomething.so) Cannot load library: link_image[1995]: failed to link libsomething.so

정적 라이브러리 참조

Androidapi.Log.pas (델파이 일부 소스 발취)

unit Androidapi.Log;

interface

{$I Androidapi.inc}

type
  android_LogPriority = (
    ANDROID_LOG_UNKNOWN,
    ANDROID_LOG_DEFAULT,
    ANDROID_LOG_VERBOSE,
    ANDROID_LOG_DEBUG,
    ANDROID_LOG_INFO,
    ANDROID_LOG_WARN,
    ANDROID_LOG_ERROR,
    ANDROID_LOG_FATAL,
    ANDROID_LOG_SILENT
 );
 {$EXTERNALSYM android_LogPriority}


function __android_log_write(Priority: android_LogPriority; const Tag, Text: MarshaledAString): Integer; cdecl;
  external AndroidLogLib name '__android_log_write';
 {$EXTERNALSYM android_LogPriority}

{ Helper functions }
function LOGI(Text: MarshaledAString): Integer;
...

implementation

function LOGI(Text: MarshaledAString): Integer;
begin
  Result := __android_log_write(android_LogPriority.ANDROID_LOG_INFO, 'info', Text);
end;

...

end.

정적라이브러리 소스

testlib.h

/*
 * testlib.h
 *
 *  Created on: 2012. 9. 6.
 *      Author: bluexmas
 */
 
#ifndef LIBTEST_H_
#define LIBTEST_H_
 
extern const char* get_version();
 
#endif /* LIBTEST_H_ */

testlib.c

/*
 * testlib.cpp
 *
 *  Created on: 2012. 9. 6.
 *      Author: bluexmas
 */
 
#include "testlib.h"
 
const char* get_version() {
  return "testlib ver 0.1";
}

Makefile

ANDROID_NDK_ROOT = /opt/android-ndk-r9
NDK_TOOLCHAIN_VERSION = 4.6
TOOLCHAIN = /opt/android-9-toolchain

CROSS  = $(TOOLCHAIN)/bin/arm-linux-androideabi-
INCDIR = $(TOOLCHAIN)/sysroot/usr/include
LIBDIR = $(TOOLCHAIN)/sysroot/usr/lib

CC     = $(CROSS)gcc
AR     = $(CROSS)ar $(ARFLAGS) r
RANLIB = $(CROSS)ranlib
NM     = $(CROSS)nm

CFLAGS = -g $(INC) -fPIC 

TARGET = libtest.so

OBJS = testlib.o
#SRCS = testlib.c

.SUFFIXES : .c .o 

all : $(TARGET)

$(TARGET) : $(OBJS)
	$(CC) -shared -Wl,-soname,$@ -o $@ $(OBJS)

dep :
	gccmakedep $(INCDIR) $(SRCS)

clean :
	rm -rf $(OBJS) $(TARGET) core

 컴파일

파일

testlib.h /testlib.c /Makefile

위에서 만들어진 정적 라이브러리 파일을 /sdcard 폴더에 복사한 뒤(직접복사), 동적으로 로딩하여 get_version 함수룰 호출하는 예제입니다. library\lib\armeabi\ 경로에서 상대경로로 읽으려고 했지만, 절대경로만 인식하는 것 같습니다. 참고 자료가 별로 없어 확인 할 수 없었습니다.

Unit2.pas

unit Unit2;

interface

uses
  Classes, SysUtils, SyncObjs,
  System.IOUtils, Posix.Dlfcn;

//function __get_version(): MarshaledAString; cdecl; external '/sdcard/libtest.so' name 'get_version';

type
  _get_version = function(): MarshaledAString; {$IFNDEF CLR}cdecl;{$ENDIF}

function LoadLib() : IntPtr;

var
  get_version : _get_version;

implementation

function LoadLib() : IntPtr;
begin
  //
  //hLiteLib2 := dlopen(MarshaledAString(GetHomePath() + PathDelim + 'libtest.so'), RTLD_LAZY);
  //hLiteLib2 := dlopen(MarshaledAString(TPath.Combine(TPath.GetDocumentsPath, 'libtest.so')), RTLD_LAZY);
  Result := dlopen(MarshaledAString('/sdcard/libtest.so'), RTLD_LAZY);

  if NativeUInt(Result) > 0 then begin
    get_version := dlsym(Result, MarshaledAString('get_version'));
  end;
end;

Initialization
  get_version := nil;

end.

Unit1.pas

unit Unit1;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants, FMX.Types,
  FMX.Controls, FMX.Forms, FMX.Dialogs, FMX.WebBrowser, FMX.StdCtrls, FMX.Layouts,
  FMX.Edit, FMX.Memo;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Edit1: TEdit;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
    hLiteLib: IntPtr;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

uses
  System.IOUtils, Unit2;

{$R *.fmx}

procedure TForm1.Button1Click(Sender: TObject);
begin
  //ShowMessage(TPath.Combine(TPath.GetDocumentsPath, 'libtest.so'));

  hLiteLib := LoadLib();
  if NativeUInt(hLiteLib) = 0 then begin
    ShowMessage('Cannot load client library: ' + 'libtest.so');
  end;

  if Assigned(get_version) then begin
    Edit1.Text := get_version(); // IntToStr( hLiteLib );
  end;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  Memo1.Lines.LoadFromFile(TPath.Combine(TPath.GetDocumentsPath, 'memo.txt'));
end;

end.

Deployment 화면

실행결과

실행결과 Button1 선택

실행결과 Button2 선택

댓글
300x250
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함