Linux系统上java调用C++ so库文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
package com.tree.go.util;
import com.sun.jna.Library;
import com.sun.jna.Native;
//继承Library,用于加载库文件 --Class mapping
public interface CPPTest extends Library {
// 加载libhello.so链接库
public static final String JNA_ImgProcess = "hello" ;
public static final CPPTest instance = (CPPTest)Native.loadLibrary(CPPTest.JNA_ImgProcess,CPPTest. class );
// 此方法为链接库中的方法 function mapping
void test();
int addTest( int a, int b);
//调用,singleton
public static void main(String[] args) {
CPPTest instance =CPPTest.instance;
instance.test();
int c =instance.addTest( 10 , 20 );
}
} |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
#include <iostream> using namespace std;
extern "C" { //避免name mangling,编译后名称symbol破坏,导致无法找到函数,告诉编译器下面的代码块使用c编译器来编译
int addTest( int a, int b)
{ cout << "a+b" << a + b << endl;
return a + b;
} void test()
{
cout << "hello word from C++ ! " << endl;
} } |
linux动态库的命名规则
显式调用C++动态库注意点
附件:Linux下库相关命令
g++(gcc)编译选项
nm命令
ldd命令
echo “export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/root/haihang/linux64” >> ~/.bashrc
多个源文件相互调用,然后才调用动态库
g++ -fPIC -shared -o libhello.so hello.cpp hsdownload.cpp -L./ -lpthread -lstdc++ -lrt -ldl -lm -lresolv -lcrypto -lssl -lcurl -lprotobuf -lhpr -lhlog -lopensslwrap -lys_net -lezviz_streamclient -lezserveropensdk -I./include -I./
# nm libhello.so |grep hs_download
Default Type Mappings
Java primitive types (and their object equivalents) map directly to the native C type of the same size.
Native Type | Size | Java Type | Common Windows Types |
char | 8-bit integer | byte | BYTE, TCHAR |
short | 16-bit integer | short | WORD |
wchar_t | 16/32-bit character | char | TCHAR |
int | 32-bit integer | int | DWORD |
int | boolean value | boolean | BOOL |
long | 32/64-bit integer | NativeLong | LONG |
long long | 64-bit integer | long | __int64 |
float | 32-bit FP | float | |
double | 64-bit FP | double | |
char* | C string | String | LPTCSTR |
void* | pointer | Pointer | LPVOID, HANDLE, LPXXX |
Unsigned types use the same mappings as signed types. C enums are usually interchangeable with “int”.
https://blog.csdn.net/baidu_35679960/article/details/77585107