转: Visual Studio 2008编译机器学习算法库Shark

看到博主:猫咪的晴天,把VS2008配置Shark写的特别详细,非常好,保留下来。http://blog.csdn.net/qtyl1988/article/details/44751071


Shark是一个快速、模块化、功能丰富且开源的C++机器学习库。它实现了线性和非线性优化、基于核函数的学习算法、神经网络等多种机器学习算法。Shark依赖于CMake和Boost,与Windows、Solaris、MacOS X和Linux兼容。当前最新的官方版本为Shark 3.0,可以从该项目的SVN上获取源码(点击打开链接

    本文描述了Windows 7(64位)上基于Visual Studio 2008编译Shark 3.0源码并安装的完整过程,同时对安装过程中产生的问题进行说明。该过程依赖于CMake 3.2.1(点击打开链接)和Boost 1.57.0(点击打开链接)。

    首先利用Visual Studio 2008中编译安装Boost。打开Visual Studio Tools目录下的Visual Studio 2008 Command Prompt,进入Boost所在目录(这里为D:\code\boost_1_57_0),输入以下命令:

  1. bootstrap --prefix="C:\Program Files\boost_1_57_0"  
bootstrap --prefix="C:\Program Files\boost_1_57_0"

    运行结果如下图所示:


      booststrap会在当前目录中生成b2.exe和bjam.exe,如下图所示。b2是整个Boost.Build构建系统的引擎,在Boost的早期版本中,该功能由bjam实现;从Boost 1.47.0开始,官方将bjam改名为b2,考虑到兼容问题,仍然保留了bjam。

扫描二维码关注公众号,回复: 5317162 查看本文章


    在当前目录中执行以下命令:

  1. b2 --build-type=complete --toolset=msvc-9.0 --layout=versioned --prefix="C:\Program Files\boost_1_57_0" --without-mpi install  
b2 --build-type=complete --toolset=msvc-9.0 --layout=versioned --prefix="C:\Program Files\boost_1_57_0" --without-mpi install

    其中,参数“--build-type=complete”使得Boost.Build建立库所支持的所有版本;“--toolset=msvc-9.0”表示用于编译Boost的工具集是msvc 9.0工具集,该工具集对应于Visual Studio 2008。“--layout=versioned”表示Boost.Build生成的库文件名应当包含Boost版本号,编译器名称及版本号,以及一些其他的构建属性;“--prefix=C:\Program Files\boost_1_57_0”指定了Boost头文件及生成库的存放目录;“--without-mpi”要求Boost.Build不构建MPI库(Message Passing Interface,一种用于在高性能并行计算中进行消息传递的库。在某些版本的Boost中,生成MPI库可能会出现“dumplicate name”错误)。上述过程完成后(大约30分钟,具体时间因机器而异),将在指定目录(此处为C:\Program Files\boost_1_57_0)中生成include和lib两个目录,至此完成boost的编译和构建,接下来的过程为Shark的编译和构建。

    然后打开CMake,将源码目录和构建目录均设为Shark根目录(此处为D:\code\Shark),如下图所示:


    点击“Add Entry”为CMake设置缓存条目(也可以用命令行方式进行,具体可参考CMake官方文档)。首先添加BOOL型参数Boost_NO_SYSTEM_PATHS,如下图所示(注意勾选Value复选框,表示将此BOOL值设置为真):


    然后添加Path型参数BOOST_ROOT,该参数用于指定Boost库所在目录(此处为C:\Program Files\boost_1_57_0),如下图所示:


    完成参数设置后,点击“Configure”按钮,选择构造器“Visual Studio 9 2008”,如下图所示:


    完成上述配置后,Configure过程开始,该过程生成一系列configure文件。运行成功后再点击“Generate”按钮,生成相应的Visual Studio工程文件(.vcproj)和解决方案文件(.sln)。


    执行Generate后,在Shark根目录下生成shark.sln文件,用Visual Studio 2008打开可以看到所有的240个工程,如下图所示:


    修改include/shark/Algorithms/Trainers/RFTrainer.h文件,添加如下代码:

  1. #include <set>  
#include <set>

    分别在debug和release模式下构建整个shark解决方案,忽略所有警告,即可得到相应的库文件。

    在编译安装Shark的过程中可能遇到的一些错误

    1. 用CMake进行Configure的过程中,出现如下错误信息:

  1. CMake Error at C:/Program Files (x86)/CMake/share/cmake-3.2/Modules/FindBoost.cmake:1182 (message):  
  2.   Unable to find the requested Boost libraries.  
  3.   
  4.   Boost version: 1.57.0  
  5.   
  6.   Boost include path: C:/Program Files/boost_1_57_0/include/boost-1_57  
  7.   
  8.   Could not find the following static Boost libraries:  
  9.   
  10.           boost_system  
  11.           boost_date_time  
  12.           boost_filesystem  
  13.           boost_program_options  
  14.           boost_serialization  
  15.           boost_thread  
  16.           boost_unit_test_framework  
  17.   
  18.   No Boost libraries were found.  You may need to set BOOST_LIBRARYDIR to the  
  19.   directory containing Boost libraries or BOOST_ROOT to the location of  
  20.   Boost.  
  21. Call Stack (most recent call first):  
  22.   CMakeLists.txt:168 (FIND_PACKAGE)  
CMake Error at C:/Program Files (x86)/CMake/share/cmake-3.2/Modules/FindBoost.cmake:1182 (message):
  Unable to find the requested Boost libraries.

  Boost version: 1.57.0

  Boost include path: C:/Program Files/boost_1_57_0/include/boost-1_57

  Could not find the following static Boost libraries:

          boost_system
          boost_date_time
          boost_filesystem
          boost_program_options
          boost_serialization
          boost_thread
          boost_unit_test_framework

  No Boost libraries were found.  You may need to set BOOST_LIBRARYDIR to the
  directory containing Boost libraries or BOOST_ROOT to the location of
  Boost.
Call Stack (most recent call first):
  CMakeLists.txt:168 (FIND_PACKAGE)
    可能原因:BOOST_ROOT设置错误,导致CMake无法找到Boost安装目录;执行b2时,工具集未选择或选择错误,导致生成的Boost库文件名称未包含或错误地包含了编译器名称和版本信息。

    2. 编译链接shark.sln过程中,出现如下编译错误:

  1. Shark\include\shark/Algorithms/Trainers/RFTrainer.h(168) : error C2976: 'std::set' : too few template arguments  
Shark\include\shark/Algorithms/Trainers/RFTrainer.h(168) : error C2976: 'std::set' : too few template arguments

    可能原因:RFTrainer.h文件未包含set头文件。

    3. 编译链接shark.sln过程中,出现如下(或类似)链接错误:

  1. LINK : fatal error LNK1104: cannot open file 'libboost_serialization-vc90-mt-gd-1_57.lib'  
LINK : fatal error LNK1104: cannot open file 'libboost_serialization-vc90-mt-gd-1_57.lib'

    可能原因:BOOST_ROOT设置错误,导致CMake无法找到Boost安装目录;执行b2时,layout参数未设置为versioned,导致生成的Boost库文件名称未包含编译器名称和版本信息。

    4. 部分工程出现如下(或类似)编译错误:

  1. Shark\include\shark/Models/Kernels/MultiTaskKernel.h(100) : error C2529: 'abstract declarator' : reference to reference is illegal  
Shark\include\shark/Models/Kernels/MultiTaskKernel.h(100) : error C2529: 'abstract declarator' : reference to reference is illegal

    可能原因:引用之引用非法。Shark 3.0中大量使用C++模板技术,时常出现双重引用的情况。可能的解决方案是利用Boost提供的Call Trait机制,将模板类中某些成员函数的参数替换为call_traits中的等价表达式,或更换至更新版本的编译器。

猜你喜欢

转载自blog.csdn.net/ADAS_LOVER/article/details/51838599
今日推荐