用C ++编译和链接JNI代码

时间:2012-10-12 10:41:38

标签: c++ java-native-interface shared-libraries jnienv jniwrapper

  

可能重复:
  JNI in C++ to read file to jbyteArray

我使用JNI编译了以下C ++程序,它向我展示了下面提到的错误。请帮我解决问题。看起来Linking存在一些问题。如果我需要添加任何库,请告诉我。

    #include<iostream>
    #include<fstream>
    #include <stdio.h>
    #include "com_sp_dll_NativeMethods.h"   // this header file was generated by javah

    using namespace std;


    JNIEXPORT void JNICALL Java_HelloWorld_displayMessage(JNIEnv *env, jobject obj)
    {
      printf("Hello World!\n");
    }

    JNIEXPORT jbyteArray JNICALL Java_com_sp_dll_NativeMethods_getFile
      (JNIEnv *env, jobject obj)
     {


            ifstream fl("/home/rkannan/myFile.txt");
            fl.seekg(0, ios::end );
            size_t len = fl.tellg();
            char *ret = new char[len];
            fl.seekg(0, ios::beg);
            fl.read(ret, len);
            fl.close();


            jbyteArray result = env->NewByteArray( len);
            env->SetByteArrayRegion( result, 0, len, (const jbyte*) ret );
            delete[] ret;


            return result;
    }

收到的错误如下所示:

    bash-3.00$ g++ -I /usr/jdk/instances/jdk1.5.0/include -I /usr/jdk/instances/jdk1                                                                                                 .5.0/include/solaris -o libSample.so -shared com_sp_dll_NativeMethods.cpp
    Text relocation remains                         referenced
        against symbol                  offset      in file
    std::__ioinit                       0x550       /var/tmp//ccXRxj7L.o
    std::__ioinit                       0x554       /var/tmp//ccXRxj7L.o
    std::__ioinit                       0x588       /var/tmp//ccXRxj7L.o
    std::__ioinit                       0x58c       /var/tmp//ccXRxj7L.o
    .rodata (section)                   0x204       /var/tmp//ccXRxj7L.o
    .rodata (section)                   0x208       /var/tmp//ccXRxj7L.o
    .rodata (section)                   0x244       /var/tmp//ccXRxj7L.o
    .rodata (section)                   0x248       /var/tmp//ccXRxj7L.o
    .rodata (section)                   0x2f4       /var/tmp//ccXRxj7L.o
    .rodata (section)                   0x2f8       /var/tmp//ccXRxj7L.o
    std::fpos<__mbstate_t>::operator long long() const0x348         /var/tmp//ccXRxj                                                                                                 7L.o
    std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_tr                                                                                                 aits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char)0x400                                                                                                              /var/tmp//ccXRxj7L.o
    std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_tr                                                                                                 aits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)0x2                                                                                                 cc      /var/tmp//ccXRxj7L.o
    std::basic_ifstream<char, std::char_traits<char> >::is_open()0x260      /var/tmp                                                                                                 //ccXRxj7L.o
    std::basic_istream<char, std::char_traits<char> >::read(char*, int)0x39c                                                                                                         /var/tmp//ccXRxj7L.o
    JNIEnv_::SetByteArrayRegion(_jbyteArray*, int, int, signed char const*)0x468                                                                                                             /var/tmp//ccXRxj7L.o
    operator new[](unsigned int)        0x364       /var/tmp//ccXRxj7L.o
    std::basic_istream<char, std::char_traits<char> >::seekg(long long, std::_Ios_Se                                                                                                 ekdir)0x31c             /var/tmp//ccXRxj7L.o
    std::basic_istream<char, std::char_traits<char> >::seekg(long long, std::_Ios_Se                                                                                                 ekdir)0x384             /var/tmp//ccXRxj7L.o
    std::basic_istream<char, std::char_traits<char> >& std::operator>><char, std::ch                                                                                                 ar_traits<char> >(std::basic_istream<char, std::char_traits<char> >&, char*)0x2b                                                                                                 4       /var/tmp//ccXRxj7L.o
    printf                              0x20c       /var/tmp//ccXRxj7L.o
    JNIEnv_::NewByteArray(int)          0x444       /var/tmp//ccXRxj7L.o
    std::ios_base::Init::Init()         0x558       /var/tmp//ccXRxj7L.o
    std::basic_string<char, std::char_traits<char>, std::allocator<char> >::size() c                                                                                                 onst0x14        /var/tmp//ccXRxj7L.o
    std::cout                           0x2c0       /var/tmp//ccXRxj7L.o
    std::cout                           0x2c4       /var/tmp//ccXRxj7L.o
    std::cout                           0x3f4       /var/tmp//ccXRxj7L.o
    std::cout                           0x3f8       /var/tmp//ccXRxj7L.o
    std::basic_ifstream<char, std::char_traits<char> >::basic_ifstream()0x234                                                                                                        /var/tmp//ccXRxj7L.o
    std::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator                                                                                                 [](unsigned int) const0x9c              /var/tmp//ccXRxj7L.o
    std::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator                                                                                                 [](unsigned int) const0x128             /var/tmp//ccXRxj7L.o
    std::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator                                                                                                 [](unsigned int) const0x184             /var/tmp//ccXRxj7L.o
    std::basic_ifstream<char, std::char_traits<char> >::open(char const*, std::_Ios_                                                                                                 Openmode)0x250          /var/tmp//ccXRxj7L.o
    std::ios_base::Init::~Init()        0x590       /var/tmp//ccXRxj7L.o
    std::basic_ios<char, std::char_traits<char> >::eof() const0x288         /var/tmp                                                                                                 //ccXRxj7L.o
    unsigned int const& std::min<unsigned int>(unsigned int const&, unsigned int con                                                                                                 st&)0x44        /var/tmp//ccXRxj7L.o
    std::basic_ifstream<char, std::char_traits<char> >::~basic_ifstream()0x49c                                                                                                       /var/tmp//ccXRxj7L.o
    std::basic_ifstream<char, std::char_traits<char> >::~basic_ifstream()0x4ac                                                                                                       /var/tmp//ccXRxj7L.o
    std::basic_ifstream<char, std::char_traits<char> >::~basic_ifstream()0x4d4                                                                                                       /var/tmp//ccXRxj7L.o
    std::basic_ifstream<char, std::char_traits<char> >::~basic_ifstream()0x4f8                                                                                                       /var/tmp//ccXRxj7L.o
    std::basic_ifstream<char, std::char_traits<char> >::close()0x2e4        /var/tmp                                                                                                 //ccXRxj7L.o
    std::basic_ifstream<char, std::char_traits<char> >::close()0x3ac        /var/tmp                                                                                                 //ccXRxj7L.o
    std::basic_ifstream<char, std::char_traits<char> >::basic_ifstream(char const*,                                                                                                  std::_Ios_Openmode)0x300        /var/tmp//ccXRxj7L.o
    operator delete[](void*)            0x484       /var/tmp//ccXRxj7L.o
    std::basic_istream<char, std::char_traits<char> >::tellg()0x334         /var/tmp                                                                                                 //ccXRxj7L.o
    _Unwind_Resume                      0x508       /var/tmp//ccXRxj7L.o
    ld: fatal: relocations remain against allocatable but non-writable sections
    collect2: ld returned 1 exit status
    bash-3.00$

0 个答案:

没有答案