【问题标题】:VS Code cannot open source file "opencv2/opencv.hpp"VS Code 无法打开源文件“opencv2/opencv.hpp”
【发布时间】:2021-03-11 21:54:06
【问题描述】:

我正在尝试为我的项目包含 OpenCV 库。但是 VS Code 不断向我显示错误:

#include errors detected. Please update your includePath. Squiggles are disabled for this translation unit (/Users/erenerogullari/Desktop/School/DIP/dip02/main.cpp). 
cannot open the source file 'opencv2/opencv.hpp'.

即使我将它添加到“settings.json”中的包含路径中。

这是我的“main.cpp”文件:

#include <opencv2/opencv.hpp> // Error is shown here
#include <stdexcept>
#include <iostream>
#include <string>
#include <sstream>

using namespace std;
using namespace cv;

int main(int argc, char **argv){...}

还有我在 VS Code 中用于 C/C++ 扩展的“setting.json”:

{
    "editor.formatOnSave": true,
    "kite.showWelcomeNotificationOnStartup": false,
    "C_Cpp.default.includePath": ["${default}", "/Users/eren/opencv-4.5.0/include/opencv2"]
}

我使用 CMake Tools for VS Code 构建了该项目。这是我的“CMakeLists.txt”文件,如果它有任何帮助的话:

cmake_minimum_required(VERSION 3.1)

project( dip2 LANGUAGES CXX )

find_package( OpenCV REQUIRED )


add_library(code 
    Dip2.cpp
    Dip2.h
)

set_target_properties(code PROPERTIES
    CXX_STANDARD 11
    CXX_STANDARD_REQUIRED YES
    CXX_EXTENSIONS NO
)


target_link_libraries(code
    PUBLIC
        ${OpenCV_LIBS}
)

target_include_directories(code PUBLIC ${OpenCV_INCLUDE_DIRS})


add_executable(main 
    main.cpp 
)

set_target_properties(main PROPERTIES
    CXX_STANDARD 11
    CXX_STANDARD_REQUIRED YES
    CXX_EXTENSIONS NO
)

target_link_libraries(main 
    PRIVATE
        code
)

target_include_directories(main PUBLIC ${OpenCV_INCLUDE_DIRS})


add_executable(unit_test 
    unit_test.cpp 
)

set_target_properties(unit_test PROPERTIES
    CXX_STANDARD 11
    CXX_STANDARD_REQUIRED YES
    CXX_EXTENSIONS NO
)

target_link_libraries(unit_test 
    PRIVATE
        code
)

target_include_directories(unit_test PUBLIC ${OpenCV_INCLUDE_DIRS})

我在 macOS 11.0.1 上使用 Visual Studio Code 1.51.1。提前谢谢!

【问题讨论】:

  • 你需要一个 target_include_directories() 因为你没有在你的系统位置安装 opencv。
  • 将其更改为 target_link_libraries(code -L/Users/eren/opencv-4.5.0/include) 如果这就是您的意思并没有帮助
  • -L 部分是错误的。它应该是target_include_directories(code ${OpenCV_INCLUDE_DIRS}),您可能也必须对 main 和 unit_test 目标执行此操作。
  • 我已按照您的建议将其添加到所有这些中,但仍然出现相同的错误。
  • 也许你需要在 CMake 阶段重新配置。不确定您是如何在 Visual Studio Code 中使用它的。

标签: c++ opencv visual-studio-code cmake


【解决方案1】:

我能够通过使用自制软件重新安装 OpenCV 并将路径添加到 C/C++ 扩展中包含的路径来解决它:

{
"editor.formatOnSave": true,
"kite.showWelcomeNotificationOnStartup": false,
"C_Cpp.default.includePath": [
    "${default}",
    "/usr/local/Cellar/opencv/4.5.0_5/include/opencv4"
]

}

【讨论】:

    猜你喜欢
    • 2016-08-26
    • 2020-05-03
    • 1970-01-01
    • 2021-12-21
    • 1970-01-01
    • 1970-01-01
    • 2019-11-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多