jna包装共享.so库 - 访问类

时间:2021-09-05 06:29:25

I have an opensource shared library called libmbc.so. I am interested in using this library in a java implementation which should be able to reproduce a behavior already tested in c++. Ideally I aim to have access to a class in this library called MBCNodal, and call its member functions in the java wrapper. If I unroll the content of the shared library with nm -gC libmbc.so, I obtain the following

我有一个名为libmbc.so的开源共享库。我有兴趣在java实现中使用这个库,它应该能够重现已经在c ++中测试过的行为。理想情况下,我的目标是访问此库中名为MBCNodal的类,并在java包装器中调用其成员函数。如果我使用nm -gC libmbc.so展开共享库的内容,我将获得以下内容

                 U __assert_fail@@GLIBC_2.2.5
                 U bind@@GLIBC_2.2.5
000000000020e278 B __bss_start
                 U close@@GLIBC_2.2.5
                 U connect@@GLIBC_2.2.5
                 w __cxa_finalize@@GLIBC_2.2.5
                 U __cxa_pure_virtual@@CXXABI_1.3
                 U __cxa_rethrow@@CXXABI_1.3
000000000020e278 D _edata
000000000020e280 B _end
                 U __errno_location@@GLIBC_2.2.5
0000000000009ab0 T _fini
                 U fprintf@@GLIBC_2.2.5
                 U freeaddrinfo@@GLIBC_2.2.5
                 U free@@GLIBC_2.2.5
                 U fwrite@@GLIBC_2.2.5
                 U getaddrinfo@@GLIBC_2.2.5
                 w __gmon_start__
                 U __gxx_personality_v0@@CXXABI_1.3
0000000000003db8 T _init
                 w _ITM_deregisterTMCloneTable
                 w _ITM_registerTMCloneTable
                 w _Jv_RegisterClasses
                 U malloc@@GLIBC_2.2.5
0000000000004670 T mbc_check_cmd
000000000020e27c B mbc_dummy
00000000000046c0 T mbc_get_cmd
0000000000004850 T mbc_inet_init
0000000000005f60 T mbc_modal_destroy
0000000000005690 T mbc_modal_get_motion
0000000000005a40 T mbc_modal_init
0000000000005b90 T mbc_modal_negotiate_request
0000000000005db0 T mbc_modal_negotiate_response
00000000000058d0 T mbc_modal_put_forces
00000000000055d0 T mbc_nodal_destroy
0000000000004970 T mbc_nodal_get_motion
0000000000004c30 T mbc_nodal_init
00000000000051d0 T mbc_nodal_negotiate_request
00000000000053f0 T mbc_nodal_negotiate_response
0000000000004b30 T mbc_nodal_put_forces
0000000000004720 T mbc_put_cmd
00000000000048f0 T mbc_unix_init
00000000000097a0 T mbdyn_host2inet_addr
0000000000009980 T mbdyn_make_inet_socket
0000000000009890 T mbdyn_make_inet_socket_type
0000000000009aa0 T mbdyn_make_named_socket
00000000000099a0 T mbdyn_make_named_socket_type
                 U recv@@GLIBC_2.2.5
                 U send@@GLIBC_2.2.5
                 U snprintf@@GLIBC_2.2.5
                 U socket@@GLIBC_2.2.5
                 U stderr@@GLIBC_2.2.5
                 U stdout@@GLIBC_2.2.5
                 U strerror@@GLIBC_2.2.5
                 U strlen@@GLIBC_2.2.5
                 U strncpy@@GLIBC_2.2.5
                 U _Unwind_Resume@@GCC_3.0
                 U usleep@@GLIBC_2.2.5
                 U operator delete(void*, unsigned long)@@CXXABI_1.3.9
0000000000006000 T MBCBase::SetTimeout(int)
0000000000006010 T MBCBase::SetVerbose(bool)
0000000000007710 T MBCBase::DynamicsLabel()
0000000000006020 T MBCBase::SetDataAndNext(bool)
0000000000007a20 T MBCBase::F(unsigned char)
0000000000007bf0 T MBCBase::M(unsigned char)
00000000000064b0 T MBCBase::Init(char const*)
0000000000006510 T MBCBase::Init(char const*, unsigned short)
00000000000062b0 T MBCBase::SetStatus(MBCBase::Status)
00000000000063e0 T MBCBase::MBCBase()
00000000000063e0 T MBCBase::MBCBase()
0000000000006490 T MBCBase::~MBCBase()
0000000000006400 T MBCBase::~MBCBase()
0000000000006400 T MBCBase::~MBCBase()
0000000000009310 T MBCModal::Initialize(MBCBase::Rot, unsigned int)
0000000000009490 T MBCModal::DynamicsLabel()
00000000000094b0 T MBCModal::F(unsigned char)
00000000000094d0 T MBCModal::M(unsigned char)
0000000000009730 T MBCModal::P(unsigned int)
00000000000093a0 T MBCModal::MBCModal(MBCBase::Rot, unsigned int)
00000000000092c0 T MBCModal::MBCModal()
00000000000093a0 T MBCModal::MBCModal(MBCBase::Rot, unsigned int)
00000000000092c0 T MBCModal::MBCModal()
0000000000006470 T MBCModal::~MBCModal()
0000000000006450 T MBCModal::~MBCModal()
0000000000006450 T MBCModal::~MBCModal()
0000000000007d40 T MBCNodal::Initialize(MBCBase::Rot, unsigned int, bool, MBCBase::Rot, bool)
0000000000008e60 T MBCNodal::DynamicsLabel(unsigned int)
0000000000007f20 T MBCNodal::DynamicsLabel()
0000000000007f40 T MBCNodal::F(unsigned char)
0000000000009110 T MBCNodal::F(unsigned int, unsigned char)
0000000000007f60 T MBCNodal::M(unsigned char)
0000000000009220 T MBCNodal::M(unsigned int, unsigned char)
0000000000007e00 T MBCNodal::MBCNodal(MBCBase::Rot, unsigned int, bool, MBCBase::Rot, bool)
0000000000007cf0 T MBCNodal::MBCNodal()
0000000000007e00 T MBCNodal::MBCNodal(MBCBase::Rot, unsigned int, bool, MBCBase::Rot, bool)
0000000000007cf0 T MBCNodal::MBCNodal()
0000000000006430 T MBCNodal::~MBCNodal()
0000000000006410 T MBCNodal::~MBCNodal()
0000000000006410 T MBCNodal::~MBCNodal()
00000000000077c0 T MBCBase::GetRefNodeF() const
0000000000007870 T MBCBase::GetRefNodeM() const
0000000000006780 T MBCBase::GetRefNodeR() const
00000000000066d0 T MBCBase::GetRefNodeX() const
0000000000006050 T MBCBase::bDataAndNext() const
0000000000006a20 T MBCBase::GetRefNodeXP() const
0000000000007660 T MBCBase::DynamicsLabel() const
0000000000006100 T MBCBase::GetRefNodeRot() const
0000000000006bb0 T MBCBase::GetRefNodeXPP() const
0000000000006150 T MBCBase::bAccelerations() const
0000000000006ad0 T MBCBase::GetRefNodeOmega() const
0000000000006860 T MBCBase::GetRefNodeTheta() const
0000000000006620 T MBCBase::KinematicsLabel() const
0000000000006c90 T MBCBase::GetRefNodeOmegaP() const
0000000000006940 T MBCBase::GetRefNodeEuler123() const
0000000000007950 T MBCBase::F(unsigned char) const
0000000000007af0 T MBCBase::M(unsigned char) const
0000000000006e60 T MBCBase::R(unsigned char, unsigned char) const
0000000000006d90 T MBCBase::X(unsigned char) const
00000000000075b0 T MBCBase::GetRefNodeDynamicsLabel() const
00000000000065a0 T MBCBase::GetRefNodeKinematicsLabel() const
00000000000071b0 T MBCBase::XP(unsigned char) const
0000000000007380 T MBCBase::XPP(unsigned char) const
0000000000007280 T MBCBase::Omega(unsigned char) const
0000000000006fb0 T MBCBase::Theta(unsigned char) const
0000000000006580 T MBCBase::GetCmd() const
0000000000006080 T MBCBase::GetRot() const
0000000000007480 T MBCBase::OmegaP(unsigned char) const
0000000000006190 T MBCBase::bLabels() const
00000000000060c0 T MBCBase::bRefNode() const
0000000000006030 T MBCBase::bVerbose() const
00000000000070b0 T MBCBase::Euler123(unsigned char) const
0000000000006070 T MBCBase::GetStatus() const
0000000000005fd0 T MBCModal::GetBasePtr() const
0000000000009480 T MBCModal::DynamicsLabel() const
0000000000005fe0 T MBCModal::GetRefNodePtr() const
00000000000093f0 T MBCModal::KinematicsLabel() const
00000000000094a0 T MBCModal::F(unsigned char) const
00000000000094c0 T MBCModal::M(unsigned char) const
00000000000096c0 T MBCModal::P(unsigned int) const
00000000000095a0 T MBCModal::Q(unsigned int) const
0000000000009410 T MBCModal::R(unsigned char, unsigned char) const
0000000000009400 T MBCModal::X(unsigned char) const
0000000000009600 T MBCModal::QP(unsigned int) const
0000000000009440 T MBCModal::XP(unsigned char) const
0000000000009460 T MBCModal::XPP(unsigned char) const
0000000000009670 T MBCModal::GetP() const
0000000000009520 T MBCModal::GetQ() const
00000000000063a0 T MBCModal::Close() const
0000000000009560 T MBCModal::GetQP() const
0000000000009450 T MBCModal::Omega(unsigned char) const
0000000000009420 T MBCModal::Theta(unsigned char) const
0000000000009470 T MBCModal::OmegaP(unsigned char) const
0000000000005ff0 T MBCModal::GetType() const
0000000000009430 T MBCModal::Euler123(unsigned char) const
00000000000094e0 T MBCModal::GetModes() const
0000000000006280 T MBCModal::GetMotion() const
0000000000006350 T MBCModal::Negotiate() const
0000000000006240 T MBCModal::PutForces(bool) const
0000000000005fa0 T MBCNodal::GetBasePtr() const
0000000000008af0 T MBCNodal::GetEuler123(unsigned int) const
0000000000008140 T MBCNodal::GetEuler123() const
0000000000008dd0 T MBCNodal::DynamicsLabel(unsigned int) const
0000000000007f10 T MBCNodal::DynamicsLabel() const
0000000000005fb0 T MBCNodal::GetRefNodePtr() const
0000000000008910 T MBCNodal::KinematicsLabel(unsigned int) const
0000000000007e80 T MBCNodal::KinematicsLabel() const
0000000000008d60 T MBCNodal::GetDynamicsLabel() const
0000000000008880 T MBCNodal::GetKinematicsLabel(unsigned int) const
0000000000007fb0 T MBCNodal::GetKinematicsLabel() const
0000000000007f30 T MBCNodal::F(unsigned char) const
00000000000090a0 T MBCNodal::F(unsigned int, unsigned char) const
0000000000007f50 T MBCNodal::M(unsigned char) const
0000000000009180 T MBCNodal::M(unsigned int, unsigned char) const
0000000000007ea0 T MBCNodal::R(unsigned char, unsigned char) const
00000000000083d0 T MBCNodal::R(unsigned int, unsigned char, unsigned char) const
0000000000007e90 T MBCNodal::X(unsigned char) const
0000000000008360 T MBCNodal::X(unsigned int, unsigned char) const
0000000000007ed0 T MBCNodal::XP(unsigned char) const
0000000000008600 T MBCNodal::XP(unsigned int, unsigned char) const
0000000000007ef0 T MBCNodal::XPP(unsigned char) const
0000000000008710 T MBCNodal::XPP(unsigned int, unsigned char) const
0000000000008fa0 T MBCNodal::GetF(unsigned int) const
0000000000008ef0 T MBCNodal::GetF() const
0000000000009010 T MBCNodal::GetM(unsigned int) const
0000000000008f30 T MBCNodal::GetM() const
00000000000089f0 T MBCNodal::GetR(unsigned int) const
0000000000008060 T MBCNodal::GetR() const
00000000000089a0 T MBCNodal::GetX(unsigned int) const
0000000000008020 T MBCNodal::GetX() const
0000000000006310 T MBCNodal::Close() const
0000000000008b70 T MBCNodal::GetXP(unsigned int) const
00000000000081b0 T MBCNodal::GetXP() const
0000000000007ee0 T MBCNodal::Omega(unsigned char) const
0000000000008670 T MBCNodal::Omega(unsigned int, unsigned char) const
0000000000007eb0 T MBCNodal::Theta(unsigned char) const
00000000000084a0 T MBCNodal::Theta(unsigned int, unsigned char) const
0000000000008c40 T MBCNodal::GetXPP(unsigned int) const
0000000000008260 T MBCNodal::GetXPP() const
0000000000007f00 T MBCNodal::OmegaP(unsigned char) const
00000000000087b0 T MBCNodal::OmegaP(unsigned int, unsigned char) const
0000000000005fc0 T MBCNodal::GetType() const
0000000000007ec0 T MBCNodal::Euler123(unsigned char) const
0000000000008550 T MBCNodal::Euler123(unsigned int, unsigned char) const
0000000000007f70 T MBCNodal::GetNodes() const
0000000000008bc0 T MBCNodal::GetOmega(unsigned int) const
00000000000081f0 T MBCNodal::GetOmega() const
0000000000008a70 T MBCNodal::GetTheta(unsigned int) const
00000000000080d0 T MBCNodal::GetTheta() const
0000000000006210 T MBCNodal::GetMotion() const
0000000000008cc0 T MBCNodal::GetOmegaP(unsigned int) const
00000000000082d0 T MBCNodal::GetOmegaP() const
00000000000062c0 T MBCNodal::Negotiate() const
00000000000061d0 T MBCNodal::PutForces(bool) const
000000000020dc48 V typeinfo for MBCBase
000000000020dc70 V typeinfo for MBCModal
000000000020dc58 V typeinfo for MBCNodal
000000000000b468 V typeinfo name for MBCBase
000000000000b488 V typeinfo name for MBCModal
000000000000b478 V typeinfo name for MBCNodal
000000000020dc88 V vtable for MBCBase
000000000020dd38 V vtable for MBCModal
000000000020dce0 V vtable for MBCNodal
                 U vtable for __cxxabiv1::__class_type_info@@CXXABI_1.3
                 U vtable for __cxxabiv1::__si_class_type_info@@CXXABI_1.3

Therefore I have the feeling that the MBCNodal class is somehow accessible with JNA. But I don't manage to figure out how?

因此,我觉得MBNCodal类可以通过JNA以某种方式访问​​。但我不知道怎么弄?

I wish not to modify the library implementation. I have the source code of both .h and .cpp. But I don't know how it was compiled in order to produce the libmbc.so.

我希望不要修改库实现。我有.h和.cpp的源代码。但我不知道如何编译它以生成libmbc.so。

thanks for your help.

谢谢你的帮助。

Edit:

here is a sample code on how I load the library and access the extern methods:

这是一个示例代码,说明如何加载库并访问extern方法:

private interface Wrapper extends Library {

        Wrapper INSTANCE = Native.loadLibrary( C_LIBRARY_PATH, Wrapper.class );

        Pointer mbc_nodal_init(Pointer ptr, int refnode, int nodes, int label, int rot, int accels);

        int mbc_nodal_get_motion(Pointer ptr);

        int mbc_nodal_negotiate_request(Pointer ptr);

        int mbc_nodal_put_forces(Pointer ptr, int converged);


    }

1 个解决方案

#1


0  

You can use JNI as an adapter.

您可以将JNI用作适配器。

Take a look at these two samples. I guess, this is something you are looking for.

看看这两个样本。我想,这是你正在寻找的东西。

http://jnicookbook.owsiak.org/recipe-No-021/

http://jnicookbook.owsiak.org/recipe-No-023/

In both cases, you have code that will be adapted by JNI code

在这两种情况下,您都有可以通过JNI代码进行调整的代码

jna包装共享.so库 - 访问类

Of course, you have to make sure to properly map Java data to what ever is required in your .so based functions.

当然,您必须确保将Java数据正确映射到基于.so的函数中所需的数据。

As for the JNA, there is quite comprehensive tutorial site here:

至于JNA,这里有一个相当全面的教程网站:

http://www.eshayne.com/jnaex/index.html

#1


0  

You can use JNI as an adapter.

您可以将JNI用作适配器。

Take a look at these two samples. I guess, this is something you are looking for.

看看这两个样本。我想,这是你正在寻找的东西。

http://jnicookbook.owsiak.org/recipe-No-021/

http://jnicookbook.owsiak.org/recipe-No-023/

In both cases, you have code that will be adapted by JNI code

在这两种情况下,您都有可以通过JNI代码进行调整的代码

jna包装共享.so库 - 访问类

Of course, you have to make sure to properly map Java data to what ever is required in your .so based functions.

当然,您必须确保将Java数据正确映射到基于.so的函数中所需的数据。

As for the JNA, there is quite comprehensive tutorial site here:

至于JNA,这里有一个相当全面的教程网站:

http://www.eshayne.com/jnaex/index.html