error LNK2019: unresolved external symbol opencv

90,479

Solution 1

Like others mentioned, you need to make sure you're linking to the OpenCV libs correctly.

Check that your Project -> Properties -> VC++ Directories -> Library Directories, includes the path where the OpenCV libraries are, the default would be 'C:\opencv\build\x86\vc11\lib' (on a 32-bit machine running VS2012, it will vary on other setups).

Next, check that the following libraries are included in your Project -> Properties -> Linker -> Input -> Additional Dependencies :

opencv_core246d.lib
opencv_imgproc246d.lib
opencv_highgui246d.lib
opencv_ml246d.lib
opencv_video246d.lib
opencv_features2d246d.lib
opencv_calib3d246d.lib
opencv_objdetect246d.lib
opencv_contrib246d.lib
opencv_legacy246d.lib
opencv_flann246d.lib

If the above are correct, you shouldn't get any more OpenCV link errors.

Solution 2

Perhaps you are building for win32 but linking to x64. If you set your application to x64 then it will build, whereas in win32 it gives linking errors. Right click on the solution and go to configuration, platform column. I found it tricky to set this, I wonder if it's buggy or not.

Solution 3

Follow the steps to solve it

  • Go to Project right click and select settings->C++->General->Additional include directories click edit and add this C:\opencv\build\include.

  • Now go to settings->Linker->General->Additional library Directories and paste C:\opencv\build\x64\vc15\lib

  • Now go to settings->Linker->Input and paste opencv_world343.lib,opencv_world343d.lib that is all,but these settings are true for visual studio 2017 and OpenCV 3.4.3 only for other versions will have to change accordingly.

Solution 4

All of the answers point in the right direction but I want to update what RedFred answered to the latest build as of today (4.0.0), change the libraries he mentioned for:

opencv_core400d.lib
opencv_imgproc400d.lib
opencv_highgui400d.lib
opencv_ml400d.lib
opencv_video400d.lib
opencv_features2d400d.lib
opencv_calib3d400d.lib
opencv_objdetect400d.lib
opencv_flann400d.lib

For next or previous builds just go to your lib folder in your opencv's directory and search for each of the items in the list that RedFred or I provided (obviously copy and paste only up to the last letter before the version number, in my case 400) and create your own additional dependencies list for your linker.

By the way I had to create my Visual Studio .sln from the source code using CMake, build with VS, then for some reason with the source came no include files so I added them to my include directories from the Win pack.

Solution 5

Anyone else who stumbles on to this problem with only a couple of unresolved external symbols, please be aware that the opencv_world450d.lib should be linked when building Debug, and opencv_world450.lib should be linked when building Release.

Share:
90,479

Related videos on Youtube

RamBracha
Author by

RamBracha

Updated on November 20, 2020

Comments

  • RamBracha
    RamBracha over 3 years

    I wrote this simple program that loads matrices from txt files and calculate distances. When compiling the program in visual studio on windows I get the following errors:

    1>main.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>system.obj : error LNK2001: unresolved external symbol "void __cdecl cv::fastFree(void *)" (?fastFree@cv@@YAXPAX@Z)
    1>main.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>system.obj : error LNK2001: unresolved external symbol "public: void __thiscall cv::Mat::deallocate(void)" (?deallocate@Mat@cv@@QAEXXZ)
    1>main.obj : error LNK2019: unresolved external symbol "int __cdecl cv::_interlockedExchangeAdd(int *,int)" (?_interlockedExchangeAdd@cv@@YAHPAHH@Z) referenced in function "public: void __thiscall cv::Mat::release(void)" (?release@Mat@cv@@QAEXXZ)
    1>system.obj : error LNK2001: unresolved external symbol "int __cdecl cv::_interlockedExchangeAdd(int *,int)" (?_interlockedExchangeAdd@cv@@YAHPAHH@Z)
    1>system.obj : error LNK2019: unresolved external symbol "public: __thiscall cv::Exception::Exception(int,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,int)" (??0Exception@cv@@QAE@HABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@00H@Z) referenced in function "public: int & __thiscall cv::Mat::at<int>(int,int)" (??$at@H@Mat@cv@@QAEAAHHH@Z)
    1>system.obj : error LNK2019: unresolved external symbol "public: virtual __thiscall cv::Exception::~Exception(void)" (??1Exception@cv@@UAE@XZ) referenced in function "public: int & __thiscall cv::Mat::at<int>(int,int)" (??$at@H@Mat@cv@@QAEAAHHH@Z)
    1>system.obj : error LNK2019: unresolved external symbol "void __cdecl cv::error(class cv::Exception const &)" (?error@cv@@YAXABVException@1@@Z) referenced in function "public: int & __thiscall cv::Mat::at<int>(int,int)" (??$at@H@Mat@cv@@QAEAAHHH@Z)
    1>system.obj : error LNK2019: unresolved external symbol "public: void __thiscall cv::Mat::create(int,int const *,int)" (?create@Mat@cv@@QAEXHPBHH@Z) referenced in function "public: void __thiscall cv::Mat::create(int,int,int)" (?create@Mat@cv@@QAEXHHH@Z)
    1>C:\Users\Ram\documents\visual studio 2012\Projects\descrip\Debug\descrip.exe : fatal error LNK1120: 7 unresolved externals
    

    I intsalled opencv 2.4.6 on my computer and linked it to visual studio properly.

    main.cpp

    #include "system.h"
    
    using namespace std;
    
    int main(int argc, char* argv[]){    
      if(argc != 3){
        cout << "Not enough arguments" << endl;
        exit(-1);
      }
    
      System s(argv[2]);
      s.Parse_Centers(argv[1]);
      s.Run();
      return 0;
    } 
    

    system.h

    #include <iostream>
    #include <fstream>
    #include <dirent.h> 
    #include <time.h>
    #include "cv.h"
    #include "highgui.h"
    #include "opencv2/opencv.hpp"
    
    #define NUM_CENTERS 5000
    #define NUM_COL 512
    
    using namespace cv;
    
    class System{
    public:
        System(char *dir);
        void Run();
        void Parse_Centers(char* path);
        void Compute_Histogram(const char* filename);
    
    private:
        Mat centers;
        Mat centers_zero;
        char *dir_path;
    };
    

    system.cpp

    #include "system.h"
    
    using namespace std;
    using namespace cv;
    
    System::System(char *dir){
        centers.create(NUM_CENTERS, NUM_COL, CV_8U);
        centers_zero.create(NUM_CENTERS, NUM_COL, CV_8U);
        dir_path = dir;
    };
    
    void System::Parse_Centers(char* path){
        ifstream fin;
        int temp, n, line = 0;
        fin.open(path);
    
        if(!fin.good()){ 
            throw 1; 
        }
    
        while(!fin.eof()){
            char buf[2048];
            const char* token[NUM_COL] = {};
    
            n = 0;
            fin.getline(buf, 2048);
            token[0] = strtok(buf, ",");
    
            if(token[0]){
                temp = atoi(token[0]);
                if(temp){
                    centers.at<int>(line,n) = temp;
                    centers_zero.at<int>(line,n) = temp * temp;
                }
    
                for(int n = 1; n < 512; n++){
                    token[n] = strtok(0, ",");
                    temp = atoi(token[n]);
                    if(temp){
                        centers.at<int>(line,n) = temp;
                        centers_zero.at<int>(line,n) = temp * temp;
                    }
                }
            }
            line++;
        }
    
        fin.close();
    };  
    
    void System::Run(){
        DIR *dir;
        struct dirent *entry;
        time_t start_t;
        time_t end_t;
    
        dir = opendir(dir_path);
        if(!dir){
            cout << "Directory wasn't found" << endl;
            throw 3;  
        }
    
        while((entry = readdir(dir)) != NULL){
            if(entry->d_name[0] != '.'){
                string path = string(dir_path) + "/" + string(entry->d_name);
                cout << "entry: " << path;
                time(&start_t);
                Compute_Histogram(path.c_str());
                time(&end_t);
                cout << "   " << difftime(start_t,end_t) << "sec" << endl;
            }
        }
    
        closedir(dir);
    }
    
    void System::Compute_Histogram(const char* filename){
        int dist[NUM_CENTERS];
        int desc[NUM_CENTERS] = {0};
        int temp, place = 0;
    
        ifstream fin;
        fin.open(filename);
    
        if(!fin.good()){ 
            throw 2; 
        }
    
        while(!fin.eof()){
            char buf[2048];
            const char* token[512] = {};
    
            fin.getline(buf, 2048);
            token[0] = strtok(buf, ",");
            if(token[0]){
                temp = atoi(token[0]);
                if(temp){
                    for(int i = 0; i < NUM_CENTERS; i++){
                        dist[i] = (temp - centers.at<int>(i,0)) * (temp - centers.at<int>(i,0));
                    }
                }
                else{
                    for(int i = 0; i < NUM_CENTERS; i++){  
                        dist[i] = centers_zero.at<int>(i,0);
                    }
                }
    
                for(int n = 1; n < NUM_COL; n++){
                    token[n] = strtok(0, ",");
                    temp = atoi(token[n]);
    
                    if(temp){
                        for(int i = 0; i < NUM_CENTERS; i++){
                            dist[i] += (temp - centers.at<int>(i,n)) * (temp - centers.at<int>(i,n));
                            if((n == 511) && (i > 0)){
                                if(dist[i] < dist[place]){
                                    place = i;
                                }
                            }
                        }
                    }
                    else{
                        for(int i = 0; i < NUM_CENTERS; i++){
                            dist[i] += centers_zero.at<int>(i,n);
                            if((n == 511) && (i > 0)){
                                if(dist[i] < dist[place]){
                                    place = i;
                                }
                            }
                        }
                    }
                }
            }
    
            desc[place]++;
        }
    
        fin.close();
    
        ofstream outfile;
        string path;
        path = string(filename) + ".csv";
        outfile.open(path.c_str());
        for(int i = 0; i < 4999; i++){
            outfile << desc[i] << ",";
        }
        outfile << desc[4999];
        outfile.close();
    };
    

    What am i doing wrong????

    • Arne Mertz
      Arne Mertz almost 11 years
      Seems the CV library did not get linked to your project properly.
    • Abhineet
      Abhineet over 10 years
      And thus the LNK2019 popped again for the umpteenth time... :-D
    • Elliot Woods
      Elliot Woods over 7 years
      I'm having a similar experience. These same missing symbols (exceptions and interlockedExchangeAdd) appear. If i remove the opencv libs then i get many more missing symbols, but when they're all added, these specific symbols are still missing. there's something else going on here..
  • RamBracha
    RamBracha almost 11 years
    I added C:\OpenCV\build\include\opencv to include dirs. and C:\OpenCV\build\x64\vc11\lib to library dirs and opencv_<files>.lib to additional dependencies in the linker. plus my computer path variable points to C:\OpenCV\build\x64\vc11\bin dll dir. what else?
  • berak
    berak almost 11 years
    all messages hint at missing opencv_core246.lib (or what version you got there)
  • OpenMinded
    OpenMinded almost 11 years
    What tutorial did you use to setup OpenCV with Visual Studio? You should edit your path variable to C:\OpenCV\build\x64\vc10\ without "\bin". Also with Visual Studio 2010 you should use vc10 folder instead of vc11.
  • Schütze
    Schütze over 6 years
    Did not work for me, I am still getting the same error.
  • jrh
    jrh over 6 years
    This might be outdated now, the only lib I get in the binary distribution of opencv 3.4.1 is opencv_world341d.lib.
  • Felipe Gutierrez
    Felipe Gutierrez over 5 years
    @jhr I know you probably solved it by now, but for the people that will come here, I added an answer that addresses your problem.
  • Felipe Gutierrez
    Felipe Gutierrez over 5 years
    Note that the d after the 400 point to a build to Debug, if you are using a Release build, you should omit them.
  • Felipe Gutierrez
    Felipe Gutierrez over 5 years
    Glad if it can help somebody in the future.
  • Jonathan Lidbeck
    Jonathan Lidbeck over 4 years
    Surprising that the Nuget package installation doesn't do any of this. It does appear to add the include directory.
  • Felipe Gutierrez
    Felipe Gutierrez over 4 years
    Latest release build as of today. opencv_core412.lib opencv_imgproc412.lib opencv_highgui412.lib opencv_ml412.lib opencv_video412.lib opencv_features2d412.lib opencv_calib3d412.lib opencv_objdetect412.lib opencv_flann412.lib
  • Damien
    Damien over 3 years
    The last answer but the one that solved my issue. Thanks