How to setup Gradle project that uses Java and native C++ code?

Viewed 257

I am learning how to create a Gradle project in which I want to call native C++ code from Java using JNI. I was following this JetBrains tutorial which was working fine for me and I understand it somewhat.

But the project in this tutorial uses C native code but I would like to use C++ instead. The problem is that I tried few ways but honestly, I don't know what I am doing. My tries ended with a problem that if I build the project the libraries are not built (.dll for me since I am using windows). I get no error message the dll are just present same as if I built it with C.

How should I modify the build.gradle to build the C++ native code instead of C?

The build.gradle working for me (but using C, which I want to change) looks like this:

apply plugin: 'java'
apply plugin: 'application'
apply plugin: 'c'

mainClassName = 'HelloWorld'

repositories {
    mavenCentral()
}
dependencies {
    testCompile('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) {
            targetPlatform "x64"
            binaries.all {
                if (targetPlatform.operatingSystem.macOsX) {
                    cCompiler.args '-I', "${org.gradle.internal.jvm.Jvm.current().javaHome}/include"
                    cCompiler.args '-I', "${org.gradle.internal.jvm.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', "${org.gradle.internal.jvm.Jvm.current().javaHome}/include"
                    cCompiler.args '-I', "${org.gradle.internal.jvm.Jvm.current().javaHome}/include/linux"
                    cCompiler.args '-D_FILE_OFFSET_BITS=64'
                } else if (targetPlatform.operatingSystem.windows) {
                    cCompiler.args "-I${org.gradle.internal.jvm.Jvm.current().javaHome}/include"
                    cCompiler.args "-I${org.gradle.internal.jvm.Jvm.current().javaHome}/include/win32"
                    linker.args "Shlwapi.lib", "Advapi32.lib"
                } else if (targetPlatform.operatingSystem.freeBSD) {
                    cCompiler.args '-I', "${org.gradle.internal.jvm.Jvm.current().javaHome}/include"
                    cCompiler.args '-I', "${org.gradle.internal.jvm.Jvm.current().javaHome}/include/freebsd"
                }
            }
        }
    }
}

test.dependsOn 'helloSharedLibrary'
0 Answers
Related