一文解决vscode中借助CMake配置使用Opencv过程中的所有问题

vscode中借助CMake配置使用opencv过程中的问题

vscode编译工程的完整过程

  1. 编写好CMakeLists.txt
  2. vscode中 ctrl+shift+p 选择cmake configure
  3. vscode中 ctrl+shift+p 选择cmake build

CMake问题

1. set OpenCV_FOUND to FALSE so package “OpenCV” is considered to be NOT FOUND

解决:打开opencv安装文件夹下的OpenCVConfig.cmake文件(我的文件路径是D:\opencv\opencv\build\OpenCVConfig.cmake)修改倒数第二行 将 set(OpenCV_FOUND FALSE) 改为 set(OpenCV_FOUND TRUE)
一文解决vscode中借助CMake配置使用Opencv过程中的所有问题_第1张图片

2. fatal error: opencv2/opencv.hpp: No such file or directory #include

解决,CMakeLists.txt中加上

include_directories(D:\\opencv\\opencv\\build\\include\\)
include_directories(D:\\opencv\\opencv\\build\\include\\opencv2)

我的完整的CMakeLists.txt

cmake_minimum_required(VERSION 3.5)
project(path_smoother)

add_compile_options(-std=c++11)

set(OpenCV_DIR D:\\opencv\\opencv\\build\\)
find_package(OpenCV 4.5.1 REQUIRED COMPONENTS core highgui imgproc)
include_directories(${OpenCV_INCLUDE_DIRS})
include_directories(D:\\opencv\\opencv\\build\\include\\)
include_directories(D:\\opencv\\opencv\\build\\include\\opencv2)



add_library(${PROJECT_NAME}
        bucket_queue.h
        math_utils.h
        pose2d.h
        vec2d.h
        vec2i.h
        constants.h
        path_smoother.h
        bucket_queue.cpp
        vec2d.cpp
        path_smoother.cpp
        )

target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS})

add_executable(path_smoother_example path_smoother_example.cpp)
target_link_libraries(path_smoother_example ${PROJECT_NAME} ${OpenCV_LIBS})

VSCODE问题

1. Unable to determine what CMake generator to use

解决:save workspace as… 创建一个.code-workspace文件,添加一行"cmake.generator": “Unix Makefiles”

{
	"folders": [
		{
			"path": "."
		}
	],
	"settings": {
		"cmake.generator": "Unix Makefiles"
	}
}

一文解决vscode中借助CMake配置使用Opencv过程中的所有问题_第2张图片

2. cmake configure之后 一些opencv头文件依然找不到

解决办法:头文件不在vscode默认文件夹下,因此还得再配置vscode的搜索头文件位置才能得到代码提示
ctrl shift p 选择C/C++ edit configuration
修改c_cpp_properties.json,在include path 中增加搜索文件(opencv的头文件路径)
我的文件:

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**",
                "D:/opencv/opencv/build/include/**",
                "D:/opencv/opencv/build/include/opencv2/**"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "compilerPath": "D:\\mingw\\mingw64\\bin\\gcc.exe",
            "cStandard": "c17",
            "cppStandard": "gnu++14",
            "intelliSenseMode": "windows-gcc-x64",
            "configurationProvider": "ms-vscode.cpptools"
        }
    ],
    "version": 4
}

3. 第二个问题改完之后,还找不到头文件 报错:#include errors detected based on information provided by the configuration

把c_cpp_properties.json中的 “configurationProvider”,改为ms-vscode.cpptools

opencv问题

1. CV_BGR2GRAY’ was not declared in this scope

这是opencv2的用法,现在opencv3和4是COLOR_GARY2BGR,若还报错,则应该是cv::COLOR_GARY2BGR(加上命名空间)

你可能感兴趣的:(opencv,vscode)