ubuntu 20 安装多个opencv4版本

卸载opencv装在/usr里的 ,这一步看需求

sudo rm -rf /usr/local/lib/libopencv_*
sudo rm -rf /usr/local/include/opencv
sudo rm -rf /usr/local/include/opencv2
sudo rm -rf /usr/local/include/opencv4

在Github上下载opencv4所需要的版本
解压

cd ~/Documents/opencv-4.6.0
mkdir build
cd build
mkdir installed

cmake

cmake -D CMAKE_BUILD_TYPE=Release -D OPENCV_GENERATE_PKGCONFIG=YES -D CMAKE_INSTALL_PREFIX=~/Documents/opencv-4.6.0/build/installed ..

其中OPENCV_GENERATE_PKGCONFIG=YES是opencv4必要的,如果不添加不会生成pkgconfig文件
CMAKE_INSTALL_PREFIX=~/Documents/opencv-4.6.0/build/installed ,改成自己安装的目录

make -j16
sudo make install

在~/.bashrc里添加 (不添加好像也没有什么影响)

export PKG_CONFIG_PATH=~/Documents/opencv-4.6.0/build/installed/lib/pkgconfig
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:~/Documents/opencv-4.6.0/build/installed/lib

然后保存退出,

source ~/.bashrc

在需要调用的功能包等,的Cmakelist.txt里,开头添加

set(OpenCV_DIR "~/Documents/opencv-4.6.0/build")  
find_package(OpenCV4.6.0 REQUIRED)

如:

cmake_minimum_required(VERSION 3.10)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}" ${CMAKE_MODULE_PATH})
set(OpenCV_DIR "~/Documents/opencv-4.6.0/build")  

project(TensorRTSample)

find_package(Threads)
find_package(CUDA REQUIRED)
find_package(OpenCV4.6.0 REQUIRED)
find_package(TensorRT REQUIRED)

add_executable(testlin src/testlin.cpp)

include_directories(${OpenCV_INCLUDE_DIRS}  
                  ${catkin_INCLUDE_DIRS})
target_link_libraries(testlin ${OpenCV_LIBS} ${catkin_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT})

参考博客:
https://blog.csdn.net/qq_41968210/article/details/120489307
https://blog.csdn.net/learning_tortosie/article/details/80594399

你可能感兴趣的:(ubuntu20,ubuntu,opencv,linux)