Gradle cpp plugin doesn't link shared library

2020-05-04 07:08发布

问题:

I have a C++ library to listen to radio with RF24. I wrap this library using JNI with a Java class.

The issue that I have is that gradle don't link the shared library librf24.so into my library libtransmitter.so.

When I call ldd on libtransmitter.so I have :

linux-vdso.so.1 (0x7ee24000)
/usr/lib/arm-linux-gnueabihf/libarmmem.so (0x76ecc000)
libstdc++.so.6 => /usr/lib/arm-linux-gnueabihf/libstdc++.so.6 (0x76d73000)
libm.so.6 => /lib/arm-linux-gnueabihf/libm.so.6 (0x76cf4000)
libgcc_s.so.1 => /lib/arm-linux-gnueabihf/libgcc_s.so.1 (0x76cc7000)
libc.so.6 => /lib/arm-linux-gnueabihf/libc.so.6 (0x76b88000)
/lib/ld-linux-armhf.so.3 (0x76ef5000)

librf24.so is not present

Here is my gradle configuration :

model {

    platforms {
        raspberry {
            architecture "arm"
            operatingSystem "linux"
        }
    }
    toolChains {
        raspToolChain(Gcc) {
            target("raspberry") {
              //  path 'C:\\SysGCC\\raspberry\\bin'
              //  cCompiler.executable 'arm-linux-gnueabihf-g++.exe'
              //  cppCompiler.executable 'arm-linux-gnueabihf-g++.exe'
              //  assembler.executable 'arm-linux-gnueabihf-g++.exe'
              //  linker.executable 'arm-linux-gnueabihf-g++.exe'
              //  staticLibArchiver.executable 'arm-linux-gnueabihf-ar.exe'
            }
        }
    }

    repositories {
        libs(PrebuiltLibraries) {
            jniLib {
                headers.srcDir projectDir.absolutePath + "\\jni\\include"
                headers.srcDir projectDir.absolutePath + "\\jni\\include\\linux"
            }
            rf24 {
                headers.srcDir projectDir.absolutePath + "\\rf24\\RF24"
                headers.srcDir projectDir.absolutePath + "\\rf24\\RF24\\utility"
                headers.srcDir projectDir.absolutePath + "\\rf24\\RF24\\utility\\RPi"
                binaries.withType(SharedLibraryBinary) {
                    sharedLibraryFile = file( projectDir.absolutePath + "\\rf24\\librf24.so")
                    sharedLibraryLinkFile = file( projectDir.absolutePath + "\\rf24\\librf24.so")
                }
            }
        }
    }

    components {
        transmitter(NativeLibrarySpec) {
            targetPlatform "raspberry"
            sources {
                cpp.lib library: 'jniLib', linkage: 'api'
                cpp.lib library: 'rf24', linkage: 'api'
            }
        }
    }

}

回答1:

Use the following instead:

cpp.lib library: 'rf24', linkage: 'shared'

This induces a transitive dependency from libtransmission to librf24.

It is not enough to put the files side by side to be found, however. You should either set LD_LIBRARY_PATH at runtime or compile libtransmission with a relative RPATH .