javacpp icon indicating copy to clipboard operation
javacpp copied to clipboard

C++ and Java lambda support?

Open ghost opened this issue 4 years ago • 7 comments

I want to ask a question. Can Java lambda expressions be used with C++ versions of Lambda expressions (i.e. std::function)? Moreover, the "wrapping" in one direction and the other?

javaCb(cppLm)
cppCb(javaLm)

ghost avatar Feb 19 '20 13:02 ghost

It's possible, yes, but JavaCPP doesn't have any special support for std::function, yet. We can define a FunctionPointer as well as @Virtual functions apparently and use that to initialize a std::function: https://stackoverflow.com/questions/47986978/virtual-functions-and-stdfunction However, it's probably a good idea to stick with FunctionPointer...

To invoke an std::function, since its exact definition changes depending on the compiler, we need to map it manually, as demonstrated here, for example: https://github.com/bytedeco/javacpp/wiki/Interface-Thrust-and-CUDA Simply calling operator() on an instance of std::function is probably easiest.

Let me know if you have problems coming up with a working example though! Thanks

saudet avatar Feb 19 '20 19:02 saudet

But can Java methods invoked by C++ code (i.e. another direction), with accept parameters and return values? I.e. place Java function for handle by native code? Here bit rude example of mean... Bit same issue with yield based C++ methods, when asking Java to action (with argument get), and answer to C++ context back, then complete C++ procedure by return.

void add(int a, int b, Uint32Ptr handly) {
  handly.assign(a+b);
}
int example(){
  uint32_t result = 0;
  java::add(5,5,&result);
  // and get 10 from `result`
}

ghost avatar Feb 20 '20 03:02 ghost

Yes, as I mentioned above, that works with FunctionPointer, like this: https://github.com/bytedeco/javacpp#creating-callback-functions

saudet avatar Feb 20 '20 07:02 saudet

Tried to compile example above, but getting error with unresolved symbols. https://gist.github.com/helixd-2k18/ca223100d2377d46406b467b0a91c756

/out:jniFoo.dll
/ltcg
/dll
/implib:jniFoo.lib
/OUT:jniFoo.dll
"/LIBPATH:C:\Program Files\Java\jdk-13.0.1\lib"
"/LIBPATH:C:\Program Files\Java\jdk-13.0.1\bin\server"
psapi.lib
jvm.lib
jniFoo.obj
jnijavacpp.obj
   Creating library jniFoo.lib and object jniFoo.exp
jniFoo.obj : error LNK2001: unresolved external symbol __imp__JNI_CreateJavaVM@12
jniFoo.obj : error LNK2001: unresolved external symbol __imp__JNI_GetCreatedJavaVMs@12
jniFoo.dll : fatal error LNK1120: 2 unresolved externals
Exception in thread "main" java.lang.RuntimeException: Process exited with an error: 2
        at org.bytedeco.javacpp.tools.Builder.generateAndCompile(Builder.java:644)
        at org.bytedeco.javacpp.tools.Builder.build(Builder.java:1107)
        at org.bytedeco.javacpp.tools.Builder.main(Builder.java:1341)

ghost avatar Feb 20 '20 07:02 ghost

Does this happen only with JDK 13, or does it also happen with JDK 8?

saudet avatar Feb 20 '20 07:02 saudet

Ah, here's a possible solution: https://stackoverflow.com/a/23954033/523744

saudet avatar Feb 20 '20 08:02 saudet

Problem has with Developer Command Prompt for VS 2019. But from .bat file with that content compiles successfully.

call vcvarsall.bat x64
call vcvars64.bat
call java -jar javacpp.jar JiviX.java -header 
pause

ghost avatar Feb 20 '20 11:02 ghost