BridJ初体验

  |   0 评论   |   135 浏览

背景

前文介绍了JNA。但是JNA不支持C++。

这里的BriDJ的一个优势就是支持C++。

于是拭目以待,看看怎么用吧。

支持的平台

./linux_x64/libbridj.so
./win32/bridj.dll
./darwin_universal/libbridj.dylib
./sunos_x86/libbridj.so
./sunos_x64/libbridj.so
./win64/bridj.dll
./linux_armhf/libbridj.so
./linux_x86/libbridj.so

自动生成BridJ代码初体验

准备工作

先编写一个最简单的c++的so包。

├── include
│   └── hello.h
├── lib
├── source
│   └── hello.cpp
└── src
    └── main.cpp

准备 hello.h 文件

$cat include/hello.h
class Hello {
    public:
        void show();
};

准备 hello.cpp 文件

$cat source/hello.cpp
#include "../include/hello.h"
#include <stdio.h>

void Hello::show() {
    printf("hello world\n");
}

编译 libhello.so

$g++ source/hello.cpp -fPIC -shared -o lib/libhello.so

编写 main.cpp 文件

$cat src/main.cpp
#include "../include/hello.h"

int main(int argc, char** argv){
    Hello* hello = new Hello();
    hello->show();
}

编译 main.cpp 文件

$g++ src/main.cpp -L lib/ -l hello -o main

运行 main 文件

直接运行,会提示找不到 hello

$LD_LIBRARY_PATH=lib/ ./main
hello world

自动生成BridJ代码

下载

wget http://central.maven.org/maven2/com/nativelibs4java/jnaerator/0.12/jnaerator-0.12-shaded.jar

运行

$java -jar jnaerator-0.12-shaded.jar -v -runtime BridJ -mode Maven -mavenGroupId com.abeffect.jna -mavenArtifactId testJNA -o jna_code -package com.abeffect.jna.core -f -library Hello lib/libhello.so include/hello.h

结果

jna_code/
├── pom.xml
└── src
    └── main
        ├── java
        │   └── com
        │       └── abeffect
        │           └── jna
        │               └── core
        │                   ├── Hello.java
        │                   └── HelloLibrary.java
        └── resources
            └── lib
                └── linux_x64
                    └── libhello.so

Hello.java如下:

package com.abeffect.jna.core;
import org.bridj.BridJ;
import org.bridj.ann.Library;
import org.bridj.ann.Runtime;
import org.bridj.cpp.CPPRuntime;
/**
 * Wrapper for library <b>Hello</b><br>
 * This file was autogenerated by <a href="http://jnaerator.googlecode.com/">JNAerator</a>,<br>
 * a tool written by <a href="http://ochafik.com/">Olivier Chafik</a> that <a href="http://code.google.com/p/jnaerator/wiki/CreditsAndLicense">uses a few opensource projects.</a>.<br>
 * For help, please visit <a href="http://nativelibs4java.googlecode.com/">NativeLibs4Java</a> or <a href="http://bridj.googlecode.com/">BridJ</a> .
 */
@Library("Hello")
@Runtime(CPPRuntime.class)
public class HelloLibrary {
        static {
                BridJ.register();
        }
}

HelloLibrary.java如下:

package com.abeffect.jna.core;
import org.bridj.BridJ;
import org.bridj.ann.Library;
import org.bridj.ann.Runtime;
import org.bridj.cpp.CPPRuntime;
/**
 * Wrapper for library <b>Hello</b><br>
 * This file was autogenerated by <a href="http://jnaerator.googlecode.com/">JNAerator</a>,<br>
 * a tool written by <a href="http://ochafik.com/">Olivier Chafik</a> that <a href="http://code.google.com/p/jnaerator/wiki/CreditsAndLicense">uses a few opensource projects.</a>.<br>
 * For help, please visit <a href="http://nativelibs4java.googlecode.com/">NativeLibs4Java</a> or <a href="http://bridj.googlecode.com/">BridJ</a> .
 */
@Library("Hello")
@Runtime(CPPRuntime.class)
public class HelloLibrary {
	static {
		BridJ.register();
	}
}

参考

评论

发表评论

validate