bytedeco / sample-projects

Sample project files for JavaCPP, JavaCPP Presets, and JavaCV
219 stars 174 forks source link

Unable to interact with shared objects (.so files) #47

Closed nitin2212 closed 4 years ago

nitin2212 commented 4 years ago

Hi, I am trying to load a pre-compiled dynamic library ( libabc.so ) which contains extern functions declared in header and cpp files. But I am getting error during compilation.

The header and CPP file are as below : [ export.h ] extern "C" { void multiply(int a, float b); }

[ export.cpp ] _#include "export.h"

include < iostream >

using namespace std; extern "C" { void multiply (int a, float b) { cout << "Hello-World\n"; } }_

I compiled them into a shared object : libabc.so and copied it inside the src/main/java/somepackage/* . ( I am using the sample demo given here : https://github.com/bytedeco/sample-projects/blob/master/javacpp-mvn-simple-demo/ )

I am trying to load the library from Java and call the methods as shown below :

package somepackage;

import org.bytedeco.javacpp.; import org.bytedeco.javacpp.annotation.;

@Platform(library="abc") public class MultiplyDemo {

static {
    Loader.load();
}

public native void multiply(int a, float b);

public static void main(String[] args) {

MultiplyDemo mul = new MultiplyDemo();
    mul.multiply(123, 100f);
}    

}


I am getting the error : [INFO] --- javacpp:1.0:build (process-classes) @ javacpp-mvn-simple-demo --- [INFO] Executing JavaCPP Builder [INFO] Generating /home/necuser/nitin/bridge/javacpp-mvn-simple-demo/target/classes/somepackage/jniMultiplyDemo.cpp [WARNING] The class somepackage.MultiplyDemo does not map to any C++ type. Compilation will most likely fail. [WARNING] The class somepackage.MultiplyDemo does not map to any C++ type. Compilation will most likely fail. [WARNING] The class somepackage.MultiplyDemo does not map to any C++ type. Compilation will most likely fail. [WARNING] The class somepackage.MultiplyDemo does not map to any C++ type. Compilation will most likely fail. [INFO] Compiling /home/necuser/nitin/bridge/javacpp-mvn-simple-demo/target/classes/somepackage/linux-x86_64/libjniMultiplyDemo.so [INFO] g++ -I/home/necuser/nitin/bridge/javacpp-mvn-simple-demo/src/main/java -I/usr/lib/jvm/java-8-openjdk-amd64/include -I/usr/lib/jvm/java-8-openjdk-amd64/include/linux /home/necuser/nitin/bridge/javacpp-mvn-simple-demo/target/classes/somepackage/jniMultiplyDemo.cpp -Wl,-rpath,$ORIGIN/ -march=x86-64 -m64 -Wall -O3 -fPIC -shared -s -o /home/necuser/nitin/bridge/javacpp-mvn-simple-demo/target/classes/somepackage/linux-x86_64/libjniMultiplyDemo.so /home/necuser/nitin/bridge/javacpp-mvn-simple-demo/target/classes/somepackage/jniMultiplyDemo.cpp: In function ‘void Java_somepackage_MultiplyDemo_add_1Conv(JNIEnv*, jobject, jint, jfloat)’: /home/necuser/nitin/bridge/javacpp-mvn-simple-demo/target/classes/somepackage/jniMultiplyDemo.cpp:1054:6: error: ‘ptr’ was not declared in this scope ptr = ()jlong_to_ptr(env->GetLongField(obj, JavaCPP_addressFID)); ^~~ /home/necuser/nitin/bridge/javacpp-mvn-simple-demo/target/classes/somepackage/jniMultiplyDemo.cpp:1054:13: error: expected primary-expression before ‘)’ token ptr = ()jlong_to_ptr(env->GetLongField(obj, JavaCPP_addressFID));

I am new to JavaCPP. Please help.

saudet commented 4 years ago

You're missing an "include" in the @Platform annotation. Try to add it.

nitin2212 commented 4 years ago

@saudet This worked: @Platform(include="somepackage/export.cpp", library="abc") and making the method as native static

But is there some way that I can use only the library libabc.so to call the extern "C" functions ? Like in python ctypes, we use, caller = ctypes.loadDllLibrary("abc") caller.multiply(4, 5.0)

Thanks a lot !!

saudet commented 4 years ago

Not really, JavaCPP doesn't support that at the moment. We would need to use a wrapper for libffi or something like JNA or JNR do, but it's a lot slower than making calls with a C/C++ compiler, so it's not something I would recommend to do anyway.

/cc @yukoba

nitin2212 commented 4 years ago

Alright @saudet. Thanks for the help !

saudet commented 4 years ago

Duplicate of https://github.com/bytedeco/javacpp/issues/416