2013-08-21 106 views
14

私はこの単純なプログラムを書いて、txtファイルから行列を読み込んで距離を計算しました。 Windows上でVisual Studioでプログラムをコンパイルする場合 私は、次のエラーを取得:エラーLNK2019:未解決の外部シンボルopencv

1>main.obj : error LNK2019: unresolved external symbol "void __cdecl cv::fastFree(void *)" ([email protected]@@[email protected]) referenced in function "public: __thiscall  cv::Mat::~Mat(void)" ([email protected]@@[email protected]) 
1>system.obj : error LNK2001: unresolved external symbol "void __cdecl cv::fastFree(void *)" ([email protected]@@[email protected]) 
1>main.obj : error LNK2019: unresolved external symbol "public: void __thiscall cv::Mat::deallocate(void)" ([email protected]@[email protected]@QAEXXZ) referenced in function "public: void __thiscall cv::Mat::release(void)" ([email protected]@[email protected]@QAEXXZ) 
1>system.obj : error LNK2001: unresolved external symbol "public: void __thiscall cv::Mat::deallocate(void)" ([email protected]@[email protected]@QAEXXZ) 
1>main.obj : error LNK2019: unresolved external symbol "int __cdecl cv::_interlockedExchangeAdd(int *,int)" ([email protected]@@[email protected]) referenced in function "public: void __thiscall cv::Mat::release(void)" ([email protected]@[email protected]@QAEXXZ) 
1>system.obj : error LNK2001: unresolved external symbol "int __cdecl cv::_interlockedExchangeAdd(int *,int)" ([email protected]@@[email protected]) 
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)" ([email protected]@@[email protected][email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected]) referenced in function "public: int & __thiscall cv::Mat::at<int>(int,int)" ([email protected]@[email protected]@@[email protected]) 
1>system.obj : error LNK2019: unresolved external symbol "public: virtual __thiscall cv::Exception::~Exception(void)" ([email protected]@@[email protected]) referenced in function "public: int & __thiscall cv::Mat::at<int>(int,int)" ([email protected]@[email protected]@@[email protected]) 
1>system.obj : error LNK2019: unresolved external symbol "void __cdecl cv::error(class cv::Exception const &)" ([email protected]@@[email protected]@@Z) referenced in function "public: int & __thiscall cv::Mat::at<int>(int,int)" ([email protected]@[email protected]@@[email protected]) 
1>system.obj : error LNK2019: unresolved external symbol "public: void __thiscall cv::Mat::create(int,int const *,int)" ([email protected]@[email protected]@[email protected]) referenced in function "public: void __thiscall cv::Mat::create(int,int,int)" ([email protected]@[email protected]@[email protected]) 
1>C:\Users\Ram\documents\visual studio 2012\Projects\descrip\Debug\descrip.exe : fatal error LNK1120: 7 unresolved externals 

は、私は自分のコンピュータ上のOpenCV 2.4.6をintsalled、適切に視覚的なスタジオにそれをリンクさ。私が間違っているのは何

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(); 
}; 

????

+1

CVライブラリはあなたのプロジェクトに適切にリンクされていないようです。 –

+1

そして、LNK2019が何十時間も再びポップアップしました:-D – Abhineet

+1

私は同じような経験をしています。これらの同じ欠落シンボル(例外およびinterlockedExchangeAdd)が表示されます。私はopencvのライブラリを削除すると、さらに多くのシンボルが失われますが、それらがすべて追加されると、これらのシンボルはまだ失われています。ここに何か他のことがあります.. –

答えて

1

おそらく、適切なヘッダーファイルが含まれていますが、ライブラリを追加するのを忘れていました。プロジェクト設定で対応する* .libファイルを追加する必要があります。

+1

私はC:\ OpenCV \ build \ include \ opencvを追加してディレクトリを追加しました。およびC:\ OpenCV \ build \ x64 \ vc11 \ libをライブラリディレクトリに、opencv_ .libをリンカの追加の依存関係に追加します。私のコンピュータパス変数はC:\ OpenCV \ build \ x64 \ vc11 \ bin dllディレクトリを指しています。ほかに何か? – RamBracha

+0

すべてのメッセージにopencv_core246.libがない(またはそのバージョンがあります) – berak

+0

Visual StudioでOpenCVをセットアップするために使用したチュートリアルは何ですか?パス変数を "\ bin"を除いてC:\ OpenCV \ build \ x64 \ vc10 \に編集する必要があります。また、Visual Studio 2010では、vc11の代わりにvc10フォルダを使用する必要があります。 – OpenMinded

17

他にも述べたように、OpenCVライブラリに正しくリンクしていることを確認する必要があります。 (: '\ OpenCVの\ \ X86 \ VC11 \ libに構築C'>のプロパティ - - - > VC++ディレクトリ]> [ライブラリディレクトリ、OpenCVのライブラリがあるパスが含まれ、デフォルトでは以下のようになり、あなたのプロジェクトがあること

チェックVS2012を実行している32ビットマシンでは、他の設定では異なります)。

次に、以下のライブラリがあなたのプロジェクトに含まれていることを確認してください - >プロパティ - >リンカ - >入力 - >追加の依存

opencv_core246d.lib
opencv_imgproc246d.lib
opencv_highgui246d。
opencv_ml246d.lib
opencv_video246d.lib

opencv_features2d246d.lib opencv_cal libにib3d246d.lib
opencv_objdetect246d.lib
opencv_contrib246d.lib
opencv_legacy246d.lib
opencv_flann246d.lib

上記が正しい場合は、任意のより多くのOpenCVのリンクエラーを取得するべきではありません。

+0

これは私のために働いた..ありがとう – Hetal

+0

私のために働いていない、私はまだ同じエラーを取得しています。 –

+0

[これは時代遅れかもしれません](http://answers.opencv.org/question/122936/library-to-link-for-imread/)、opencv 3.4.1のバイナリディストリビューションで得られる唯一のlibですopencv_world341d.lib。 – jrh

9

おそらくあなたはwin32用にビルドしていますが、x64にはリンクしています。アプリケーションをx64に設定すると、ビルドされますが、win32ではリンクエラーが発生します。ソリューションを右クリックし、設定、プラットフォームの列に移動します。私はこれを設定するのが難しいと思った、それはバギーかどうか疑問に思います。

+1

これは私のために働いた。ありがとう。たくさんの時間を節約しました。 –

+1

これは私の問題を解決します。どうもありがとう。 – seleucia

関連する問題