简体   繁体   English

错误 LNK2019:未解析的外部符号 opencv

[英]error LNK2019: unresolved external symbol opencv

I wrote this simple program that loads matrices from txt files and calculate distances.我编写了这个简单的程序,它从 txt 文件中加载矩阵并计算距离。 When compiling the program in visual studio on windows I get the following errors:在 windows 上的 Visual Studio 中编译程序时,出现以下错误:

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.我在我的电脑上安装了 opencv 2.4.6 并将其正确链接到 Visual Studio。

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????我究竟做错了什么????

Like others mentioned, you need to make sure you're linking to the OpenCV libs correctly.像其他人提到的那样,您需要确保正确链接到 OpenCV 库。

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).检查您的Project -> Properties -> VC++ Directories -> Library Directories ,包括 OpenCV 库所在的路径,默认为 'C:\\opencv\\build\\x86\\vc11\\lib'(在 32 位机器上运行 VS2012,它会在其他设置上有所不同)。

Next, check that the following libraries are included in your Project -> Properties -> Linker -> Input -> Additional Dependencies :接下来,检查以下库是否包含在您的Project -> Properties -> Linker -> Input -> Additional Dependencies 中

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

If the above are correct, you shouldn't get any more OpenCV link errors.如果以上都是正确的,您应该不会再遇到 OpenCV 链接错误。

Perhaps you are building for win32 but linking to x64.也许您正在为 win32 构建但链接到 x64。 If you set your application to x64 then it will build, whereas in win32 it gives linking errors.如果您将应用程序设置为 x64,那么它将构建,而在 win32 中,它会出现链接错误。 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.我发现设置这个很棘手,我想知道它是否有问题。

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.转到 Project 右键单击​​并选择 settings->C++->General->Additional include directory 单击编辑并添加此 C:\\opencv\\build\\include。

  • Now go to settings->Linker->General->Additional library Directories and paste C:\\opencv\\build\\x64\\vc15\\lib现在转到设置->链接器->常规->附加库目录并粘贴 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.现在转到设置->链接器->输入并粘贴 opencv_world343.lib,opencv_world343d.lib 就是这样,但这些设置适用于 Visual Studio 2017 和 OpenCV 3.4.3 仅适用于其他版本将不得不相应更改。

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:所有答案都指向正确的方向,但我想更新RedFred对今天(4.0.0)最新版本的回答,更改他提到的库:

opencv_core400d.lib opencv_core400d.lib
opencv_imgproc400d.lib opencv_imgproc400d.lib
opencv_highgui400d.lib opencv_highgui400d.lib
opencv_ml400d.lib opencv_ml400d.lib
opencv_video400d.lib opencv_video400d.lib
opencv_features2d400d.lib opencv_features2d400d.lib
opencv_calib3d400d.lib opencv_calib3d400d.lib
opencv_objdetect400d.lib opencv_objdetect400d.lib
opencv_flann400d.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.对于下一个或上一个版本,只需转到 opencv 目录中的 lib 文件夹并搜索RedFred或我提供的列表中的每个项目(显然只复制并粘贴到版本号之前的最后一个字母,在我的情况下为 400 ) 并为您的链接器创建您自己的附加依赖项列表。

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 .顺便说一下,我必须使用 CMake 从源代码创建我的 Visual Studio .sln,用 VS 构建,然后由于某种原因,源代码没有包含文件,所以我将它们添加到Win pack 的包含目录中。

任何其他人如果只用几个未解析的外部符号opencv_world450d.lib这个问题,请注意在构建 Debug 时应该链接opencv_world450.lib在构建 Release 时应该链接opencv_world450.lib

You probably included the right header files, but forgot to add the library.您可能包含了正确的头文件,但忘记添加库。 You need to add the corresponding *.lib file in the project settings.需要在工程设置中添加对应的*.lib文件。

I got the problem by following the opencv tutorials.我按照opencv教程遇到了问题。 I fixed it by adding opencv_world450d.lib after adding opencv_world450.lib in Propeties-General-Linker-Input-Additional Dependancies.我通过在属性-通用-链接器-输入-附加依赖项中添加 opencv_world450.lib 后添加 opencv_world450d.lib 来修复它。

For my case, I could not remove _interlockedExchangeAdd link error no matter how configure include/library path, and library file on my visual studio solution.就我而言,无论我的 Visual Studio 解决方案如何配置包含/库路径和库文件,我都无法删除 _interlockedExchangeAdd 链接错误。

So I created new visual studio solution and added include/library path, library input files(opencv_world440.lib).所以我创建了新的 Visual Studio 解决方案并添加了包含/库路径、库输入文件(opencv_world440.lib)。 With this, I could build the solution.有了这个,我可以构建解决方案。

My previous solution had may sub projects inside the solution, so there might be some issue with solution configuration which I could not figure out.我以前的解决方案可能在解决方案中包含子项目,因此解决方案配置可能存在一些我无法弄清楚的问题。

One more thing, for debug build, you should use debug library(opencv_world440.lib)还有一件事,对于调试构建,您应该使用调试库(opencv_world440.lib)

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

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