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

32
我写了一个简单的程序,可以从txt文件中加载矩阵并计算距离。在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

我在电脑上安装了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();
};

我做错了什么????

4
似乎CV库没有正确链接到你的项目中。 - Arne Mertz
3
这是第N次出现LNK2019错误了... :-D - Abhineet
1
我有类似的经历。出现了相同的缺失符号(exceptions和interlockedExchangeAdd)。如果我删除opencv库,那么会出现更多的缺失符号,但当它们全部添加时,这些特定的符号仍然缺失。这里还有其他问题发生了... - Elliot Woods
8个回答

34

像其他人所提到的,你需要确保正确链接OpenCV库。

检查你的项目 -> 属性 -> VC++目录 -> 库目录是否包含了OpenCV库的路径,默认路径是'C:\opencv\build\x86\vc11\lib'(在运行VS2012的32位机器上会有所不同,其他设置也会有所不同)。

接下来,请检查以下库是否包含在你的项目 -> 属性 -> 链接器 -> 输入 -> 附加依赖项中:

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

如果上述内容正确,那么你就不应该再遇到OpenCV链接错误了。


4
对我来说没有用,我仍然得到相同的错误。 - Schütze
3
现在可能已经过时了,OpenCV 3.4.1的二进制发行版中唯一可用的库是opencv_world341d.lib,该库可用于链接imread函数。 - jrh
@jhr 我知道你可能已经解决了,但是为了那些会来到这里的人,我添加了一个回答来解决你的问题。 - Felipe Gutierrez
1
很奇怪Nuget包安装没有执行任何操作。它似乎只是添加了include目录。 - Jonathan Lidbeck

16

也许您正在为win32构建,却链接到x64。如果将您的应用程序设置为x64,则可以构建它,而在win32中会导致链接错误。右键单击解决方案并转到配置、平台列。我发现这很棘手,不知道它是否有缺陷。


5

如果有其他人遇到只有几个未解决的外部符号的问题,请注意在构建 Debug 时应链接 opencv_world450d.lib,而在构建 Release 时应链接 opencv_world450.lib


4

按照以下步骤解决问题:

  • 进入项目,右键单击选择设置->C++->常规->附加包含目录点击编辑并添加此路径 C:\opencv\build\include。

  • 现在进入设置->链接器->常规->附加库目录并粘贴 C:\opencv\build\x64\vc15\lib

  • 现在进入设置->链接器->输入并粘贴 opencv_world343.lib,opencv_world343d.lib 就这样了,但是这些设置仅适用于Visual Studio 2017和OpenCV 3.4.3,其他版本需要相应更改。


3
所有的答案都指向了正确的方向,但我想更新一下RedFred在最新版本(4.0.0)中的回答,并更改他提到的库为:

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

对于下一个或上一个版本,只需进入您的opencv目录下的lib文件夹,并搜索RedFred或我提供的列表中的每个项目(显然只复制并粘贴版本号前的最后一个字母,在我的情况下是400),并为您的链接器创建自己的附加依赖项列表。
顺便说一句,我不得不使用CMake从源代码创建我的Visual Studio .sln,使用VS构建,然后出于某种原因,源代码中没有包含文件,所以我从Win pack中添加了它们到我的include目录。

2
请注意,400后面的d代表Debug版本,如果您使用的是Release版本,则应省略它们。 - Felipe Gutierrez
希望这能在未来帮助到某些人。 - Felipe Gutierrez
截至今日的最新版本构建。 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 - Felipe Gutierrez

3

我按照OpenCV教程学习时遇到了问题。在Properties-General-Linker-Input-Additional Dependancies中添加opencv_world450.lib后,我又添加了opencv_world450d.lib以解决这个问题。


最后一个回答解决了我的问题。谢谢。 - Damien

1
你可能已经包含了正确的头文件,但忘记添加库文件。你需要在项目设置中添加相应的 *.lib 文件。

1
我将C:\OpenCV\build\include\opencv添加到包含目录中,将C:\OpenCV\build\x64\vc11\lib添加到库目录中,并在链接器中将opencv_<files>.lib添加为附加依赖项。此外,我的计算机路径变量指向C:\OpenCV\build\x64\vc11\bin dll目录。还需要做什么? - RamBracha
所有的消息都提示缺少opencv_core246.lib(或者你那里的版本)。 - berak
你用了哪个教程在Visual Studio中安装OpenCV?你应该将路径变量编辑为C:\ OpenCV \ build \ x64 \ vc10 \,不要加上“\ bin”。另外,在使用Visual Studio 2010时,你应该使用vc10文件夹而不是vc11。 - OpenMinded

0

对于我的情况,无论如何配置包含/库路径和在我的Visual Studio解决方案中的库文件,我都无法消除_interlockedExchangeAdd链接错误。

因此,我创建了一个新的Visual Studio解决方案,并添加了包含/库路径、库输入文件(opencv_world440.lib)。

有了这个,我就可以构建解决方案了。

我的以前的解决方案中有很多子项目,所以可能存在一些解决方案配置问题,我无法找出原因。

还有一件事,对于调试版本,您应该使用调试库(opencv_world440.lib)。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接