Ubuntu下编译caffe解决方案总结

本文详细记录了在Ubuntu16.04环境下搭建Caffe深度学习框架的全过程,包括解决各种常见错误的方法,如HDF5库版本不匹配、缺失文件等问题。通过本文,读者可以了解如何正确配置环境并解决安装过程中遇到的各种问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  下一届学生要在我的项目的基础上开展大学生科研项目,所以记录下来给自己以后借鉴使用,也可以帮助他们少走一些弯路。
  caffe这个深度学习框架学要得对环境的要求和依赖的使用非常多,很容易出现问题,网上关于caffe的安装教程非常多,但是关于每一步是否操作成功,出现了什么样的错误又该如何处理没有给出说明。
  操作系统的环境千差万别,按照博客中的教程一步步的安装,最后可能失败。建议一定要先查看官方文档,不要盲目复制别人技术博客的命令。
  caffe官方给出的ubuntu下的caffe.berkeleyvision.org教程

一切不说具体实验环境的配置都是耍流氓

环境

操作系统: Ubuntu 16.04
GCC/G++:5.4.3
OpenCV: 2.4.11和3.1.0
Matlab :R2014b(a)
Python: 2.7

“fatal error: hdf5.h: 没有那个文件或目录”

解决方法:
1.修改 Makefile.config 文件内容:

将
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib 
修改为: 
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/include/hdf5/serial
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib /usr/lib/x86_64-linux-gnu /usr/lib/x86_64-linux-gnu/hdf5/serial       

2.修改 caffe 目录下的 Makefile 文件:

将:
LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_hl hdf5
修改为:
LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_serial_hl hdf5_serial

apt-get软件包没有被完全安装或卸载解决

错误是 x的 pre-remove 脚本引发的,导致不能正常卸载这个包
解决方法:
/var/lib/dpkg/info 中找到 x.prerm 修改,清除里面的脚本,然后删除这个受损的包,可以利用

apt-get remove --purge x

直接删除这个包

leveldb/db.h:没有那个文件或目录

解决方法:
levelDB是一个快速的key-value (String,String)存储库,由Google编写。
1.下载levelDB文件:

git clone https://github.com/google/leveldb.git

2.到levelDB目录下,执行make命令
执行完成后:
leveldb/下多出out-shared和out-static目录,其中out-shared/下有:

db db_bench helpers libleveldb.so libleveldb.so.1 libleveldb.so.1.19 port table util

3.将文件拷贝

sudo cp out-shared/libleveldb.so* /usr/local/lib & sudo cp -R include/* /usr/local/include

make: protoc: 命令未找到

解决方法:

sudo apt-get install protobuf-c-compiler protobuf-compiler

fatal error: gflags/gflags.h:没有那个文件或目录

解决方法:

sudo apt-get install libgflags-dev

fatal error: glog/logging.h: 没有那个文件或目录

解决方法:

sudo apt-get install libgoogle-glog-dev

fatal error: hdf5.h: 没有那个文件或目录

解决方法:

sudo apt-get install libhdf5-*

fatal error: lmdb.h: 没有那个文件或目录

解决方法:

sudo apt-get install liblmdb-dev

./include/caffe/util/cudnn.hpp:5:19: fatal error: cudnn.h: 没有那个文件或目录

解决方法:在Makefile.config中添加#include

error: ‘GetSolver’ is not a member of ‘caffe’

解决方法:

mySolver.reset(caffe::GetSolver(solver_param));
修改为
 mySolver.reset(caffe::SolverRegistry::CreateSolver(solver_param));

caffe/blob.hpp:9:34: fatal error: caffe/proto/caffe.pb.h: No such file or directory

解决方法:
用protoc从caffe/src/caffe/proto/caffe.proto生成caffe.pb.h和caffe.pb.cc

~/caffe/src/caffe/proto$ protoc --cpp_out=/当前路径 caffe.proto

Makefile:594: recipe for target ‘.build_release/cuda/src/caffe/layers/power_layer.o’ failed

解决方法:
1. Makefile.config 中 CUDA_ARCH 设置未按规定设置(CUDA 9.0)

    # CUDA architecture setting: going with all of them.  
    # For CUDA < 6.0, comment the *_50 through *_61 lines for compatibility.  
    # For CUDA < 8.0, comment the *_60 and *_61 lines for compatibility.  
    # For CUDA >= 9.0, comment the *_20 and *_21 lines for compatibility.  
    CUDA_ARCH := -gencode arch=compute_20,code=sm_20 \  
                    -gencode arch=compute_20,code=sm_21 \  
                    -gencode arch=compute_30,code=sm_30 \  
                    -gencode arch=compute_35,code=sm_35 \  
                    -gencode arch=compute_50,code=sm_50 \  
                    -gencode arch=compute_52,code=sm_52 \  
                    -gencode arch=compute_60,code=sm_60 \  
                    -gencode arch=compute_61,code=sm_61 \  
                    -gencode arch=compute_61,code=compute_61  

2.删除版本不兼容的代码即可

    -gencode arch=compute_20,code=sm_20 \  
    -gencode arch=compute_20,code=sm_21 \  

3.对于makefile.config文件,可以用\来换行,此时要注意,在一行的最后面加上\,而这个\后面不能再有任何字符,哪怕是空格也不允许,否则出错。

error while loading shared libraries: libcudnn.so.7: cannot open shared object file: No such file or directory

解决方法:
1.lib文件夹是在系统路径里的,用ls -al发现是文件权限的问题,因此用下述命令先删除软连接

cd /usr/local/cuda/lib64/

sudo rm -rf libcudnn.solibcudnn.so.7

2.修改文件权限,并创建新的软连接

sudo chmod u=rwx,g=rx,o=rx libcudnn.so.7.1.2

sudo ln -s libcudnn.so.7.1.2 libcudnn.so.7

sudo ln -s libcudnn.so.7 libcudnn.so

sudo ldconfig -v

Makefile:532: recipe for target ‘runtest’ failed

解决方法:

export MKL_CBWR=AUTO
export CUDA_VISIBLE_DEVICES=0

Check failed: error == cudaSuccess (8 vs. 0) invalid device function

解决方法:
1.修改Makefile.config中的这一部分

# CUDA architecture setting: going with all of them (up to CUDA 5.5 compatible).
# For the latest architecture, you need to install CUDA >= 6.0 and uncomment
# the *_50 lines below.
CUDA_ARCH := -gencode arch=compute_20,code=sm_20 \
             -gencode arch=compute_20,code=sm_21 \
             -gencode arch=compute_30,code=sm_30 \
             -gencode arch=compute_35,code=sm_35
             -gencode=arch=compute_50,code=sm_50 \
             -gencode=arch=compute_50,code=compute_50 \

2.查询N卡计算能力,将Makefile.config文件中的CUDA_ARCH参数更改,注释掉不适用的参数

Check failed: error == cudaSuccess (35 vs. 0) CUDA driver version is insufficient for CUDA runtime version

解决方法:
1.在双显卡系统中,cuda运行时NVIDAI显卡必须是当前使用的显卡,否则无法获取GPU设备,cudaGetDeviceCount函数会报错,错误码35。
2.使用nvidia-prime切换到Nvidia显卡时,如果只是按照提示logout,再重新login是不行的,必须重启系统,否则会报错,错误码30。
3.caffe调用cuda的cudaGetDeviceCount函数获取GPU设备,执行

nvidia-setting

4.在PRIME profile中果然显示当前使用的是Intel 集成显卡,于是切换到nvidia显卡,重启

error while loading shared libraries: libcublas.so.8.0: cannot open shared object file: No such file or directory

解决方法:
cd到caffe目录下

cd caffe/

1.make all
2.make test
3.make runtest

HDF5 library and header mismatch error

解决方法:
安装caffe的过程进行到sudo make runtest时,提示HDF5 header version与HDF5 library不匹配,主要是anaconda和系统自带的发生了冲突,系统中h5py库存在多个版本,系统自带的python下有以前的h5py版本,所以只保留新版本
1.到hdfgroup.org官网下载对应的版本
请先注意PATHON环境变量配置正确
我的提示是Headers are 1.10.1, library is 1.8.16
2.卸载系统自带的HDF5旧版本

sudo conda uninstall h5py

3.解压安装新版本

tar -xvf hdf5-1.8.16.tar
cd hdf5-1.8.16
./configure --prefix=/usr/local/hdf5 
make
make check
make install
make check-install
conda install h5py

总结:

  编译环境和依赖的不同,每个人产生的问题也会不一样。但是大部分情况第一、二两种解决方法可以解决绝大多数问题。cuda、cudnn、opencv版本兼容问题要优先考虑,安装caffe时一定要加倍细心。

-- Build files have been written to: /root/Workspace/openpose/build/caffe/src/openpose_lib-build [ 75%] Performing build step for 'openpose_lib' [ 1%] Running C++/Python protocol buffer compiler on /root/Workspace/openpose/3rdparty/caffe/src/caffe/proto/caffe.proto [ 1%] Building CXX object src/caffe/CMakeFiles/caffeproto.dir/__/__/include/caffe/proto/caffe.pb.cc.o [ 1%] Linking CXX static library ../../lib/libcaffeproto.a [ 1%] Built target caffeproto [ 1%] Building NVCC (Device) object src/caffe/CMakeFiles/cuda_compile_1.dir/util/cuda_compile_1_generated_math_functions.cu.o [ 1%] Building NVCC (Device) object src/caffe/CMakeFiles/cuda_compile_1.dir/layers/cuda_compile_1_generated_absval_layer.cu.o [ 2%] Building NVCC (Device) object src/caffe/CMakeFiles/cuda_compile_1.dir/layers/cuda_compile_1_generated_base_data_layer.cu.o [ 2%] Building NVCC (Device) object src/caffe/CMakeFiles/cuda_compile_1.dir/layers/cuda_compile_1_generated_accuracy_layer.cu.o [ 2%] Building NVCC (Device) object src/caffe/CMakeFiles/cuda_compile_1.dir/layers/cuda_compile_1_generated_batch_norm_layer.cu.o [ 2%] Building NVCC (Device) object src/caffe/CMakeFiles/cuda_compile_1.dir/layers/cuda_compile_1_generated_batch_reindex_layer.cu.o [ 4%] Building NVCC (Device) object src/caffe/CMakeFiles/cuda_compile_1.dir/layers/cuda_compile_1_generated_bnll_layer.cu.o [ 4%] Building NVCC (Device) object src/caffe/CMakeFiles/cuda_compile_1.dir/layers/cuda_compile_1_generated_bias_layer.cu.o In file included from /root/Workspace/openpose/3rdparty/caffe/src/caffe/util/math_functions.cu:1: /usr/local/cuda-11.8/include/math_functions.h:54:2: warning: #warning "math_functions.h is an internal header file and must not be used directly. This file will be removed in a future CUDA release. Please use cuda_runtime_api.h or cuda_runtime.h instead." [-Wcpp] 54 | #warning "math_functions.h is an internal header file and must not be used directly. This file will be removed in a future CUDA release.
最新发布
03-15
<think>好的,我现在要解决用户提到的CUDA编译时出现的math_functions.h警告问题,并寻找替代方案,比如使用cuda_runtime_api.h或cuda_runtime.h。首先,我需要回忆一下CUDA头文件的结构和用途。 用户提到的警告可能是因为math_functions.h被标记为内部头文件,不建议直接引用。根据CUDA的文档,开发人员应该使用更高级别的头文件,比如cuda_runtime.h或cuda_runtime_api.h,这些头文件会自动包含必要的内部文件。所以,用户需要检查他们的代码中是否有直接包含math_functions.h的情况,并替换为官方推荐的头文件。 接下来,用户可能还需要检查编译环境是否正确配置。例如,确保CUDA安装路径正确,并在编译时用-I指定包含路径,如-I/usr/local/cuda/include。此外,交叉编译时可能需要安装对应的工具链,如用户提到的aarch64-linux-gnu工具链,这可能涉及到引用[4]中的内容。 另外,用户提供的引用[3]中的代码涉及到CUDA和OpenGL的互操作,这可能与他们的项目相关,但当前问题主要集中在头文件引用上。需要确认在链接阶段是否正确链接了CUDA库,比如使用-lcudart。 此外,用户提到的警告信息可能类似于“math_functions.h is an internal header file and should not be used directly.”这时,建议用户检查所有源文件中是否有直接包含该头文件的地方,并替换为cuda_runtime.h,因为后者已经包含了必要的数学函数声明。 还可能要考虑代码中使用的数学函数是否有替代方案。例如,CUDA的运行时API提供了一些数学函数,或者使用标准库函数,但需要注意设备代码和主机代码的区别。例如,在设备代码中应使用__sin、__cos等内置函数,而不是标准库的sin、cos,这些可能在math_functions.h中定义,但现在应该通过包含正确的头文件来访问。 最后,用户需要确保编译命令正确,包括正确的CUDA版本和路径设置。如果用户遇到找不到头文件的错误,可能需要检查CUDA安装是否正确,或者参考引用[2]中的解决方案,安装相应的开发包,如nvidia-cuda-dev。</think>### 解决CUDA编译时`math_functions.h`警告的替代方案 #### 1. **问题分析** CUDA的`math_functions.h`是内部头文件,直接包含会触发警告: ```bash warning: math_functions.h is an internal header file and should not be used directly. ``` 官方建议使用`cuda_runtime.h`或`cuda_runtime_api.h`替代,因为它们已封装了必要的数学函数接口[^1]。 --- #### 2. **替代方案** ##### (1) 使用`cuda_runtime.h`替代 - **作用**:包含CUDA运行时API的核心接口和数学函数(如`sin`、`cos`、`sqrt`等)。 - **修改代码**: ```c // 原代码(不推荐) #include <math_functions.h> // 修改为(推荐) #include <cuda_runtime.h> ``` ##### (2) 使用标准库函数(仅限主机代码) - **场景**:在主机代码(CPU端)中使用标准数学函数。 - **示例**: ```c #include <cmath> // 标准C++数学库 // 主机代码中使用std::sin、std::sqrt等 ``` ##### (3) 使用CUDA内置函数(设备端代码) - **场景**:在设备端(GPU核函数)中直接调用CUDA内置数学函数。 - **示例**: ```cuda __global__ void kernel(float* data) { int idx = threadIdx.x; data[idx] = __sinf(data[idx]); // 使用CUDA内置单精度正弦函数 } ``` --- #### 3. **编译配置调整** ##### (1) 确保CUDA头文件路径正确 在编译命令中指定CUDA头文件路径: ```bash nvcc -I/usr/local/cuda/include your_code.cu -o output ``` ##### (2) 链接CUDA运行时库 添加`-lcudart`参数: ```bash nvcc your_code.cu -o output -lcudart ``` ##### (3) 交叉编译配置(如适用) 若需交叉编译(如ARM架构),安装对应工具链: ```bash sudo apt-get install gcc-aarch64-linux-gnu g++-aarch64-linux-gnu # 引用[4] ``` --- #### 4. **验证代码示例** ```cuda #include <cuda_runtime.h> // 替代math_functions.h #include <cmath> // 主机端标准数学库 __global__ void compute(float* d_data) { int idx = blockIdx.x * blockDim.x + threadIdx.x; d_data[idx] = __expf(d_data[idx]); // 设备端使用内置指数函数 } int main() { float* d_data; cudaMalloc(&d_data, 100 * sizeof(float)); compute<<<1, 100>>>(d_data); cudaFree(d_data); return 0; } ``` --- #### 5. **常见问题解决** - **头文件缺失错误**:确保已安装CUDA开发包(如`nvidia-cuda-dev`)[^2]。 - **OpenGL互操作问题**:若涉及CUDA与OpenGL交互,需正确注册缓冲区(参考引用[3])。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

极客范儿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值