Created
March 1, 2014 05:40
-
-
Save takanuva/9285762 to your computer and use it in GitHub Desktop.
Integrating Java and C++ with GCC/CNI :)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Compile Main for Java bytecode | |
gcj -C Main.java | |
# Compile Main for native | |
gcj -findirect-dispatch -fno-indirect-classes -fpic -c Main.class -o java.o | |
# Generate header file | |
gcjh -cp . Main | |
# Compile C++ file | |
g++ -fpic -c main.cpp -o main.o | |
# Link all together | |
gcc main.o java.o -lstdc++ -lgcj -o prog |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Main headers | |
#include <cstdlib> | |
#include <cstdio> | |
#include <iostream> | |
// Include the GNU Java runtime | |
#include <gcj/cni.h> | |
// Include the Java interface (generated with gcjh!) | |
#include "Main.h" | |
// Implement the Java method! | |
jint Main::sum(jint x, jint y) { | |
using namespace std; | |
cout << "Summing in C++ ;)" << endl; | |
return x + y; | |
} | |
// | |
int main() { | |
using namespace std; | |
using namespace java::lang; | |
JvCreateJavaVM(NULL); | |
JvAttachCurrentThread(NULL, NULL); | |
//JvInitClass(&Main::class$); | |
Main *m = new Main; | |
JvDetachCurrentThread(); | |
return EXIT_SUCCESS; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Main { | |
public Main() { | |
System.out.println("In Java, gonna make a summation... =P"); | |
int r = sum(5, 10); | |
System.out.printf("Back in Java, Sum of 5 and 10 is %d%n", r); | |
}; | |
// Mark this method as native, we will implement it in C++ :) | |
public native int sum(int x, int y); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment