vscode与Cmake基本使用
- 下载插件
- Ctrl+Shift+p打开VSCode的指令面板,然后输入cmake:q,VSCode会根据输入自动提示,然后选择CMake: Quick Start
- 选择编译器
- 根据提示输入项目名称
- 选择可执行文件
- 编译项目
- 方式一:执行命令
cd build cmake ..
- 方式二: 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时的版本不对应