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 에러가 나오네요.

뭐, 여기서 삽질은 종료 되었습니다.

제가 알고 싶은것은, 위의 방법이 틀린건지, 아니면 근본적으로 위의 방법은 없는건지;;

경험이 있으신분의 조언을 구합니다.

M.W.Park의 이미지

정확하게 무엇을 하고 싶으신지 잘 모르겠습니다만...
JNI나 SWIG를 이용해서 하면 안되는 상황인지부터 한번 알아보시는게 좋을듯합니다.

-----
오늘 나의 취미는 끝없는, 끝없는 인내다. 1973 法頂

-----
오늘 의 취미는 끝없는, 끝없는 인내다. 1973 法頂

밝은거울의 이미지

java 로 만들어진 API 를, 그러니까 class 들을 .a 아카이브로 만들어서 C/C++ 에서 호출하고 싶은거죠;;

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.