[英]error LNK2019: unresolved external symbol 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(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。
主程序
#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;
}
系統文件
#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();
};
我究竟做錯了什么????
像其他人提到的那樣,您需要確保正確鏈接到 OpenCV 庫。
檢查您的Project -> Properties -> VC++ Directories -> Library Directories ,包括 OpenCV 庫所在的路徑,默認為 'C:\\opencv\\build\\x86\\vc11\\lib'(在 32 位機器上運行 VS2012,它會在其他設置上有所不同)。
接下來,檢查以下庫是否包含在您的Project -> Properties -> Linker -> Input -> Additional Dependencies 中:
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 鏈接錯誤。
也許您正在為 win32 構建但鏈接到 x64。 如果您將應用程序設置為 x64,那么它將構建,而在 win32 中,它會出現鏈接錯誤。 右鍵單擊解決方案並轉到配置,平台列。 我發現設置這個很棘手,我想知道它是否有問題。
按照步驟解決
轉到 Project 右鍵單擊並選擇 settings->C++->General->Additional include directory 單擊編輯並添加此 C:\\opencv\\build\\include。
現在轉到設置->鏈接器->常規->附加庫目錄並粘貼 C:\\opencv\\build\\x64\\vc15\\lib
現在轉到設置->鏈接器->輸入並粘貼 opencv_world343.lib,opencv_world343d.lib 就是這樣,但這些設置適用於 Visual Studio 2017 和 OpenCV 3.4.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 的包含目錄中。
任何其他人如果只用幾個未解析的外部符號opencv_world450d.lib
這個問題,請注意在構建 Debug 時應該鏈接opencv_world450.lib
在構建 Release 時應該鏈接opencv_world450.lib
。
您可能包含了正確的頭文件,但忘記添加庫。 需要在工程設置中添加對應的*.lib文件。
我按照opencv教程遇到了問題。 我通過在屬性-通用-鏈接器-輸入-附加依賴項中添加 opencv_world450.lib 后添加 opencv_world450d.lib 來修復它。
就我而言,無論我的 Visual Studio 解決方案如何配置包含/庫路徑和庫文件,我都無法刪除 _interlockedExchangeAdd 鏈接錯誤。
所以我創建了新的 Visual Studio 解決方案並添加了包含/庫路徑、庫輸入文件(opencv_world440.lib)。 有了這個,我可以構建解決方案。
我以前的解決方案可能在解決方案中包含子項目,因此解決方案配置可能存在一些我無法弄清楚的問題。
還有一件事,對於調試構建,您應該使用調試庫(opencv_world440.lib)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.