使用CMake编译Caffe的项目
最近我在编译一个Caffe的项目,在编译时候总会找不到这个依赖,那个依赖,这里总结出几条经验.
1. Caffe必须要使用CMake去编译
我一开始用作者提供的Makefile去编译Caffe,结果在编译我的项目时候,总会找不到Caffe的依赖,所以一直报错.
2. Boost库不能用apt工具去安装
apt-get工具安装虽然很简单,省去很多烦恼,我以下是也是这样,编译Caffe时候确实也没问题,但是后面编译Caffe项目时候,会有很多奇怪的问题,后来我用源码安装Boost成功,中间花费的时间简直感人.
3. CMake预编译输出
我是用CLion去编译的,这里直接附上我生成build时候控制台的输出,下面可以看到我的依赖版本.
/root/clion-2021.3.2/bin/cmake/linux/bin/cmake -DCMAKE_BUILD_TYPE=Debug -DCMAKE_MAKE_PROGRAM=/root/clion-2021.3.2/bin/ninja/linux/ninja -G Ninja /root/CLionProjects/caffe_classification_demo
-- The C compiler identification is GNU 9.4.0
-- The CXX compiler identification is GNU 9.4.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found OpenCV: /usr/local (found version "3.4.5")
-- Found Boost: /usr/local/include (found version "1.63.0") found components: filesystem system
-- Found Protobuf: /usr/local/lib/libprotobuf.so;-lpthread (found version "3.3.0")
Caffe_INCLUDE_DIRS /usr/local/dxai_caffe/src;/usr/local/include;/usr/include;/usr/local/dxai_caffe/build/include;/usr/include/hdf5/serial;/usr/local/include/opencv;/usr/include/x86_64-linux-gnu;/usr/local/dxai_caffe/include
Caffe_LIBRARIES caffe
-- Configuring done
-- Generating done
-- Build files have been written to: /root/CLionProjects/caffe_classification_demo/cmake-build-debug
[Finished]
4. CMakeLists.txt
这里也直接给上了我的CMakeLists.txt的,CMakeList.txt的find_package老是抽风,一点都不智能(吐槽下,但也可能是小弟能力不足).下面是我试了不知道多少个日夜的出来的正确写法.
cmake_minimum_required(VERSION 3.21)
project(caffe_demo)
set(CMAKE_CXX_STANDARD 14)
# cpu only
option(USE_OPENCV "whether use opencv" ON)
if (USE_OPENCV)
add_definitions(-DUSE_OPENCV)
endif()
option(CPU_ONLY "whether use cpu only" ON)
if(CPU_ONLY)
add_definitions(-DCPU_ONLY)
endif()
# 设置库目录
# OpenCV
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
# Boost
add_definitions(-DBOOST_ERROR_CODE_HEADER_ONLY)
find_package(Boost REQUIRED COMPONENTS filesystem system) # 表示使用boost的filesystem组件
include_directories(${Boost_INCLUDE_DIRS})
# protobuf
find_package(Protobuf REQUIRED)
include_directories(${Protobuf_INCLUDE_DIRS})
# glog
SET(GLOG_INC_DIR /usr/local/include)
SET(GLOG_LINK_DIR /usr/local/lib)
include_directories(${GLOG_INC_DIR})
link_directories(${GLOG_LINK_DIR})
link_libraries(glog)
# Caffe
find_package(Caffe REQUIRED)
if (Caffe_FOUND)
message("Caffe_INCLUDE_DIRS ${Caffe_INCLUDE_DIRS}")
message("Caffe_LIBRARIES ${Caffe_LIBRARIES}")
endif ()
include_directories(${Caffe_INCLUDE_DIRS})
add_executable(testDetection testDetection.cpp)
add_executable(detection detection.cpp)
## 为指定的bin文件添加三方链接库
TARGET_LINK_LIBRARIES(testDetection ${OpenCV_LIBRARIES})
TARGET_LINK_LIBRARIES(testDetection glog)
TARGET_LINK_LIBRARIES(testDetection ${Caffe_LIBRARIES})
TARGET_LINK_LIBRARIES(detection ${OpenCV_LIBRARIES})
TARGET_LINK_LIBRARIES(detection glog)
TARGET_LINK_LIBRARIES(detection ${Caffe_LIBRARIES})