我如何获得 Visual Studio 以使用 OpenCV 支持来编译我的应用程序?

How do I get Visual Studio to compile my application with OpenCV support?

我需要编译 运行 一个使用 OpenCV 的 windows 程序。为此,我下载并安装了 MS Visual Studio 2010。我确认我可以编译 & 运行 一个 Hello World 终端应用程序。

现在,我已经安装了 OpenCV 并相应地设置了编译器和链接器路径。我写了一个小示例应用程序:

#include "stdafx.h"
#include<opencv2/opencv.hpp>
#include<iostream>

using namespace cv;

int _tmain(int argc, _TCHAR* argv[])
{
    Mat img = imread("7MLS142.jpg");
    namedWindow("image", WINDOW_NORMAL);
    imshow("image", img);
    waitKey(0);
    return 0;
}

但是当我想编译和 运行 它时得到以下错误:

1>------ Build started: Project: Hello_World, Configuration: Debug Win32 ------
1>Build started 2018-12-11 11:42:00 AM.
1>InitializeBuildStatus:
1>  Touching "Debug\Hello_World.unsuccessfulbuild".
1>ClCompile:
1>  All outputs are up-to-date.
1>  Hello_World.cpp
1>ManifestResourceCompile:
1>  All outputs are up-to-date.
1>Hello_World.obj : error LNK2019: unresolved external symbol "int __cdecl cv::waitKey(int)" (?waitKey@cv@@YAHH@Z) referenced in function _wmain
1>Hello_World.obj : error LNK2019: unresolved external symbol "void __cdecl cv::imshow(class cv::String const &,class cv::debug_build_guard::_InputArray const &)" (?imshow@cv@@YAXABVString@1@ABV_InputArray@debug_build_guard@1@@Z) referenced in function _wmain
1>Hello_World.obj : error LNK2019: unresolved external symbol "void __cdecl cv::namedWindow(class cv::String const &,int)" (?namedWindow@cv@@YAXABVString@1@H@Z) referenced in function _wmain
1>Hello_World.obj : error LNK2019: unresolved external symbol "class cv::Mat __cdecl cv::imread(class cv::String const &,int)" (?imread@cv@@YA?AVMat@1@ABVString@1@H@Z) referenced in function _wmain
1>Hello_World.obj : error LNK2019: unresolved external symbol "private: char * __thiscall cv::String::allocate(unsigned int)" (?allocate@String@cv@@AAEPADI@Z) referenced in function "public: __thiscall cv::String::String(char const *)" (??0String@cv@@QAE@PBD@Z)
1>Hello_World.obj : error LNK2019: unresolved external symbol "private: void __thiscall cv::String::deallocate(void)" (?deallocate@String@cv@@AAEXXZ) referenced in function "public: __thiscall cv::String::~String(void)" (??1String@cv@@QAE@XZ)
1>Hello_World.obj : error LNK2019: unresolved external symbol "void __cdecl cv::fastFree(void *)" (?fastFree@cv@@YAXPAX@Z) referenced in function "public: __thiscall cv::Mat::~Mat(void)" (??1Mat@cv@@QAE@XZ)
1>Hello_World.obj : error LNK2019: unresolved external symbol "public: void __thiscall cv::Mat::deallocate(void)" (?deallocate@Mat@cv@@QAEXXZ) referenced in function "public: void __thiscall cv::Mat::release(void)" (?release@Mat@cv@@QAEXXZ)
1>c:\users\rideo\src\visual studio 2010\Projects\Hello_World\Debug\Hello_World.exe : fatal error LNK1120: 8 unresolved externals
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:03.46

Linker 设置中的 Additional library path 设置为 %OPENCV_DIR%\bin 其中我已设置 setx -m OPENCV_DIR C:\Users\rideo\opencv\build\x64\vc14 (有一个 vc14 和一个 vc15) 目录并且我已经安装了'Visual Studio 2010` - 这就是导致这里问题的原因吗?

更新

我现在安装了 MS Visual Studio Professional 2015,设置 C:\Users\rideo\opencv\build\x64\vc15`,打开相同的项目但仍然得到:

1>------ Rebuild All started: Project: Hello_World, Configuration: Debug Win32 ------
1>Build started 2018-12-11 2:09:41 PM.
1>     1>
1>_PrepareForClean:
1>         Deleting file "Debug\Hello_World.tlog\Hello_World.lastbuildstate".
1>       InitializeBuildStatus:
1>         Touching "Debug\Hello_World.tlog\unsuccessfulbuild".
1>       ClCompile:
1>         stdafx.cpp
1>         Hello_World.cpp
1>     1>
1>Hello_World.obj : error LNK2019: unresolved external symbol "void __cdecl cv::fastFree(void *)" (?fastFree@cv@@YAXPAX@Z) referenced in function "public: __thiscall cv::Mat::~Mat(void)" (??1Mat@cv@@QAE@XZ)
1>     1>
1>Hello_World.obj : error LNK2019: unresolved external symbol "private: char * __thiscall cv::String::allocate(unsigned int)" (?allocate@String@cv@@AAEPADI@Z) referenced in function "public: __thiscall cv::String::String(char const *)" (??0String@cv@@QAE@PBD@Z)
1>     1>
1>Hello_World.obj : error LNK2019: unresolved external symbol "private: void __thiscall cv::String::deallocate(void)" (?deallocate@String@cv@@AAEXXZ) referenced in function "public: __thiscall cv::String::~String(void)" (??1String@cv@@QAE@XZ)
1>     1>
1>Hello_World.obj : error LNK2019: unresolved external symbol "public: void __thiscall cv::Mat::deallocate(void)" (?deallocate@Mat@cv@@QAEXXZ) referenced in function "public: void __thiscall cv::Mat::release(void)" (?release@Mat@cv@@QAEXXZ)
1>     1>
1>Hello_World.obj : error LNK2019: unresolved external symbol "class cv::Mat __cdecl cv::imread(class cv::String const &,int)" (?imread@cv@@YA?AVMat@1@ABVString@1@H@Z) referenced in function _wmain
1>     1>
1>Hello_World.obj : error LNK2019: unresolved external symbol "void __cdecl cv::namedWindow(class cv::String const &,int)" (?namedWindow@cv@@YAXABVString@1@H@Z) referenced in function _wmain
1>     1>
1>Hello_World.obj : error LNK2019: unresolved external symbol "int __cdecl cv::waitKey(int)" (?waitKey@cv@@YAHH@Z) referenced in function _wmain
1>     1>
1>Hello_World.obj : error LNK2019: unresolved external symbol "void __cdecl cv::imshow(class cv::String const &,class cv::debug_build_guard::_InputArray const &)" (?imshow@cv@@YAXABVString@1@ABV_InputArray@debug_build_guard@1@@Z) referenced in function _wmain
1>     1>
1>C:\Users\rideo\src\visual studio 2010\Projects\Hello_World\Debug\Hello_World.exe : fatal error LNK1120: 8 unresolved externals
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:01.92
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========

这是opencv-3.4.3-vc14_vc15.exe

vc14 用于使用 Visual studio 2015 的构建。

但是你安装了Visual studio2010

对于VS2010,环境变量应该使用(取决于32位或64位)

setx -m OPENCV_DIR D:\OpenCV\Build\x86\vc10     (suggested for Visual Studio 2010 - 32 bit Windows)
setx -m OPENCV_DIR D:\OpenCV\Build\x64\vc10     (suggested for Visual Studio 2010 - 64 bit Windows)

这里有更多关于使用静态库或动态库的指南:

https://docs.opencv.org/2.4/doc/tutorials/introduction/windows_install/windows_install.html#windowssetpathandenviromentvariable

这里是带截图的详细指南,涉及在 visual studio 2015 C++ 项目中使用 OpenCV 所需的所有项目设置。

https://www.opencv-srf.com/2017/11/install-opencv-with-visual-studio.html