Я пытаюсь создать библиотеку Java с некоторым кодом CPP, используя JNI и gradle. Я выполнил следующие шаги: https://www.jetbrains.com/help/idea/setting-up-jni-development-in-gradle-project.html
Но это просто показывает шаги по настройке JNI с C. Мне нужно сделать это с CPP. Я изменил каталог c
на cpp
, обновил код до cpp и использовал плагин cpp в файле build.gradle. Но при построении он выдает мне эту ошибку:
> Task :compileHelloSharedLibraryHelloCpp FAILED
In file included from /home/karthik/gradle_try/hello/src/hello/cpp/hello.cpp:1:0:
/home/karthik/gradle_try/hello/src/hello/cpp/hello.h:2:10: fatal error: jni.h: No such file or directory
#include <jni.h>
^~~~~~~
compilation terminated.
Вот мой build.gradle
файл:
import org.gradle.internal.jvm.Jvm
/*
* This file was generated by the Gradle 'init' task.
*
* This generated file contains a sample Java Library project to get you started.
* For more details take a look at the Java Libraries chapter in the Gradle
* User Manual available at https://docs.gradle.org/6.2.2/userguide/java_library_plugin.html
*/
plugins {
// Apply the java-library plugin to add support for Java Library
id 'java-library'
id 'cpp'
}
repositories {
// Use jcenter for resolving dependencies.
// You can declare any Maven/Ivy/file repository here.
jcenter()
mavenCentral()
}
dependencies {
// This dependency is exported to consumers, that is to say found on their compile classpath.
api 'org.apache.commons:commons-math3:3.6.1'
// This dependency is used internally, and not exposed to consumers on their own compile classpath.
implementation 'com.google.guava:guava:28.1-jre'
// Use JUnit test framework
testImplementation 'junit:junit:4.12'
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
test {
systemProperty "java.library.path", file("${buildDir}/libs/hello/shared").absolutePath
}
model {
platforms {
x64 {
architecture "x64"
}
}
components {
hello(NativeLibrarySpec) {
binaries.all {
if (targetPlatform.operatingSystem.macOsX) {
cCompiler.args '-I', "${Jvm.current().javaHome}/include"
cCompiler.args '-I', "${Jvm.current().javaHome}/include/darwin"
cCompiler.args '-mmacosx-version-min=10.4'
linker.args '-mmacosx-version-min=10.4'
} else if (targetPlatform.operatingSystem.linux) {
cCompiler.args '-I', "${Jvm.current().javaHome}/include"
cCompiler.args '-I', "${Jvm.current().javaHome}/include/linux"
cCompiler.args '-D_FILE_OFFSET_BITS=64'
} else if (targetPlatform.operatingSystem.windows) {
cCompiler.args "-I${Jvm.current().javaHome}/include"
cCompiler.args "-I${Jvm.current().javaHome}/include/win32"
linker.args "Shlwapi.lib", "Advapi32.lib"
} else if (targetPlatform.operatingSystem.freeBSD) {
cCompiler.args '-I', "${Jvm.current().javaHome}/include"
cCompiler.args '-I', "${Jvm.current().javaHome}/include/freebsd"
}
}
}
}
}
test.dependsOn 'helloSharedLibrary'
Другие соответствующие файлы:
xyz.h
#include<iostream>
class xyz {
xyz(){}
void print();
};
xyz.cpp
#include "xyz.h"
void xyz::print(){
std::cout<<"Hello CPP\n";
}
hello.h
#include <jni.h>
/* Header for class hello_Library */
#ifndef _Included_hello_Library
#define _Included_hello_Library
#ifdef __cplusplus
extern "C" {
#endif
JNIEXPORT void JNICALL Java_hello_Library_print
(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endiff
#endif
hello.cpp
#include "hello.h"
#include "xyz.h"
void
Java_hello_Library_print(JNIEnv *env, jobject obj)
{
xyz o();
o.print();
}
Library.java
package hello;
public class Library {
public boolean someLibraryMethod() {
return true;
}
public native void print();
static {
System.loadLibrary("hello");
}
}
Я не получаю эту ошибку при попытке реализовать реализацию JNI C, и она работает как положено. Любые идеи о том, почему это происходит и как это решить?