1

I'm working with GraalVM coding in Java and C++ as polyglot code. I have this code to print in C++ with Java:

printMessage.cpp C++ code:

#include <iostream>

int imprimemensaje(std::string s);

int main() {
    int imprimemensaje(std::string s);
    std::cout << "Hello, C++ World!" << std::endl;
    int a = imprimemensaje("Hola mundo c++");
}

int imprimemensaje(std::string s){
    std::cout << "Mensaje desde c++ : " << s << std::endl;
    return 0;
}

and this code in Java to execute C++ object using execute():

File file = new File("printMessage");
Source source = Source.newBuilder("llvm", file).build();    
Value cpart = context.eval(source);
cpart.execute();

And print on screen this:

Hello, C++ World! Mensaje desde c++ : Hola mundo c++

I need to call only imprimemensaje() function to send data from Java, I tried with cpart.getMember("IDmember") method, but it retrieves me a null value, also cpart.getMemberKeys() has no items on it.

Does anyone know why this happened or is there another way to send data to c++ object through Java? Thanks.

Antonio
  • 115
  • 6
  • I think c++ by default mangles the names, so the member search doesn't find it. Maybe you can try it with: `extern "C" int imprimemensaje() {`, but I don't know the right answer – Oleg Šelajev Jun 29 '20 at 21:28
  • It does't work, but thanks. I change the language for C and create and .o executable. Now I can read it from context in Java GraalVM. – Antonio Jul 17 '20 at 01:44

0 Answers0