简体   繁体   English

Opencv C ++ MinGW VSCode致命错误编译

[英]Opencv C++ MinGW VSCode fatal error to compile

Hi all I want to use VSCode MinGW C++ and OpenCV to create a simple opencv project but for an unknown reason, I get this error what should I do? 大家好我想使用VSCode MinGW C ++和OpenCV创建一个简单的opencv项目,但由于未知原因,我得到这个错误我该怎么办?

I want to mention that in Visual studio 2017 works I can run the main.cpp on x64 architecture. 我想提一下,在Visual studio 2017我可以在x64架构上运行main.cpp。

The code below is what I want to run on VSCode is the same code I run on the Visual Studio 2017. 我想在VSCode上运行的代码与我在Visual Studio 2017上运行的代码相同。

After 10 days of trying I give up 50 points if someone proves that with: 经过10天的尝试,如果有人证明以下情况,我会放弃50 points

  • VSCODE VSCODE
  • C++17 C ++ 17
  • Opencv OpenCV的
  • on Windows 10 x64 Architecture 在Windows 10 x64架构上

A successful build. 一个成功的构建。

src/main.cpp 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;
}

In my VSCode editor, I try to build the application using the tasks.json with CTRL + SHIFT + B 在我的VSCode编辑器中,我尝试使用CTRL + SHIFT + B的tasks.json构建应用程序

tasks.json tasks.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 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
}

And I get this error 我得到了这个错误

> 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.

I have generated the opencv libs using the vcpkg using this command vcpkg install opencv and I think is doing the job very well. 我已经使用vcpkg使用vcpkg install opencv生成了opencv库,我认为这样做非常好。

After I generated all the files with the vspkg I test all the files with Visual Studio 2017 and is works, but my main goal is to make work with the VSCode but I don't have any clue why I get the errors. 在使用vspkg生成所有文件后,我使用Visual Studio 2017测试所有文件并且正常工作,但我的主要目标是使用VSCode但我没有任何线索,为什么我得到错误。

To show you that I have generated the files. 告诉你我已经生成了文件。

C:\\vcpkg\\installed\\x64-windows\\include C:\\ vcpkg \\安装\\ x64的Windows \\包括

在此输入图像描述

C:\\vcpkg\\installed\\x64-windows\\include\\opencv2 C:\\ vcpkg \\安装\\ x64的Windows \\包括\\ opencv2

在此输入图像描述

C:\\vcpkg\\installed\\x64-windows\\lib C:\\ vcpkg \\安装\\ x64的Windows \\ LIB

在此输入图像描述

Here are instructions for setting up OpenCV in MSYS2/mingw64. 以下是在MSYS2 / mingw64中设置OpenCV的说明。 I can't help with VsCode but perhaps this will help you make progress, or help other people who find this question by searching. 我无法帮助VsCode,但这可能会帮助你取得进步,或帮助其他通过搜索找到这个问题的人。


You don't need to actually build OpenCV yourself; 您不需要自己实际构建OpenCV; there are prepackaged binaries for MinGW-w64. MinGW-w64有预先打包的二进制文件。 The purpose of MSYS2 is to provide a unix-style shell and act as a package manager. MSYS2的目的是提供一个unix风格的shell并充当包管理器。 If you haven't used MSYS2 before: 如果您之前没有使用过MSYS2:

  1. Install MSYS2 and update to latest as shown here . 安装MSYS2并更新到最新版本,如下所示
  2. Open a MSYS2/mingw64 shell (not a MSYS2/msys2 shell). 打开MSYS2 / mingw64 shell(不是MSYS2 / msys2 shell)。 Binaries built in this shell will run as standalone Windows binaries, they do not depend on the MSYS2 environment. 在此shell中构建的二进制文件将作为独立的Windows二进制文件运行,它们不依赖于MSYS2环境。
  3. Test that g++ works. 测试g++工作原理。

Then you can add OpenCV: 然后你可以添加OpenCV:

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

and it's all ready to go. 它已经准备好了。 I compiled your sample program using the following command in MSYS2 shell: 我在MSYS2 shell中使用以下命令编译了示例程序:

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

and ran the program successfully. 并成功运行程序。


Note that you do not need to use MSYS2 for the build environment; 请注意,您不需要将MSYS2用于构建环境; you can invoke g++ from Windows Command Prompt or from any other IDE if you set up the Include and Library paths to point to where MSYS2's package manager installed the headers and libraries. 如果设置Include和Library路径指向MSYS2的程序包管理器安装标头和库的位置,则可以从Windows命令提示符或任何其他IDE调用g++ (Which is under /mingw64/include/opencv* and /mingw64/lib , under the MSYS2 installation root). (在MSYS2安装根目录下的/mingw64/include/opencv*/mingw64/lib下)。

I have success trying to build and run a C++ program with OpenCV 3.4.1 using MinGW-w64 g++ in Visual Studio Code on Windows 10 x64. 我尝试使用Windows 10 x64上的Visual Studio Code中的MinGW-w64 g ++在OpenCV 3.4.1中构建和运行C ++程序。 But I don't know if this is actually the "right" way to do it. 但我不知道这是否真的是“正确”的做法。 Screenshot 截图


  1. Download and Install MinGW-w64 choose x86_64-8.1.0-posix-seh-rt_v6-rev0 version or newer http://mingw-w64.org/doku.php/start . 下载并安装MinGW-w64选择x86_64-8.1.0-posix-seh-rt_v6-rev0版本或更新的http://mingw-w64.org/doku.php/start
  2. Open CMD and go to C:\\ then clone this repo https://github.com/huihut/OpenCV-MinGW-Build then checkout the OpenCV-3.4.1-x64 branch or just execute this git clone https://github.com/huihut/OpenCV-MinGW-Build.git -b OpenCV-3.4.1-x64 打开CMD并转到C:\\然后克隆此repo https://github.com/huihut/OpenCV-MinGW-Build然后签出OpenCV-3.4.1-x64分支或只执行此git clone https://github.com/huihut/OpenCV-MinGW-Build.git -b OpenCV-3.4.1-x64
  3. Add Mingw-w64 bin folder to the system path (For me the path is C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin ) 将Mingw-w64 bin文件夹添加到系统路径(对我来说路径是C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin
  4. Add OpenCV mingw bin folder that you clone earlier to the system path (For me the path is C:\\OpenCV-MinGW-Build\\x64\\mingw\\bin ) 将先前克隆的OpenCV mingw bin文件夹添加到系统路径(对我来说,路径为C:\\OpenCV-MinGW-Build\\x64\\mingw\\bin
  5. For now you can already build a program using g++ using CMD just type g++ test.cpp -IC:\\OpenCV-MinGW-Build\\include -LC:\\OpenCV-MinGW-Build\\x64\\mingw\\bin -llibopencv_calib3d341 -llibopencv_core341 -llibopencv_dnn341 -llibopencv_features2d341 -llibopencv_flann341 -llibopencv_highgui341 -llibopencv_imgcodecs341 -llibopencv_imgproc341 -llibopencv_ml341 -llibopencv_objdetect341 -llibopencv_photo341 -llibopencv_shape341 -llibopencv_stitching341 -llibopencv_superres341 -llibopencv_video341 -llibopencv_videoio341 -llibopencv_videostab341 just change the test.cpp to your .cpp file name. 现在你已经可以使用g ++使用g ++构建一个程序,只需输入g++ test.cpp -IC:\\OpenCV-MinGW-Build\\include -LC:\\OpenCV-MinGW-Build\\x64\\mingw\\bin -llibopencv_calib3d341 -llibopencv_core341 -llibopencv_dnn341 -llibopencv_features2d341 -llibopencv_flann341 -llibopencv_highgui341 -llibopencv_imgcodecs341 -llibopencv_imgproc341 -llibopencv_ml341 -llibopencv_objdetect341 -llibopencv_photo341 -llibopencv_shape341 -llibopencv_stitching341 -llibopencv_superres341 -llibopencv_video341 -llibopencv_videoio341 -llibopencv_videostab341只是改变test.cpp到您的.cpp文件名。
  6. Open your .cpp file using Visual Studio Code then install Code Runner extension https://marketplace.visualstudio.com/items?itemName=formulahendry.code-runner , I use Code Runner to easily run my program. 使用Visual Studio Code打开.cpp文件,然后安装Code Runner扩展https://marketplace.visualstudio.com/items?itemName=formulahendry.code-runner ,我使用Code Runner轻松运行我的程序。
  7. Press Ctrl + comma in Visual Studio Code then search for "code-runner.executorMap": and change the content on "cpp:" entry to "cd $dir && g++ $fileName -o $fileNameWithoutExt -IC:/OpenCV-MinGW-Build/include -LC:/OpenCV-MinGW-Build/x64/mingw/bin -llibopencv_calib3d341 -llibopencv_core341 -llibopencv_dnn341 -llibopencv_features2d341 -llibopencv_flann341 -llibopencv_highgui341 -llibopencv_imgcodecs341 -llibopencv_imgproc341 -llibopencv_ml341 -llibopencv_objdetect341 -llibopencv_photo341 -llibopencv_shape341 -llibopencv_stitching341 -llibopencv_superres341 -llibopencv_video341 -llibopencv_videoio341 -llibopencv_videostab341 && $dir$fileNameWithoutExt", you can apply this for user settings or just for workspace settings. 在Visual Studio Code中按Ctrl + comma ,然后搜索"code-runner.executorMap":并将"cpp:"条目上的内容更改为"cd $dir && g++ $fileName -o $fileNameWithoutExt -IC:/OpenCV-MinGW-Build/include -LC:/OpenCV-MinGW-Build/x64/mingw/bin -llibopencv_calib3d341 -llibopencv_core341 -llibopencv_dnn341 -llibopencv_features2d341 -llibopencv_flann341 -llibopencv_highgui341 -llibopencv_imgcodecs341 -llibopencv_imgproc341 -llibopencv_ml341 -llibopencv_objdetect341 -llibopencv_photo341 -llibopencv_shape341 -llibopencv_stitching341 -llibopencv_superres341 -llibopencv_video341 -llibopencv_videoio341 -llibopencv_videostab341 && $dir$fileNameWithoutExt",您可以将其应用于用户设置或仅用于工作区设置。 ` `
  8. After that just press Ctrl+Alt+N to Build and Run your program or just press the play button. 之后只需按Ctrl+Alt+N即可构建并运行程序或只需按下播放按钮即可。

I believe you can also apply this to the tasks.json by adding the -IC:\\OpenCV-MinGW-Build\\include .. to the args part to use the Visual Studio Code C/C++ extension debugging. 我相信您也可以通过向args部分添加tasks.json -IC:\\OpenCV-MinGW-Build\\include ..来将其应用于tasks.json ,以使用Visual Studio Code C / C ++扩展调试。 Also you can add "C:/OpenCV-MinGW-Build/include" to the c_cpp_properties.json include path to enable intellisense. 您还可以将"C:/OpenCV-MinGW-Build/include"c_cpp_properties.json包含路径以启用智能感知。 If you want to use another version of OpenCV just replace all the "341" part of the g++ argument to another version for example to use OpenCV 3.3.1 use -llibopencv_calib3d331 and so on. 如果你想使用另一个版本的OpenCV,只需将g ++参数的所有“341”部分替换为另一个版本,例如使用OpenCV 3.3.1使用-llibopencv_calib3d331等等。


EDIT: I have try to edit my tasks.json and launch.json and it works with gdb debugger. 编辑:我已经尝试编辑我的tasks.jsonlaunch.json ,它适用于gdb调试器。 tasks.json : https://gist.github.com/agtbaskara/4a2ec9a3a9a963069e719c0477185321 launch.json : https://gist.github.com/agtbaskara/de04db8b6a31522dd1e62c43aa6e0f89 c_cpp_properties.json : https://gist.github.com/agtbaskara/0eb773ac3085557baf7bf20b031fb49e tasks.jsonhttps://gist.github.com/agtbaskara/4a2ec9a3a9a963069e719c0477185321 launch.jsonhttps://gist.github.com/agtbaskara/de04db8b6a31522dd1e62c43aa6e0f89 c_cpp_properties.jsonhttps://gist.github.com/agtbaskara/ 0eb773ac3085557baf7bf20b031fb49e

Install vcpkg ( MS packager to install windows based open source projects) and use powershell command .\\vcpkg install opencv:x64-windows-static . 安装vcpkg(MS packager以安装基于Windows的开源项目)并使用powershell命令.\\vcpkg install opencv:x64-windows-static Dependency libraries will be auto installed for you. 将自动为您安装依赖库。

Opencv can be auto integrated into your VS project using .\\vcpkg integrate install . 可以使用.\\vcpkg integrate install自动集成到您的VS项目中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM