简体   繁体   English

如何在C ++代码中使用SVM Light? (可能不从C ++代码调用可执行文件)

[英]How is it possible use SVM Light inside the C++ code? (Possibly without invoking the executable from C++ code)

I have to use the tool SVM Light in my C++ code. 我必须在我的C ++代码中使用工具SVM Light。 I compiled and linked the SVM Light to my code like in http://svmlight.joachims.org/ but now how can I call 我像在http://svmlight.joachims.org/一样将SVM Light编译并链接到我的代码,但是现在我该如何调用

./svm_learn -v 0 -x 1  example1/train.dat example1/model

for example from my C++ code instead that from command line? 例如从我的C ++代码而不是从命令行? Namely in the original code with 即在原始代码中

./svm_learn -v 0 -x 1  example1/train.dat example1/model

i get the svm model. 我得到了svm模型。 How can I get now the same model from my C++ code?Preferably calling a function from my C++ code rather than calling the executable from command line? 现在如何从C ++代码中获得相同的模型?最好是从C ++代码中调用函数,而不是从命令行中调用可执行文件? (May be that I'm enforced to use system or similar functions to invoke the C object code (the executable) from my c++ code.... Is like this?) (可能是我被迫使用系统或类似功能从我的c ++代码中调用C对象代码(可执行文件)。是这样吗?)

(I'm using C++11 compiler, on Linux) Thanks in advance (我在Linux上使用C ++ 11编译器)在此先感谢

I found a possible solution from myself. 我从自己那里找到了可能的解决方案。 I post here for anyone was interested. 我在这里张贴任何有兴趣的人。 I modified svm_learn_main.c, I left here a empty main alone. 我修改了svm_learn_main.c,我在这里留下了一个空的主干。 I added a new file svm_mylearn.c and svm_mylearn.h . 我添加了一个新文件svm_mylearn.c和svm_mylearn.h。 I copied the original svm_learn_main.c in svm_mylearn.c. 我将原始svm_learn_main.c复制到svm_mylearn.c中。 I added here in the extern declaration the header svm_mylearn.h. 我在这里在外部声明中添加了标题svm_mylearn.h。 I moved the signature of functions in svm_mylearn.h . 我将功能的签名移到svm_mylearn.h中。 I changed the name of the main function in svm_my_exec(int,char* []) with the same code of the main. 我使用与main相同的代码在svm_my_exec(int,char * [])中更改了main函数的名称。 I modified the makefile so create the object code (the .o) for svm_mylearn.c Later naming my .cpp file Test.cpp i have to do: 我修改了makefile,因此为svm_mylearn.c创建了目标代码(.o),稍后为我的.cpp文件Test.cpp命名时,我必须这样做:

make all
g++ -c Test.cpp
g++ Test.o svm_learn.o svm_common.o svm_hideo.o svm_mylearn.o    (linking)
./a.out

Furthemore, I was forgetting that in Test.cpp I have to add a extern declaration like this: 此外,我忘记了在Test.cpp中必须添加如下这样的extern声明:

extern "C" 
{
# include "svm_common.h"
# include "svm_learn.h"
# include "svm_mylearn.h"
} 

and call the svm_my_exec function in this way(for example) : 然后以这种方式(例如)调用svm_my_exec函数:

const char *comando[]={"./svm_learn" ,"-v", "1", "-x", "1", "-o", 
"2.0", "-k" ,"100", "example1/train.dat", "example1/model"}; 
svm_my_exec(sizeof(comando)/sizeof(char *),comando );

This link http://svmlight.joachims.org/ has a DLL Interface near the bottom of the page under Extensions and Additions. 该链接http://svmlight.joachims.org/在页面底部的扩展和添加项下有一个DLL接口。 I'd start there. 我从那里开始。

Better yet, under the version history, under V6.01 - V6.02 the second sentence says "Updated makefile to add the ability for compiling SVM-light into a shared-object library that gives external code easy access to learning and classification functions." 更好的是,在版本历史记录下,在V6.01-V6.02下,第二句话说:“更新了makefile,以增加将SVM-light编译到共享库中的功能,从而使外部代码可以轻松访问学习和分类功能。 ” This library interface should be documented. 该库接口应记录在案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM