将C ++源代码编译为.dll [英] Compile C++ source to a .dll

查看:241
本文介绍了将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.

这篇关于将C ++源代码编译为.dll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆