web-dev-qa-db-fra.com

erreur LNK2019: symbole externe non résolu ouvert

J'ai écrit ce programme simple qui charge les matrices à partir de fichiers txt et calcule les distances . Lors de la compilation du programme dans Visual Studio sous Windows, les erreurs suivantes apparaissent:

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

J’ai inséré 2.4.6 sur mon ordinateur et l’a lié correctement à 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();
};

Qu'est-ce que je fais mal????

22
RamBracha

Comme d'autres, vous devez vous assurer que vous vous connectez correctement aux bibliothèques OpenCV. 

Vérifiez que votre Projet -> Propriétés -> Répertoires VC++ -> Répertoires de bibliothèque , inclut le chemin où se trouvent les bibliothèques OpenCV. Ordinateur 32 bits sous VS2012, cela variera selon les configurations).

Ensuite, vérifiez que les bibliothèques suivantes sont incluses dans votre Projet -> Propriétés -> Éditeur de liens -> Entrée -> Dépendances supplémentaires :

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

Si les réponses ci-dessus sont correctes, vous ne devriez plus avoir d’erreurs de liaison OpenCV.

23
RedFred

Peut-être que vous construisez pour win32 mais en vous connectant à x64. Si vous définissez votre application sur x64, elle se construira alors que dans win32, il y aura des erreurs de liaison. Faites un clic droit sur la solution et allez à la configuration, colonne de la plateforme. J'ai trouvé cela difficile à régler, je me demande si c'est un buggy ou pas.

13
Elliot

Vous avez probablement inclus les bons fichiers d'en-tête, mais vous avez oublié d'ajouter la bibliothèque. Vous devez ajouter le fichier * .lib correspondant dans les paramètres du projet.

1
hunger

Toutes les réponses vont dans la bonne direction mais je souhaite mettre à jour ce que RedFred a répondu à la dernière version mise à jour (4.0.0), changez les bibliothèques pour lesquelles il a mentionné:

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 

Pour les versions suivantes ou précédentes, allez simplement dans votre répertoire lib du répertoire de votre opencv et recherchez chacun des éléments de la liste que RedFred ou j’ai fournis (évidemment copier-coller jusqu’à la dernière lettre précédant le numéro de version, dans mon cas 400) et créez votre propre liste de dépendances supplémentaires pour votre éditeur de liens.

En passant, je devais créer mon fichier .sln Visual Studio à partir du code source à l'aide de CMake, compiler avec VS, puis pour une raison quelconque, le fichier source ne contenait aucun fichier d'inclusion; je les ai donc ajoutés à mes répertoires d'inclusion à partir du Win pack .

0
Felipe Gutierrez