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

C++ 错误LNK2019:未解析的外部符号opencv,c++,visual-studio-2010,opencv,C++,Visual Studio 2010,Opencv,我编写了这个简单的程序,从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(vo

我编写了这个简单的程序,从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
1>main.obj:错误LNK2019:未解析的外部符号“void u cdecl cv::fastFree(void*)”(?fastFree@cv@@YAXPAX@Z)在函数“public:u thiscall cv::Mat::~Mat(void)”中引用(?)??1Mat@cv@@QAE@XZ)
1> system.obj:错误LNK2001:未解析的外部符号“void u cdecl cv::fastFree(void*)”(?fastFree@cv@@YAXPAX@Z)
1> main.obj:错误LNK2019:未解析的外部符号“public:void\uu thiscall cv::Mat::deallocate(void)”(?deallocate@Mat@函数“public:void\uu thiscall cv::Mat::release(void)”(?release@Mat@cv@@QAEXXZ)
1> system.obj:错误LNK2001:未解析的外部符号“public:void u thiscall cv::Mat::deallocate(void)”(?deallocate@Mat@cv@@QAEXXZ)
1> main.obj:错误LNK2019:未解析的外部符号“int u cdecl cv::_interlockedExchangeAdd(int*,int)”(_interlockedExchangeAdd@cv@@YAHPAHH@Z)在函数“public:void u thiscall cv::Mat::release(void)”中引用(?release@Mat@cv@@QAEXXZ)
1> system.obj:错误LNK2001:未解析的外部符号“int uu cdecl cv::_InterlocatedExchangeAdd(int*,int)”(_interlockedExchangeAdd@cv@@YAHPAHH@Z)
1> system.obj:错误LNK2019:未解析的外部符号“public:u thiscall cv::Exception::Exception(int,class std::basic_string const&,class std::basic_string const&,class std::basic_string const&,int)”(??0Exception@cv@@QAE@HABV?$basic_string@DU?$char_traits@D@性病病毒$allocator@D@2@@std@@00H@Z)在函数中引用“public:int&u thiscall cv::Mat::at(int,int)”($at@H@Mat@cv@@QAEAAHHH@Z)
1> system.obj:错误LNK2019:未解析的外部符号“public:virtual\u thiscall cv::Exception::~Exception(void)”(?)??1Exception@cv@@UAE@XZ)在函数“public:int&u thiscall cv::Mat::at(int,int)”中引用(?)$at@H@Mat@cv@@QAEAAHHH@Z)
1> system.obj:错误LNK2019:未解析的外部符号“void u cdecl cv::error(类cv::Exception const&)”(?error@cv@@YAXABVException@1@@Z) 在函数“public:int&u thiscall cv::Mat::at(int,int)”中引用(?)$at@H@Mat@cv@@QAEAAHHH@Z)
1> system.obj:错误LNK2019:未解析的外部符号“public:void\uu thiscall cv::Mat::create(int,int const*,int)”(?create@Mat@简历@@QAEXHPBHH@Z)函数“public:void\uu thiscall cv::Mat::create(int,int,int)”中引用(?create@Mat@简历@@QAEXHHH@Z)
1> C:\Users\Ram\documents\visual studio 2012\Projects\descripp\Debug\descripp.exe:致命错误LNK1120:7个未解析的外部
我在计算机上插入了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”
使用名称空间std;
intmain(intargc,char*argv[]){
如果(argc!=3){

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

与前面提到的其他文件一样,您需要确保正确链接到OpenCV库

检查您的项目->属性->VC++目录->库目录,包括OpenCV库所在的路径,默认为“C:\OpenCV\build\x86\vc11\lib”(在运行VS2012的32位计算机上,其他设置会有所不同)

接下来,检查项目->属性->链接器->输入->其他依赖项中是否包含以下库:

opencv_core246d.lib
opencv_imgproc246d.lib
opencv_highgui246d.lib
opencv_ml246d.lib
opencv\u video246d.lib
opencv_特性2d246d.lib
opencv_calib346d.lib
opencv_objdetect246d.lib
opencv_contrib246d.lib
opencv_legacy246d.lib
opencv_flann246d.lib


如果以上所述是正确的,则不应再出现任何OpenCV链接错误。

也许您正在为win32构建,但链接到x64。如果您将应用程序设置为x64,则它将进行构建,而在win32中,它会出现链接错误。右键单击解决方案并转到“配置”、“平台”列。我发现设置此项很棘手,我想知道是否需要进行设置是否有问题。

所有答案都指向正确的方向,但我想更新截至今天(4.0.0)的最新版本的答案,更改他提到的库:

opencv_core400d.lib
opencv_imgproc400d.lib
opencv_highgui400d.lib
opencv_ml400d.lib
opencv\u video400d.lib
opencv_功能2d400d.lib
opencv_calib3d400d.lib
opencv_objdetect400d.lib
opencv_flann400d.lib

对于下一个或上一个版本,只需转到opencv目录中的lib文件夹,搜索我提供的列表中的每个项目(显然,复制并粘贴到版本号之前的最后一个字母,在我的例子中是400),然后为链接器创建自己的附加依赖项列表


顺便说一句,我必须使用CMake从源代码中创建Visual Studio.sln,使用VS构建,然后由于某些原因,源代码中没有包含文件,因此我从添加到包含目录中。

按照步骤解决它

  • 转到项目右键单击并选择设置->C++->常规->其他包含目录单击编辑并添加此C:\opencv\build\include

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

  • 现在转到设置->链接器->输入并粘贴opencv_world343.lib、opencv_world343d.lib 仅此而已,但对于visual studio 2017和OpenCV 3.4.3,这些设置是正确的,只有其他版本的设置需要相应更改


对于我的情况,无论如何配置visual studio解决方案上的include/library路径和库文件,我都无法删除_interlockedExchangeAdd链接错误

所以我创建了新的VisualStudio解决方案,并添加了include/library路径、库输入文件(opencv_world440.lib)。 W
#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;
};
#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();
};