centos 默认gcc/g++ 版本为4.8 且使用默认命令
yum install cmake
版本为2.8 都太低,此处记录升级gcc/g++ 和cmake.
默认yum install opencv-devel
的版本是opencv2.4 编译安装opencv4.5
安装高版本GCC
yum install centos-release-scl
yum install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils
scl enable devtoolset-9 bash
gcc -v # 9.3
g++ -v
# 如果想要将此设置为默认编译器而不是系统自带的
vi ~/.bashrc # 添加 `source /opt/rh/devtoolset-9/enable`
source ~/.bashrc
安装cmake
cmake 官网
下载指定版本cmake并解压
yum install wget openssl openssl-devel mesa-lib*
wget https://github.com/Kitware/CMake/releases/download/v3.29.3/cmake-3.29.3.tar.gz
tar xvf cmake-3.29.tar.gz
cd cmake-3.29
./bootstrap && make -j20 && make install
cmake --version # 3.29.3
编译安装opencv4.5
wget https://github.com/opencv/opencv/archive/refs/tags/4.5.5.tar.gz -O opencv4.5.5.tar.gz
wget https://github.com/opencv/opencv_contrib/archive/refs/tags/4.5.5.tar.gz -O opencv_contrib4.5.5.tar.gz
tar -xvf opencv4.5.5.tar.gz
tar -xvf opencv_contrib4.5.5.tar.gz
#
cd opencv4.5.5
mkdir build
cd build
cmake -DCMAKE_BUILD_TYPE=RELEASE -DBUILD_SHARED_LIBS=ON -DOPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-4.5.5/modules/ -DOPENCV_ENABLE_NONFREE=ON -DENABLE_CXX11=ON -DWITH_OPENCL=ON DBUILD_OPENCV_WORLD=ON .. # 过程中会下载一些东西,如果没有网络可能会有不一样的结果
make -j8 # make -j$(nproc) echo $(nproc) 查看自己有几个核
make install
- CMAKE 编译安装选项
# -DCMAKE_BUILD_TYPE=RELEASE 编译发布版本.DEBUG 则可以在debug 过程中进入opencv 项目内部代码,速度略降
# -DCMAKE_INSTALL_PREFIX=/usr/local 编译后安装目录,可不选
# -DBUILD_SHARED_LIBS=ON 生成动态共享库 .so
# -DOPENCV_EXTRA_MODULES_PATH=/opencv_contrib/modules 编译额外的库
# -DOPENCV_ENABLE_NONFREE=ON 生成一些专利保护的算法函数
# -DENABLE_CXX11=ON 支持c++11 以上语法和STL
# -DWITH_CUDA=ON
# -DWITH_OPENCL=ON
# -DBUILD_OPENCV_WORLD=ON 只编译生成一个opencv_world.so 库而不是多个库
cmake ..
过程中的一些记录也不知道这些库有没有用,等使用的时候出错了再来补充一些编译依赖和选项吧
-- General configuration for OpenCV 4.5.5 =====================================
-- Version control: unknown
--
-- Extra modules:
-- Location (extra): /root/wl/opencv_contrib-4.5.5/modules
-- Version control (extra): unknown
--
-- Platform:
-- Timestamp: 2024-06-04T05:31:56Z
-- Host: Linux 4.19.12-1.el7.elrepo.x86_64 x86_64
-- CMake: 3.29.3
-- CMake generator: Unix Makefiles
-- CMake build tool: /usr/bin/gmake
-- Configuration: RELEASE
--
-- CPU/HW features:
-- Baseline: SSE SSE2 SSE3
-- requested: SSE3
-- Dispatched code generation: SSE4_1 SSE4_2 FP16 AVX AVX2 AVX512_SKX
-- requested: SSE4_1 SSE4_2 AVX FP16 AVX2 AVX512_SKX
-- SSE4_1 (18 files): + SSSE3 SSE4_1
-- SSE4_2 (2 files): + SSSE3 SSE4_1 POPCNT SSE4_2
-- FP16 (1 files): + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 AVX
-- AVX (5 files): + SSSE3 SSE4_1 POPCNT SSE4_2 AVX
-- AVX2 (33 files): + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2
-- AVX512_SKX (8 files): + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2 AVX_512F AVX512_COMMON AVX512_SKX
--
-- C/C++:
-- Built as dynamic libs?: YES
-- C++ standard: 11
-- C++ Compiler: /opt/rh/devtoolset-9/root/usr/bin/c++ (ver 9.3.1)
-- C++ flags (Release): -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG -DNDEBUG
-- C++ flags (Debug): -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -g -O0 -DDEBUG -D_DEBUG
-- C Compiler: /opt/rh/devtoolset-9/root/usr/bin/cc
-- C flags (Release): -fsigned-char -W -Wall -Werror=return-type -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -msse -msse2 -msse3 -fvisibility=hidden -O3 -DNDEBUG -DNDEBUG
-- C flags (Debug): -fsigned-char -W -Wall -Werror=return-type -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -msse -msse2 -msse3 -fvisibility=hidden -g -O0 -DDEBUG -D_DEBUG
-- Linker flags (Release): -Wl,--exclude-libs,libippicv.a -Wl,--exclude-libs,libippiw.a -Wl,--gc-sections -Wl,--as-needed
-- Linker flags (Debug): -Wl,--exclude-libs,libippicv.a -Wl,--exclude-libs,libippiw.a -Wl,--gc-sections -Wl,--as-needed
-- ccache: NO
-- Precompiled headers: NO
-- Extra dependencies: m pthread cudart_static dl rt nppc nppial nppicc nppidei nppif nppig nppim nppist nppisu nppitc npps cublas cufft -L/usr/local/cuda/lib64 -L/lib64
-- 3rdparty dependencies:
--
-- OpenCV modules:
-- To be built: aruco barcode bgsegm bioinspired calib3d ccalib core cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev datasets dnn dnn_objdetect dnn_superres dpm face features2d flann fuzzy gapi hfs highgui img_hash imgcodecs imgproc intensity_transform line_descriptor mcc ml objdetect optflow phase_unwrapping photo plot quality rapid reg rgbd saliency shape stereo stitching structured_light superres surface_matching text tracking ts video videoio videostab wechat_qrcode xfeatures2d ximgproc xobjdetect xphoto
-- Disabled: world
-- Disabled by dependency: -
-- Unavailable: alphamat cvv freetype hdf java julia matlab ovis python2 python3 sfm viz
-- Applications: tests perf_tests apps
-- Documentation: NO
-- Non-free algorithms: YES
--
-- GUI: NONE
-- GTK+: NO
-- VTK support: NO
--
-- Media I/O:
-- ZLib: /lib64/libz.so (ver 1.2.7)
-- JPEG: /lib64/libjpeg.so (ver 62)
-- WEBP: build (ver encoder: 0x020f)
-- PNG: /lib64/libpng.so (ver 1.5.13)
-- TIFF: build (ver 42 - 4.2.0)
-- JPEG 2000: OpenJPEG (ver 2.4.0)
-- OpenEXR: build (ver 2.3.0)
-- HDR: YES
-- SUNRASTER: YES
-- PXM: YES
-- PFM: YES
--
-- Video I/O:
-- DC1394: NO
-- FFMPEG: NO
-- avcodec: NO
-- avformat: NO
-- avutil: NO
-- swscale: NO
-- avresample: NO
-- GStreamer: NO
-- v4l/v4l2: YES (linux/videodev2.h)
--
-- Parallel framework: pthreads
--
-- Trace: YES (with Intel ITT)
--
-- Other third-party libraries:
-- Intel IPP: 2020.0.0 Gold [2020.0.0]
-- at: /root/wl/opencv-4.5.5/build/3rdparty/ippicv/ippicv_lnx/icv
-- Intel IPP IW: sources (2020.0.0)
-- at: /root/wl/opencv-4.5.5/build/3rdparty/ippicv/ippicv_lnx/iw
-- VA: NO
-- Lapack: NO
-- Eigen: NO
-- Custom HAL: NO
-- Protobuf: build (3.19.1)
--
-- NVIDIA CUDA: YES (ver 11.8, CUFFT CUBLAS)
-- NVIDIA GPU arch: 35 37 50 52 60 61 70 75 80 86
-- NVIDIA PTX archs:
--
-- cuDNN: NO
--
-- OpenCL: YES (no extra features)
-- Include path: /root/wl/opencv-4.5.5/3rdparty/include/opencl/1.2
-- Link libraries: Dynamic load
--
-- Python (for build): /usr/bin/python2.7
--
-- Java:
-- ant: NO
-- JNI: NO
-- Java wrappers: NO
-- Java tests: NO
--
-- Install to: /usr/local
-- -----------------------------------------------------------------
安装GPU 版本opencv
上面在编译的时候我已经选择了-DWITH_CUDA=ON
但是测试cv::cuda::GpuMat upload(img)
会报错The library is compiled without CUDA support in function 'throw_no_cuda'
,不知道啥原因,换一台机器重新安装下,以下为记录:
-
机器配置
CentOS Linux release 7.4.1708 (Core)
GPU GTX1080
Driver Version: 550.54.15
CUDA Version: 12.4 -
预安装版本
gcc 9.3
cmake 3.29
opencv 4.5.5
cuda 11.8
cudnn