记录一下在ubuntu14.04上用clion调用caffe相关库的步骤。
新建一个project,由于clion使用的是cmake,所以在cmakelist.txt里面添加相应的头文件和库文件搜索路径(/home/leo/caffe是我caffe的根目录):
cmake_minimum_required(VERSION 3.7)
project(caffe_learning)
set(CMAKE_CXX_STANDARD 11)
include_directories(/home/leo/caffe/include /usr/local/cuda/include /usr/include/opencv /usr/include/boost /home/leo/caffe/.build_debug/src)
find_library(caffe /home/leo/caffe/build/lib)
link_libraries("/home/leo/caffe/.build_debug/lib/libcaffe.so" "/usr/local/lib/libglog.so")
set(SOURCE_FILES main.cpp)
add_executable(caffe_learning ${SOURCE_FILES})
在main.cpp写一些代码测试:
//
// Created by leo on 17-5-11.
//
#include <vector>
#include <iostream>
#include "caffe/blob.hpp"
using namespace std;
using namespace caffe;
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 z = 0; z < a.width(); ++z){
cout << a.data_at(u,v,w,z) << endl;
}
}
}
}
return 0;
}
输出:
size: (0)
size: 1 2 3 4 (24)
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
当然我这只是做了最基本的测试,不能保证还会不会缺其他库和头文件,具体添加方式看之后报什么错,再具体往cmakelist.txt添加即可。
有问题欢迎留言,一起探讨
网友评论