cmake学习(三)
调用第三方库,例如OpenCV
这里用的是opencv 4.5.3版本的
#include<iostream>
#include "opencv4/opencv2/opencv.hpp"
using namespace cv;
using namespace std;
int main()
{
cv::VideoCapture cap;
cap.open(0);
while(true)
{
cv::Mat frame;
cap >> frame;
imshow("1",frame);
if (waitKey(10) == 'q')
break;
}
return 0;
}
写法1
cmake_minimum_required(VERSION 3.16)
project(demo3)
include_directories(/usr/local/include/opencv4) # 包含头文件所在的路径
include_directories(/usr/local/include/opencv4/opencv2) # 包含头文件所在的路径
link_directories(/usr/local/lib) # 链接库文件所在的路径
add_executable(opencv_demo main.cpp)
target_link_libraries(opencv_demo opencv_core opencv_highgui opencv_imgproc opencv_imgcodecs) # 链接相应的库
写法2
cmake_minimum_required(VERSION 3.16)
project(demo3)
include_directories(/usr/local/include/opencv4)
include_directories(/usr/local/include/opencv4/opencv2)
set(lib_path /usr/local/lib)
link_directories(${lib_path})
file(GLOB LIBS "${lib_path}/libopencv*.so") # 使用通配符找到所有的库,一次全部添加
add_executable(opencv_demo main.cpp)
target_link_libraries(opencv_demo ${LIBS})
写法3
cmake_minimum_required(VERSION 3.16)
project(demo3)
find_package(OpenCV REQUIRED core highgui imgproc imgcodecs)
if (OPENCV_FOUND)
message(STATUS "找到OpenCV:\"${OpenCV_INCLUDE_DIRS}\",ENABLE_OPENCV宏已打开")
message(STATUS "找到OpenCV:\"${OpenCV_LIBS}\"")
include_directories(${OpenCV_INCLUDE_DIRS})
add_definitions(-DENABLE_OPENCV)
list(APPEND LINK_LIB_LIST ${OpenCV_LIBS})
message(${LINK_LIB_LIST})
endif (OPENCV_FOUND)
add_executable(opencv_demo main.cpp)
target_link_libraries(opencv_demo ${OpenCV_LIBS})
|