java调用c++

来源:互联网 发布:mac版wifi破解软件 编辑:程序博客网 时间:2024/05/16 18:43

test.java

public class test {  static {//System.out.println(System.getProperty("java.library.path"));String str = System.getProperty("java.library.path");System.loadLibrary("test");}    public native static void HelloWorld();    void Start()    {HelloWorld();   }public static void main(String[] args) {// TODO Auto-generated method stubtest xTest = new test();xTest.Start();}}

test.cpp test.h

// 头文件//#ifndef _BE_CALL_H_#define _BE_CALL_H_// 包含相应的头文件#ifdef WIN32#include "jni.h"#else//#include </usr/lib/jvm/jdk1.7.0_79/include/linux/jni_md.h>//#include </usr/lib/jvm/jdk1.7.0_79/include/jni.h>#include "inc/java/jni.h"#include "inc/java/jni_md.h"#endif#ifdef __cplusplus extern "C" { #endif JNIEXPORT void JNICALL  Java_test_HelloWorld(JNIEnv *, jclass);#ifdef __cplusplus } #endif #endif
#include "test.h"JNIEXPORT void JNICALL Java_test_HelloWorld(JNIEnv *, jclass){printf("hello world!\n");}

makefile

CXX = g++CXXFLAG =C11 = -std=c++11CUR_DIR = ./OBJ = \DLL_OBJ = test.oDEL_FILE      = rm -flibtest.so:$(OBJ) $(DLL_OBJ)$(CXX) -fPIC -shared -o  libtest.so $(OBJ) $(DLL_OBJ) -lpthread   \clean: $(DEL_FILE) *.o test.o: $(CUR_DIR)/test.cpp $(CUR_DIR)/test.h \$(CUR_DIR)/inc/java/jni.h \$(CUR_DIR)/inc/java/jni_md.h \$(CXX) -std=c++11 -fPIC -c $(CXXFLAGS) $(INCPATH) -o test.o $(CUR_DIR)/test.cpp



0 0
原创粉丝点击