【发布时间】:2018-07-31 21:09:09
【问题描述】:
大家好,我想使用 VSCode MinGW C++ 和 OpenCV 创建一个简单的 opencv 项目,但由于未知原因,我收到此错误,我该怎么办?
我想提一下,在 Visual studio 2017 作品中,我可以在 x64 架构上运行 main.cpp。
下面的代码是我想在 VSCode 上运行的代码,与我在 Visual Studio 2017 上运行的代码相同。
经过 10 天的尝试,如果有人证明了这一点,我会放弃 50 points:
- VSCODE
- C++17
- Opencv
- 在 Windows 10 x64 架构上
一个成功的构建。
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 编辑器中,我尝试使用带有 CTRL + SHIFT + B 的 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
{
"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.
我已经使用vcpkg 使用此命令vcpkg install opencv 生成了opencv 库,我认为这项工作做得很好。
在使用 vspkg 生成所有文件后,我使用 Visual Studio 2017 测试所有文件并且工作正常,但我的主要目标是使用 VSCode,但我不知道为什么我得到错误。
向您展示我已生成文件。
C:\vcpkg\installed\x64-windows\include
C:\vcpkg\installed\x64-windows\include\opencv2
C:\vcpkg\installed\x64-windows\lib
【问题讨论】:
-
您可能需要 MinGW 二进制文件而不是 Visual Studio 二进制文件。
-
以及如何使用 MinGW 二进制文件,您能描述一下您的解决方案吗?谢谢
-
所有教程都已过时这很可能无关紧要。
CMake的 GUI 多年来并没有真正改变太多。 -
我该怎么办?复制
.lib文件和 minGW 目录中的所有包含目录,如果是,在哪里? -
这可能会有所帮助:github.com/huihut/OpenCV-MinGW-Build
标签: c++ opencv visual-studio-code mingw