本例子来源于《21天实战Caffe》
代码如下:
#include <vector> #include <iostream> #include <caffe/blob.hpp> using namespace caffe; using namespace std; int main(void) { Blob<float> a; cout<<"Size : "<< a.shape_string()<<endl; a.Reshape(1, 2, 3, 4); cout<<"Size : "<< a.shape_string()<<endl; // 续上面代码 float * p = a.mutable_cpu_data(); for(int i = 0; i < a.count(); i++) { p[i] = i; } for(int u = 0; u < a.num(); u++) { for(int v = 0; v < a.channels(); v++) { for(int w = 0; w < a.height(); w++) { for(int x = 0; x < a.width(); x++) { cout<<"a["<<u<<"]["<<v<<"]["<<w<<"]["<<x<<"] = "<< a.data_at(u, v, w, x)<<endl; } } } } // 续上面代码 cout<<"ASUM = "<<a.asum_data()<<endl; cout<<"SUMSQ = "<<a.sumsq_data()<<endl; return 0; }
本机的caffe目录为/home/jourluohua/src/caffe,因此编译命令为:
g++ -o test blob_demo.cpp -I/home/jourluohua/src/caffe/include/ -D CPU_ONLY -I/home/jourluohua/src/caffe/.build_release/src/ -L/home/jourluohua/src/caffe/build/lib/ -lcaffe
但是却报
/usr/bin/ld: /tmp/cckoNotB.o: undefined reference to symbol '_ZN6google4base21CheckOpMessageBuilder7ForVar2Ev' //usr/lib/x86_64-linux-gnu/libglog.so.0: error adding symbols: DSO missing from command line collect2: error: ld returned 1 exit status
猜想是找不到libglog的问题,但是之前是使用apt安装过的,因此,加入对glog库的链接后正常:
g++ -o test blob_demo.cpp -I/home/jourluohua/src/caffe/include/ -D CPU_ONLY -I/home/jourluohua/src/caffe/.build_release/src/ -L/home/jourluohua/src/caffe/build/lib/ -lcaffe -lglog
之后加入运行库后(和Halide的使用方式一致)
export LD_LIBRARY_PATH=/home/jourluohua/src/caffe/build/lib/:$LD_LIBRARY_PATH
使用./test可得到结果