Cryssie
Cryssie

Reputation: 3175

Calling C++ libraries from Python or Java using Eclipse

I am trying to call a library written in C++ called VFML (http://www.cs.washington.edu/dm/vfml/) in Python or Java using Eclipse. I am new to Python and have not programmed in C language. Is there any methods to do this that doesn't require the knowledge of C++ programming as it would take time to learn the language. Any tutorials or guides on how this can be done would help as well.

I use Eclipse to run both Python modules and Java classes. It would be great if anyone knows if it's possible to call the C++ library in Eclipse using Java or Python. Thank you.

Edit 1:

Thanks for all the answers. I was mistaken about VFML being written in C++. It was C language as pointed out. It would seems most of the answers given would suggest some kind of understanding of the C language in order to call the libraries in Python or even Java. I have heard of SWIG. Any ideas if this would be workable as without any knowledge in C language I cannot even know if I can get the library working properly. Another question would be if there is any Eclipse plugin for SWIG as I use both Python and Java with Eclipse.

Upvotes: 1

Views: 1754

Answers (2)

James Kanze
James Kanze

Reputation: 153929

You cannot call directly into C++ either in Python or in Java; you have to write some bridging code. Given that the external interface to Python and in Java is C, this bridging code can be more or less complicated: strings are char*, with all of the memory management issues that implies, and reporting errors generally require calling into the interface level to create an exception in the targeted language.

The interface code for Python is fairly simple, except for these issues, but it still requires a fairly good knowledge of C++ in order to use RAII effectively on the Python objects. The interface to Java (JNI) is inordinately complex, and requires numerous calls back into Java to do even the simplest operations, as well as all of the usual resource management.

Upvotes: 0

pepuch
pepuch

Reputation: 6516

To call c++ dll method using Java you can use Java Native Access library. It's really easy to use. All you need to do is to create interface and you can use it.

For example lets imageine that you have got dll with GetSystemIp(out char[] ip), interface for this method will look like this:

import com.sun.jna.Library;
import com.sun.jna.Native;

public interface DllLibrary extends Library {
    DllLibrary INSTANCE = (DllLibrary)
        Native.loadLibrary(dllPath, DllLibrary.class);
    int GetSystemIp(String ip);

}

You can use it in this way:

String ip;
int rc = DllLibrary.INSTANCE.GetSystemIp(String ip);

More info can be found on jna site and on wiki page.

Upvotes: 2

Related Questions