How to link a static library in build.gradle file for a NativeLibrarySpec


(Florian Meinke) #1

Hi,

I’m trying to build a JNI dll that uses a 3rd party library. I have the 3rd party library’s dll, lib and header files. I tried accessing functionso f that library via LoadLibrary, but have heard that this is a very clunky way to do it (and it didn’t really seem to work). I’ve read that I would need to link the static lib-file and include the necessary header files in order to not use LoadLibrary, but Whatever I try, it won’t work. During compilation of the DLL I always get the following error:

sicc.obj : error LNK2019: unresolved external symbol __imp_sic_CreateImageCore referenced in function Java_ImageCore_nativeCreateImageCore
D:\projects\jasis\build\libs\sicc\shared\sicc.dll : fatal error LNK1120: 1 unresolved externals

which basically means that the linker can’t find the static library I’m trying to link, if I’m correct.

This is my current build.gradle file:

import org.gradle.internal.jvm.Jvm

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

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
}

test {
    systemProperty "java.library.path", file("${buildDir}/libs/sicc/shared").absolutePath
}

model {
    platforms {
        x64 {
            architecture "x64"
        }
    }

    repositories {
        libs(PrebuiltLibraries) {
            SIC {
                headers.srcDir "${rootDir}/native"
                binaries.withType(StaticLibraryBinary) {
                    staticLibraryFile = file("${rootDir}/native/SimpleImageCoreC.lib")
                }
            }
        }
    }

    components {
        sicc(NativeLibrarySpec) {
            targetPlatform "x64"
            binaries {
                all {
                    cCompiler.args "-I${Jvm.current().javaHome}/include"
                    cCompiler.args "-I${Jvm.current().javaHome}/include/win32"
                    cCompiler.args "-I${rootDir}/native"
                }
                withType(NativeLibrarySpec) {
                    lib library: 'SIC', linkage: 'static'
                }
            }
        }
    }
}

(Florian Meinke) #2

Okay, apparently I was confused about the withType section. Instead of that section, I should’ve used a

sources {
                c {
                    lib library: 'SIC', linkage: 'static'
                }
            }

Now it works, so I guess I’m doing it correctly?