首页 > 解决方案 > 在 Ubuntu 18.04 的 VIsual Studio 代码中为 C++ 设置 OpenCV

问题描述

可能有几个类似的问题,我读了大部分,但它并没有解决我的问题。这是我第一次设置这个。我会总结一下我所做的。

首先,我按照此链接从源代码(opencv 和 opencv_contrib)(版本 4.5.2)安装了 OpenCV 。之后我发现我需要 CMakeLists.txt 并将此内容放入其中:

cmake_minimum_required(VERSION 3.0.0)
project(opeet)

find_package( OpenCV 4 REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )

add_executable(opeet moje.cpp)
target_link_libraries( opeet ${OpenCV_LIBS} )

这个 CMake 文件是这样说的:

Found OpenCV: /usr/local (found suitable version "4.5.2", minimum required is "4")

所以我想它正确地找到了 OpenCV。

当我运行这个程序时:

#include<iostream>
#include<opencv2/opencv.hpp>

int main() {
   std::cout<<"hahahahha"<<std::endl;
   return 0;
}

我得到错误:opencv2/opencv_modules.hpp not found

然后我对我的目录组织做了一点改变。当我安装 OpenCV 时,它安装在目录中:

/usr/local/include/opencv4/opencv2

我将目录移动 opencv2include(因此opencv4被删除)。

所以现在 OpenCV 的目录是:

/usr/local/include/opencv2

现在上面的程序编译得很好。

但是现在如果我添加这一行(只是声明变量)cv::Mat q;,当我编译它会给我错误:

/home/faris/Desktop/Opeet/moje.cpp:5: undefined reference to `cv::Mat::Mat()'
/home/faris/Desktop/Opeet/moje.cpp:5: undefined reference to `cv::Mat::~Mat()'

现在我的痛苦开始了。我无法解决这个问题。我尝试了很多东西。我尝试添加路径,c_cpp_properties.json但没有帮助。我将发布文件,.vscode所以也许有一些错误。

c_cpp_properties.json

{
"configurations": [
    {
        "name": "Linux",
        "includePath": [
            "${workspaceFolder}/**",
            "/usr/local/include"
        ],
        "defines": [],
        "compilerPath": "/usr/bin/clang",
        "cStandard": "c11",
        "cppStandard": "c++14",
        "intelliSenseMode": "linux-clang-x64",
        "configurationProvider": "ms-vscode.cmake-tools"
    }
],
"version": 4
}

launch.json

{
"version": "0.2.0",
"configurations": [
    {
        "name": "g++ - Build and debug active file",
        "type": "cppdbg",
        "request": "launch",
        "program": "${fileDirname}/${fileBasenameNoExtension}",
        "args": [],
        "stopAtEntry": false,
        "cwd": "${fileDirname}",
        "environment": [],
        "externalConsole": false,
        "MIMode": "gdb",
        "setupCommands": [
            {
                "description": "Enable pretty-printing for gdb",
                "text": "-enable-pretty-printing",
                "ignoreFailures": true
            }
        ],
        "preLaunchTask": "C/C++: g++ build active file",
        "miDebuggerPath": "/usr/bin/gdb"
    }
]
}

tasks.json

{
"version": "2.0.0",
"tasks": [
    {
        "type": "cppbuild",
        "label": "C/C++: g++ build active file",
        "command": "/usr/bin/g++",
        "args": [
            "-g",
            "${file}",
            "-o",
            "${fileDirname}/${fileBasenameNoExtension}"
        ],
        "options": {
            "cwd": "${fileDirname}"
        },
        "problemMatcher": [
            "$gcc"
        ],
        "group": {
            "kind": "build",
            "isDefault": true
        },
        "detail": "compiler: /usr/bin/g++"
    }
]
}

如果有人可以帮助我,我将不胜感激,我不知道该怎么办了。

标签: c++opencvubuntuvisual-studio-codecmake

解决方案


推荐阅读