2013-08-21 71 views
14

Tôi đã viết chương trình đơn giản này tải ma trận từ tệp txt và tính khoảng cách. Khi biên dịch chương trình trong visual studio trên cửa sổ tôi nhận được các lỗi sau đây:lỗi LNK2019: biểu tượng bên ngoài chưa được giải quyết 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)" (?_in[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 

tôi intsalled opencv 2.4.6 trên máy tính của tôi và liên kết nó với visual studio đúng cách.

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

am i làm gì sai ????

+1

Dường như thư viện CV đã * không * được liên kết với dự án của bạn đúng cách. –

+1

Và do đó LNK2019 lại xuất hiện lần thứ ba ... :-D – Abhineet

+1

Tôi đang có trải nghiệm tương tự. Các biểu tượng bị thiếu tương tự (ngoại lệ và interlockedExchangeAdd) xuất hiện. Nếu tôi loại bỏ các libs opencv sau đó tôi nhận được nhiều biểu tượng mất tích nhiều hơn, nhưng khi chúng được thêm vào, các ký hiệu cụ thể này vẫn còn thiếu. có điều gì khác đang xảy ra ở đây .. –

Trả lời

1

Bạn có thể bao gồm các tệp tiêu đề phù hợp nhưng quên thêm thư viện. Bạn cần thêm tệp * .lib tương ứng vào các thiết lập của dự án.

+1

Tôi đã thêm C: \ OpenCV \ build \ include \ opencv để bao gồm các thư mục. và C: \ OpenCV \ build \ x64 \ vc11 \ lib vào thư mục dirs và opencv_ .lib để bổ sung các phụ thuộc trong trình liên kết. cộng với con trỏ máy tính của tôi biến điểm đến C: \ OpenCV \ build \ x64 \ vc11 \ bin dll dir. còn gì nữa – RamBracha

+0

tất cả các thông báo gợi ý thiếu opencv_core246.lib (hoặc phiên bản bạn đã có) – berak

+0

Hướng dẫn nào bạn đã sử dụng để thiết lập OpenCV với Visual Studio? Bạn nên chỉnh sửa biến đường dẫn của mình thành C: \ OpenCV \ build \ x64 \ vc10 \ không có "\ bin". Ngoài ra với Visual Studio 2010, bạn nên sử dụng thư mục vc10 thay vì vc11. – OpenMinded

17

Giống như những người khác được đề cập, bạn cần đảm bảo bạn đang liên kết với các lib OpenCV chính xác.

Kiểm tra xem bạn Project -> Properties -> VC++ Thư mục -> Thư viện Thư mục, bao gồm đường dẫn nơi các thư viện OpenCV là, mặc định sẽ là 'C: \ OpenCV \ xây dựng \ x86 \ vc11 \ lib' (trên một máy 32 bit chạy VS2012, nó sẽ thay đổi trên các thiết lập khác).

Tiếp theo, kiểm tra xem các thư viện sau đây được bao gồm trong dự án của bạn -> Properties -> Linker -> Input -> Dependencies bổ sung:

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

Nếu ở trên là chính xác, bạn không nên lấy thêm bất kỳ lỗi nào liên kết OpenCV.

+0

Điều này làm việc cho tôi .. Cảm ơn – Hetal

+0

Đã không làm việc cho tôi, tôi vẫn nhận được lỗi tương tự. –

+0

[Điều này có thể đã lỗi thời] (http://answers.opencv.org/question/122936/library-to-link-for-imread/), lib duy nhất tôi nhận được trong phân phối nhị phân của opencv 3.4.1 là opencv_world341d.lib. – jrh

9

Có lẽ bạn đang xây dựng cho win32 nhưng liên kết đến x64. Nếu bạn thiết lập ứng dụng của mình thành x64 thì nó sẽ xây dựng, trong khi đó trong win32 nó cho các lỗi liên kết. Nhấp chuột phải vào giải pháp và đi đến cấu hình, cột nền tảng. Tôi thấy nó khó khăn để thiết lập này, tôi tự hỏi nếu nó là lỗi hay không.

+1

Điều này làm việc cho tôi. Thanks.Saved rất nhiều thời gian. –

+1

Điều này giải quyết được vấn đề của tôi. Cảm ơn rất nhiều. – seleucia

Các vấn đề liên quan