首先需要从github将这个项目的存储库克隆到本地目录中。
git clone https://github.com/dmlc/rabit.git
会在当前目录出现一个名为rabit的文件
之后从进入rabit文件,里边内容如下:
产生lib文件
lib文件夹原本是空的,在lib里README.md是这么写的:
This folder holds the library file generated by the compiler. To generate the library file, type make in the project root folder. If you want mpi compatible library, type make mpi
所以需要将文件进行编译,产生lib文件。
Makefile文件已经存在rabit目录里了,我们只需要在rabit目录下输入make
就可以了,会出现如下库文件:
这些文件的介绍如下:
List of Files
-
rabit.a The rabit package library
Normally you need to link with this one
-
rabit_mock.a The rabit package library with mock test
This library allows additional mock-test
-
rabit_mpi.a The MPI backed library
- Link against this library makes the program use MPI Allreduce
- This library is not fault-tolerant
- rabit_empty.a Dummy package implementation
- This is an empty library that does not provide anything
- Only introduced to minimize code dependency for projects that only need
single machine code
设置环境变量
库文件生成之后,需要添加环境变量CPLUS_INCLUDE_PATH
、LIBRARY_PATH
和 LD_LIBRARY_PATH
C_INCLUDE_PATH
为头文件的搜索路径
LIBRARY_PATH
为静态库搜索路径(编译时包含)
LD_LIBRARY_PATH
为动态库搜索路径(链接时包含)
这三项分别设置为:
export CPLUS_INCLUDE_PATH=/home/.../rabit/include:$CPLUS_INCLUDE_PATH
export LIBRARY_PATH=/home/.../rabit/lib:$LIBRARY_PATH
export LD_LIBRARY_PATH=/home/.../rabit/lib:$LD_LIBRARY_PATH
三种方法如下:
- 修改profile文件:
vi /etc/profile
在文档末尾写入
export CPLUS_INCLUDE_PATH=/home/.../rabit/include:$CPLUS_INCLUDE_PATH
export LIBRARY_PATH=/home/.../rabit/lib:$LIBRARY_PATH
export LD_LIBRARY_PATH=/home/.../rabit/lib:$LD_LIBRARY_PATH
- 修改.bashrc文件:
vi ~/.bashrc
在文档末尾写入
export CPLUS_INCLUDE_PATH=/home/.../rabit/include:$CPLUS_INCLUDE_PATH
export LIBRARY_PATH=/home/.../rabit/lib:$LIBRARY_PATH
export LD_LIBRARY_PATH=/home/.../rabit/lib:$LD_LIBRARY_PATH
- 在Terminal命令行窗口中直接使用export命令,临时写入
export CPLUS_INCLUDE_PATH=/home/.../rabit/include:$CPLUS_INCLUDE_PATH
export LIBRARY_PATH=/home/.../rabit/lib:$LIBRARY_PATH
export LD_LIBRARY_PATH=/home/.../rabit/lib:$LD_LIBRARY_PATH
设置完成之后测试是否成功:
编写测试文件rabittest.cpp
/* rabittest.cpp */
#include <iostream>
#include <rabit.h>
int main(int argc,char *argv[]) {
std::cout << "hellorabit\n";
rabit::Init(argc, argv);
std::cout << rabit::GetRank();
std::cout << "\n";
return(0);
}
使用如下命令编译:
g++ -o rabittest rabittest.cpp -lrabit -std=gnu++0x
如果成功,会在当前目录出现一个可执行文件
之后使用./rabittest
测试即可。
测试程序运行结果:
常见错误:
错误1:rabit.h: No such file or directory
环境变量设置出现错误,需要重新设置。
错误2:error: ‘nullptr’ was not declared in this scope
网上说是关于c++11的原因,在g++命令后加上-std=gnu++0x
这一项就可以了