首先更新yum源(不执行此步骤可能会出现无法安装资源包的情况):
cd /etc/yum.repos.d/
mv CentOS-Base.repo CentOS-Base.repo_bak
wget http://mirrors.163.com/.help/CentOS7-Base-163.repo
wget -O CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum clean all
yum makecache
安装依赖库:
yum install protobuf-devel leveldb-devel snappy-devel boost-devel gflags-devel
yum install glog-devel lmdb-devel hdf5-devel opencv-devel
yum install atlas-devel
从git上克隆caff源码
# 没有安装git的执行命令 :yum install -y git
git clone https://github.com/BVLC/caffe
cd进入caffe目录
cd caffe/
cp Makefile.config.example Makefile.config
vim Makefile.config
根据自己机器需求进行配置,配置完成后退出vim
执行编译:
make all -j10
make test -j10
make runtest -j10
本次编译在Centos7 64位上测试通过
问题补充:
- 如果不更新yum源无法下载部分依赖
- 按照上述安装步骤能够有效避免如下异常:
/usr/bin/ld: /usr/local/lib/libhdf5.a(H5.c.o): relocation R_X86_64_32 against symbol
H5_debug_g' can not be used when making a shared object; recompile with -fPIC /usr/bin/ld: /usr/local/lib/libhdf5.a(H5system.c.o): relocation R_X86_64_32 against
.rodata’ can not be used when making a shared object; recompile with -fPIC
XXX 省略若干行
/usr/bin/ld: /usr/local/lib/libhdf5.a(H5Pgcpl.c.o): relocation R_X86_64_32 against.rodata' can not be used when making a shared object; recompile with -fPIC /usr/bin/ld: /usr/local/lib/libhdf5.a(H5Toffset.c.o): relocation R_X86_64_32S against
.rodata’ can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: /usr/local/lib/libhdf5.a(H5Fcwfs.c.o): relocation R_X86_64_32S against `.rodata’ can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: 最后的链结失败: 输出不可表示的节
collect2: 错误:ld 返回 1
make: *** [.build_release/lib/libcaffe.so.1.0.0] 错误 1
在配置好caffe环境之后,如果想将已经编码好的cpp文件编译为.o文件,执行下列命令(使用CPU):
(具体路径根据本地实际路径配置)
g++ -c XXX.cpp -o FaceOne.o -I /usr/local/caffe/include/ -D CPU_ONLY -I /usr/local/caffe/.build_release/src/
或者编译为.so文件:
g++ XXX.cpp -fPIC -shared -o libFaceOne.so -I /usr/local/caffe/include/ -D CPU_ONLY
-I /usr/local/caffe/.build_release/src/
将cpp文件与上述.so文件动态库链接生成可执行文件命令如下:
g++ -o main MainOne.cpp -L. -lFaceOne -I /usr/local/caffe/include/ -D CPU_ONLY
-I /usr/local/caffe/.build_release/src/
-lboost_system
-lopencv_calib3d -lopencv_core
-lglog
-lprotobuf
-lcaffe
说明:如果提示没有找到lcaffe,则:
在caffe根目录下执行命令:
sudo cp build/lib/libcaffe.so /usr/local/lib/