gcj 를 이용하여 만든 라이브러리(*.a) 를 C++ 에서 호출 할 수 있나요?
안녕하세요.
http://wiki.kldp.org/wiki.php/GCJ?refresh=1
위 문서를 보던중 궁금한것이 있어서
글을 올립니다.
gcj 를 이용하면 JAVA 로 만들어진 class 파일을 .a 의 아카이브로 만드는 것이 가능하더군요.
그래서 짧은생각으로; 간단하게 테스트 해 보았습니다.
$ cat LibExamGCJ.java
public class LibExamGCJ {
LibExamGCJ() {
System.out.println("JAVA Class GCJ Test --- this is print in lib\n");
}
}
$ javac LibExamGCJ.java
생성된 .class 파일을
$ gcj -c LibExamGCJ.class
gcj 를 이용하여 컴파일 하였습니다.
그런 다음
$ ar -crs libexam.a LibExamGCJ.o
위의 명령어를 통하여, libexam.a 를 만들어냈죠.
여기서 만들어진 아카이브를 c/c++ 에서 호출할 수 있지 않을까;; 하는 생각이 들더군요.
그래서 한번 해봤습니다.
$ cat test.cpp
#include
#include
int main()
{
printf("this is c++ output\n");
LibExamGCJ();
}
무식하게 바로; 컴파일 해 보았습니다.
$ g++ -L./ -o test test.cpp ./libexam.a
test.cpp: In function `int main()':
test.cpp:11: `LibExamGCJ' undeclared (first use this function)
test.cpp:11: (Each undeclared identifier is reported only once for each
function it appears in.)
LibExamGCJ 가 정의되지 않았다네요.
검색을 해보니 gchj 라는것이 있더군요.
$ gcjh LibExamGCJ
헤더파일이 생겼군요.
$ cat LibExamGCJ.h
// DO NOT EDIT THIS FILE - it is machine generated -*- c++ -*-
#ifndef __LibExamGCJ__
#define __LibExamGCJ__
#pragma interface
#include
extern "Java"
{
class LibExamGCJ;
};
class ::LibExamGCJ : public ::java::lang::Object
{
public: // actually package-private
LibExamGCJ ();
public:
static ::java::lang::Class class$;
};
#endif /* __LibExamGCJ__ */
자 이제 헤더를 include 해서 다시 테스트 했습니다.
$ cat test.cpp
#include
#include
#include "LibExamGCJ.h"
int main()
{
printf("this is c++ output\n");
LibExamGCJ();
}
$ g++ -L./ -o test test.cpp ./libexam.a
/tmp/ccG8PoBs.o(.eh_frame+0x11): undefined reference to `__gcj_personality_v0'
./libexam.a(LibExamGCJ.o)(.text+0xe): In function `LibExamGCJ::LibExamGCJ[in-charge]()':
: undefined reference to `java::lang::Object::Object[in-charge]()'
./libexam.a(LibExamGCJ.o)(.text+0x1c): In function `LibExamGCJ::LibExamGCJ[in-charge]()':
: undefined reference to `java::lang::System::class$'
./libexam.a(LibExamGCJ.o)(.text+0x21): In function `LibExamGCJ::LibExamGCJ[in-charge]()':
: undefined reference to `_Jv_InitClass'
./libexam.a(LibExamGCJ.o)(.text+0x29): In function `LibExamGCJ::LibExamGCJ[in-charge]()':
: undefined reference to `java::lang::System::out'
./libexam.a(LibExamGCJ.o)(.data+0x30): undefined reference to `java::lang::Object::finalize()'
./libexam.a(LibExamGCJ.o)(.data+0x34): undefined reference to `java::lang::Object::hashCode()'
./libexam.a(LibExamGCJ.o)(.data+0x38): undefined reference to `java::lang::Object::equals(java::lang::Object*)'
./libexam.a(LibExamGCJ.o)(.data+0x3c): undefined reference to `java::lang::Object::toString()'
./libexam.a(LibExamGCJ.o)(.data+0x40): undefined reference to `java::lang::Object::clone()'
./libexam.a(LibExamGCJ.o)(.data+0x60): undefined reference to `vtable for java::lang::Class'
./libexam.a(LibExamGCJ.o)(.data+0x70): undefined reference to `java::lang::Object::class$'
./libexam.a(LibExamGCJ.o)(.eh_frame+0x11): undefined reference to `__gcj_personality_v0'
collect2: ld returned 1 exit status
아, ld 에러가 나오네요.
뭐, 여기서 삽질은 종료 되었습니다.
제가 알고 싶은것은, 위의 방법이 틀린건지, 아니면 근본적으로 위의 방법은 없는건지;;
경험이 있으신분의 조언을 구합니다.
정확하게 무엇을
정확하게 무엇을 하고 싶으신지 잘 모르겠습니다만...
JNI나 SWIG를 이용해서 하면 안되는 상황인지부터 한번 알아보시는게 좋을듯합니다.
-----
오늘 나의 취미는 끝없는, 끝없는 인내다. 1973 法頂
-----
오늘 나의 취미는 끝없는, 끝없는 인내다. 1973 法頂
java 로 만들어진 API
java 로 만들어진 API 를, 그러니까 class 들을 .a 아카이브로 만들어서 C/C++ 에서 호출하고 싶은거죠;;
댓글 달기