Build Libsvm to dll

0. Environment

Windows 8

Visual studio 2012

Libsvm 3.14 (libsvm-3.14.zip)

1. Steps

1.1 Create a Win32 empty DLL project and set (in Project->$Project_Name Properties...->Configuration) to "Release."

1.2 Add svm.cpp, svm.h, svm-train.c, svm-predict.c, svm-scale.c and svm.def to your project.

1.3 Add __WIN32__ and _CRT_SECURE_NO_DEPRECATE to Preprocessor definitions (in Project->$Project_Name Properties...->C/C++->Preprocessor)

1.4 Set Create/Use Precompiled Header to "Not Using Precompiled Headers" (in Project->$Project_Name Properties...->C/C++->Precompiled Headers)

1.5 Set the path for the "Modulation Definition File" to svm.def (in Project->$Project_Name Properties...->Linker->input

1.6 Add dll.cpp file and add the following code:

extern "C" __declspec(dllexport) int libsvmdll_svm_train()
{
    int paramNumber = 6;
    int paramWordLength = 10;
    int argc = paramNumber;
    char** argv = (char**)malloc(sizeof(char*) * paramNumber);
    for (int i = 0; i < paramNumber; ++i)
    {
        argv[i] = (char*)malloc(sizeof(char) * paramWordLength);
    }
    argv[0] = "svm-train";
    argv[1] = "-s";
    argv[2] = "0";
    argv[3] = "-t";
    argv[4] = "2";
    argv[5] = "libsvm.train";
    return svm_train_main(argc, argv);
}

1.7 Add some other code you like(such as predict code) and then build the DLL.

2. References

http://www.csie.ntu.edu.tw/~cjlin/libsvm/faq.html#f211

(This article is from http://www.cnblogs.com/chenyineng/archive/2012/12/24/2830467.html, and belongs to http://chenyineng.cnblogs.com and http://www.chenyineng.info)

原文地址:https://www.cnblogs.com/chenyineng/p/2830467.html