Search code examples
opencvnugetvisual-studio-2022

Use nuget of vs2022 to install opencv, but an error occurs


I'm a newbie and I'm trying to use opencv with vs2022 for the first time.In vs2022, I created a new empty project and installed opencv using nuget. Next, I created a source file named main.cpp and wrote the following code:

#include <cstdio>
#include <string>
#include <opencv2/opencv.hpp>

using namespace cv;
using namespace std;

int main() {
    Mat image = imread("112.jpg", IMREAD_COLOR);
    if (image.empty()) {
        cout << "could not find" << endl;
    }
    imshow("Image", image);

    return 0;
}

When I run the project I get the following error:

1>------ Build started: Project: Project5, Configuration: Debug x64 ------
1>main.obj : error LNK2019: unresolved external symbol "void __cdecl cv::fastFree(void *)" (?fastFree@cv@@YAXPEAX@Z) referenced in function "public: __cdecl cv::Mat::~Mat(void)" (??1Mat@cv@@QEAA@XZ)
1>main.obj : error LNK2019: unresolved external symbol "public: __cdecl cv::_InputArray::_InputArray(class cv::Mat const &)" (??0_InputArray@cv@@QEAA@AEBVMat@1@@Z) referenced in function main
1>main.obj : error LNK2019: unresolved external symbol "public: void __cdecl 

I tried for a long time but couldn't find the reason, because as far as the information I looked up, no one had this error. After they installed opencv using nuget, they could use it normally.

I tried uninstalling opencv and reinstalling but to no avail


Solution

  • The package you are using is a very old version package upload by personal, even it is support native code.

    Nuget Package is mainly a package for .net environment projects.

    I notice the really opencv project is here:

    https://github.com/opencv/opencv

    And it still updates yesterday.

    For your C++ project, you could search the C++ packages and install packages via here:

    VCPKG IO

    This is the official document of it:

    Open CV Documents

    And this is the examples:

    Examples of OpenCV

    I think this sample has a part of code which is similar to what you are doing:

    https://docs.opencv.org/4.x/dc/dbc/samples_2cpp_2tutorial_code_2HighGUI_2AddingImagesTrackbar_8cpp-example.html

    By the way, you can take a look of this answer, he can success using another C++ library via my direction and he shared the detailed steps:

    How to use Tesseract C++ package in project