vscode与Cmake基本使用
- 下载插件
- Ctrl+Shift+p打开VSCode的指令面板,然后输入cmake:q,VSCode会根据输入自动提示,然后选择CMake: Quick Start
- 选择编译器
- 根据提示输入项目名称
- 选择可执行文件
- 编译项目
- 方式一:执行命令
win下可能的问题:cd build cmake .. make
- The CMAKE_C_COMPILER is not a full path and was not found in the PATH。加上命令-DCMAKE_C_COMPILER_FORCED=ON -DCMAKE_CXX_COMPILER_FORCED=ON 关闭检测编译器
- make 时报错 Makefile:35: *** missing separator 。cmake时加上命令-G “MinGW Makefiles” 生成支持mingw的Makefile文件,默认是NMake Makefiles。
- 方式二: VSCode提供了更便捷的方式(点击底部的Build)
- 方式一:执行命令
opencv 配置
基于Cmake方式
方法一:
-
cmake文件内容
message(STATUS "Setting up OpenCV libs for: ${CMAKE_CURRENT_SOURCE_DIR}") message(STATUS "source dir: ${CMAKE_SOURCE_DIR}") include_directories("D:/MyWork/tools/dl_deploy_cpp-main/include/opencv4/") link_directories("D:/MyWork/tools/4.8.1/") set(OpenCV_LIBS opencv_world opencv_3rdparty) message(STATUS "Setting up OpenCV libs done! OpenCV_LIBS:+[${OpenCV_LIBS}]")
-
txt文件内容
cmake_minimum_required(VERSION 3.0.0) project(demo VERSION 0.1.0 LANGUAGES C CXX) add_executable(demo main.cpp)# set(CPACK_PROJECT_NAME ${PROJECT_NAME}) # set(CPACK_PROJECT_VERSION ${PROJECT_VERSION}) # include(CPack)option(INCLUDE_OPENCV "package OpenCV into dl_deploy." ON) message(STATUS "INCLUDE_OPENCV: ${INCLUDE_OPENCV}")# opencv, add library if (INCLUDE_OPENCV)include(opencv.cmake) endif ()message(STATUS "libs is: ${OpenCV_LIBS}")if (INCLUDE_OPENCV)target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS}) endif ()
cmake LINK_DIRECTORIES不起作用,找不到lib文件,原因是ADD_EXECUTABLE出现在LINK_DIRECTORIES之前。正确的做法是将LINK_DIRECTORIES放在ADD_EXECUTABLE之前。
方法二:
cmake_minimum_required(VERSION 2.8)project(test)set(DEFAULT_CXX_STANDARD 11)set(OpenCV_DIR D:/opencv/opencv3.4.6/opencv/build/x64/vc15/lib/)#库路径你自己换成你电脑上的find_package(OpenCV REQUIRED)include_directories(${OpenCV_INCLUDE_DIRS})add_executable(main ${PROJECT_SOURCE_DIR}/main.cpp)target_link_libraries(main ${OpenCV_LIBS})
基于vscode配置
无需编译快捷版
问题解决:
- 找不到链接库, 系统环境变量添加
- opencv无法定位程序输入点于动态链接库 MinGW版本与编译opencv时的版本不对应
Ubuntu 下配置Opencv
-
选择Sources下载zip源码压缩包
-
安装所需工具
#更新apt可安装包列表 sudo apt update#安装cmake和g++ sudo apt install -y cmake g++#安装项目构建工具,有两个选择,make或ninja, ninja自动支持多线程,make得自己加-j选项,这里先全安装上 sudo apt install make ninja-build
-
使用cmake构建
#进入到源码目录 cd opencv-4.9.0#cmake构建 cmake -B build -GNinja -DCMAKE_INSTALL_PREFIX=~/lib/opencv4.9.0_install
-B build :在当前目录创建一个build文件夹,然后构建到build文件夹里
-GNinja : 指定生成用Ninja来构建的项目,默认是-GUnix(用make构建)
-DCMAKE_INSTALL_PREFIX=~/lib/opencv4.9.0_install : 指定安装目录,否则默认安装到系统目
-
继续执行命令 cmake --install build/
-
设置findpackage路径
find_package(OpenCVPATHS /opt/opencvNO_DEFAULT_PATHREQUIRED) if (OpenCV_FOUND)include_directories(${OpenCV_INCLUDE_DIRS})message( ${OpenCV_LIBS}) else()message("OpenCV not found, so we won't build the project.") endif()