首页 > 解决方案 > Opencv C++ MinGW VSCode 致命错误编译

问题描述

大家好,我想使用 VSCode MinGW C++ 和 OpenCV 创建一个简单的 opencv 项目,但由于未知原因,我收到此错误,我该怎么办?

我想提一下,在Visual studio 2017工作中我可以在 x64 架构上运行 main.cpp。

下面的代码是我想在 VSCode 上运行的代码,与我在 Visual Studio 2017 上运行的代码相同。

50 points经过 10 天的尝试,如果有人证明了这一点,我会放弃:

一个成功的构建。

src/main.cpp

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main() {

    Mat image;
    image = imread("./22.png", IMREAD_COLOR); // Read the file

    namedWindow("Display window", WINDOW_AUTOSIZE); // Create a window for display.

    if (!image.data) // Check for invalid input
    {
        cout << "Could not open or find the image" << std::endl;
    } else {        // Image is good!

        imshow("Display window", image); // Show our image inside it.
    }

    waitKey(0);
    return 0;
}

在我的 VSCode 编辑器中,我尝试使用 tasks.json 构建应用程序CTRL + SHIFT + B

任务.json

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "echo",
            "type": "shell",
            "command": "",
            "args": [
                "g++", 
                "-I", "C:\\vcpkg\\installed\\x64-windows\\include",
                "-L", "C:\\vcpkg\\installed\\x64-windows\\lib",
                "./src/main.cpp",
                "-lopencv_core341",
                "-lopencv_highgui341",
                "-o app"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

c_cpp_properties.json

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**",
                "${workspaceFolder}/src",
                "C:/vcpkg/installed/x64-windows/include",
                "C:/vcpkg/installed/x64-windows/lib"

            ],
            "browse": {
                "path": [

            ],
                "limitSymbolsToIncludedHeaders": true
            },
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "windowsSdkVersion": "10.0.17134.0",
            "compilerPath": "C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.14.26428/bin/Hostx64/x64/cl.exe",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "msvc-x64",
            "C_Cpp.intelliSenseEngine": "Tag Parser"
        }
    ],
    "version": 4
}

我得到这个错误

> Executing task: g++ main.cpp -I C:/vcpkg/installed/x64-windows/include -L C:/vcpkg/installed/x64-windows/lib -lopencv_core341 -lopencv_highgui341 -o app <

C:\Users\giorg\AppData\Local\Temp\ccNFIHQq.o:main.cpp:(.text+0x51): undefined reference to `cv::imread(cv::String const&, int)'
C:\Users\giorg\AppData\Local\Temp\ccNFIHQq.o:main.cpp:(.text+0xa2): undefined reference to `cv::namedWindow(cv::String const&, int)'
C:\Users\giorg\AppData\Local\Temp\ccNFIHQq.o:main.cpp:(.text+0x119): undefined reference to `cv::imshow(cv::String const&, cv::_InputArray const&)'
C:\Users\giorg\AppData\Local\Temp\ccNFIHQq.o:main.cpp:(.text+0x139): undefined reference to `cv::waitKey(int)'
C:\Users\giorg\AppData\Local\Temp\ccNFIHQq.o:main.cpp:(.text$_ZN2cv6StringC1EPKc[__ZN2cv6StringC1EPKc]+0x42): undefined reference to `cv::String::allocate(unsigned int)'
C:\Users\giorg\AppData\Local\Temp\ccNFIHQq.o:main.cpp:(.text$_ZN2cv6StringD1Ev[__ZN2cv6StringD1Ev]+0xf): undefined reference to `cv::String::deallocate()'
C:\Users\giorg\AppData\Local\Temp\ccNFIHQq.o:main.cpp:(.text$_ZN2cv3MatD1Ev[__ZN2cv3MatD1Ev]+0x2d): undefined reference to `cv::fastFree(void*)'
C:\Users\giorg\AppData\Local\Temp\ccNFIHQq.o:main.cpp:(.text$_ZN2cv3Mat7releaseEv[__ZN2cv3Mat7releaseEv]+0x40): undefined reference to `cv::Mat::deallocate()'
C:\Users\giorg\AppData\Local\Temp\ccNFIHQq.o:main.cpp:(.text$_ZN2cv3MataSEOS0_[__ZN2cv3MataSEOS0_]+0xb4): undefined reference to `cv::fastFree(void*)'
collect2.exe: error: ld returned 1 exit status
The terminal process terminated with exit code: 1

Terminal will be reused by tasks, press any key to close it.

我已经使用vcpkgusing this 命令生成了 opencv 库vcpkg install opencv,我认为这项工作做得很好。

在我使用 Visual Studio 2017 生成所有文件后,vspkg我使用 Visual Studio 2017 测试所有文件并且工作正常,但我的主要目标是使用,VSCode但我不知道为什么会出现错误。

向您展示我已生成文件。

C:\vcpkg\installed\x64-windows\include

在此处输入图像描述

C:\vcpkg\installed\x64-windows\include\opencv2

在此处输入图像描述

C:\vcpkg\installed\x64-windows\lib

在此处输入图像描述

标签: c++opencvvisual-studio-codemingw

解决方案


以下是在 MSYS2/mingw64 中设置 OpenCV 的说明。我对 VsCode 无能为力,但也许这会帮助您取得进步,或者帮助其他通过搜索找到此问题的人。


您不需要自己实际构建 OpenCV;MinGW-w64 有预打包的二进制文件。MSYS2 的目的是提供一个 unix 风格的 shell 并充当包管理器。如果您之前没有使用过 MSYS2:

  1. 安装 MSYS2 并更新到最新版本,如此处所示
  2. 打开 MSYS2/mingw64 shell(不是 MSYS2/msys2 shell)。在这个 shell 中构建的二进制文件将作为独立的 Windows 二进制文件运行,它们不依赖于 MSYS2 环境。
  3. 测试g++有效。

然后你可以添加 OpenCV:

  • pacman -Ss mingw64/mingw-w64-x86_64-opencv

一切准备就绪。我在 MSYS2 shell 中使用以下命令编译了您的示例程序:

g++ -o main main.cpp -std=c++17 -lopencv_core -lopencv_highgui -lopencv_imgcodecs -lopencv_imgproc

并成功运行程序。


请注意,您不需要将 MSYS2 用于构建环境;g++如果您将 Include 和 Library 路径设置为指向 MSYS2 的包管理器安装头文件和库的位置,则可以从 Windows 命令提示符或任何其他 IDE 调用。(在 MSYS2 安装根目录下/mingw64/include/opencv*和下)。/mingw64/lib


推荐阅读