将C ++源代码编译为.dll
我正在尝试使用JNI创建一个简单的示例.我在编译.cpp源文件时遇到问题.我将在下面给出我已经完成/尝试过的所有步骤.我试图遵循此处找到的教程: http://java .sun.com/docs/books/jni/html/start.html#27008
I am trying to create a simple example with JNI. I am having trouble compiling the .cpp source file. I will give all the steps that I have done/tried below. I am trying to follow the tutorial found here: http://java.sun.com/docs/books/jni/html/start.html#27008
我有一个名为HelloJNI.java
public class HelloJNI
{
private native void print();
public static void main(String[] args)
{
new HelloJNI().print();
}
static
{
System.loadLibrary("HelloJNI");
}
}
从这里我编译了Java文件并命名为
From here I compiled the java file and called
javah -jni HelloJNI
生成HelloJNI.h
从这里我创建.cpp
源文件
#include <jni.h>
#include <iostream>
#include "HelloJNI.h"
using namespace std;
JNIEXPORT void JNICALL
Java_HelloJNI_print(JNIEnv *env, jobject obj)
{
cout << "Hello JNI!" << endl;
return;
}
现在我已经尝试了所有从源文件创建.dll
的工作,我正在使用此命令在cygwin上运行gcc(在此找到此命令-
Now that I have all of that I try to create the .dll
from the source file, I am using this command to run gcc on cygwin (found this command here - http://www.inonit.com/cygwin/jni/helloWorld/c.html):
gcc -mno-cygwin -I$JAVA_HOME/include -I$JAVA_HOME/include/win32
-Wl,--add-stdcall-alias -shared -o HelloJNI.dll HelloJNI.c
执行此操作时出现错误:
When I do this I get an error:
HelloJNI.cpp:1:17: fatal error: jni.h: No such file or directory
compilation terminated.
这是我遇到的问题,我真的不知道乳清编译器找不到jni.h
它在$JAVA_HOME/include
目录中.
This is where I am stuck, I don't really know whey the compiler can't find jni.h
it is in the $JAVA_HOME/include
directory.
ls $JAVA_HOME/include
的结果:
classfile_constants.h jdwpTransport.h jvmti.h win32
jawt.h jni.h jvmticmlr.h
我知道这是一篇冗长的文章,但是任何帮助都会很棒.
I know it is a lengthy post, but any help would be awesome.
谢谢
我使用以下标志进行编译:
I use the following flags to compile:
JDK = "c:/Program Files/Java/jdk1.5.0_22/"
CFLAGS=-Wall -DGCC -DWINDOWS -I$(JDK)/include/win32 -I$(JDK)/include
但是,我应该提到的是,如果该dll是使用cygwin gcc编译的,则我将无法运行我的JNI应用程序.然后,我使用Visual Studio进行编译,并且可以正常工作.
However, I should mention that I was not able to run my JNI application if the dll was compiled with cygwin gcc. I compiled with the Visual Studio then and it worked.