2

I have python code like this:

import svmlight

training_data = __import__('data').train0
test_data = __import__('data').test0
model = svmlight.learn(training_data, type='classification', verbosity=0)
svmlight.write_model(model, 'my_model.dat')
predictions = svmlight.classify(model, test_data)

Now I have linux executable file svmlight_classify, for above code and all function are also inside this executable. This executable was create from c code using make command.

can I directly call classify() function from svmlight_classify executable file?

2 Answers 2

2

No, I don't think that you can directly call functions in an executable, but you can call functions in a shared library. You seem to have access to the C source code, so you should be able to build a shared library like this:

$ gcc -c -fPIC -o svmlight_classify.o svmlight_classify.c
$ gcc -shared -Wl,-soname,libsvmlight_classify.so -o libsvmlight_classify.so  svmlight_classify.o

This should produce a shared library named libsvmlight_classify.so. Now you can load and call its functions using ctypes:

from ctypes import cdll

lib = cdll.LoadLibrary('./libsvmlight_classify.so')
lib.classify()

Probably it will be more complicated than the above. For example, there may be arguments that need to be passed to lib.classify() of a type more complicated than int, string etc. Without the function prototype we can't advise, but the above is generally what you need to do.

Sign up to request clarification or add additional context in comments.

Comments

1

You can call C functions from DLL using ctypes standard module.

import ctypes
dll = ctypes.CDLL('your.dll') # or 'your.exe'
python_int_returned = dll.YourFunc(ctypes.c_int(3)).value

ctypes.CDLL can also be created for executable (checked it right now). I didn't check it for calling functions. Anyway you need to declare C functions in C++ code with extern "C" statement, because C++ compiler adds some "underscored" prefixes and suffixes into a binary for C++ functions by default (extern "C" turns it off).

Complicated data is often transferred to C code as a memory pointer (ctypes.POINTER can be useful).

11 Comments

I guess ctype is for windows executable like dll or exe, but in my case, I have linux executable
It should work for Linux also. Example: testlib = ctypes.CDLL('/full/path/to/testlib.so')
thanks again, I tried this ` dll = ctypes.CDLL('svm_classify')` which gives error OSError: svm_classify: cannot open shared object file: No such file or directory. The file is in same directory and also imported dll
Can you run it by subprocess.call('svm_classify') ? Full path?
yes, progress. But as svm_classify needs input parameters it gives error Not enough input parameters!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.