2016-09-21 150 views
0

解決:FOR OPENCV解決方案向上看回答Jvinniec FOR THE FAST.H解決方案向上看編輯的XCode構建失敗「爲架構x86_64的未定義符號」

我擡頭幾個類似的問題,並沒有找到一個解決方案爲我的代碼呢。我得到的錯誤如下所示:

Undefined symbols for architecture x86_64: 
"fast9_score(unsigned char const*, int, xy*, int, int)", referenced from: 
    detectCornersFast(unsigned char const*, int, int, int, int, int*) in main.o 
"fast9_detect(unsigned char const*, int, int, int, int, int*)", referenced from: 
    detectCornersFast(unsigned char const*, int, int, int, int, int*) in main.o 
"nonmax_suppression(xy const*, int const*, int, int*)", referenced from: 
    detectCornersFast(unsigned char const*, int, int, int, int, int*) in main.o 
"cv::namedWindow(cv::String const&, int)", referenced from: 
    _main in main.o 
"cv::GaussianBlur(cv::_InputArray const&, cv::_OutputArray const&, cv::Size_<int>, double, double, int)", referenced from: 
    _main in main.o 
"cv::Mat::deallocate()", referenced from: 
    cv::Mat::release() in main.o 
"cv::Mat::copySize(cv::Mat const&)", referenced from: 
    cv::Mat::operator=(cv::Mat const&) in main.o 
    cv::Mat::Mat(cv::Mat const&) in main.o 
"cv::String::deallocate()", referenced from: 
    cv::String::~String() in main.o 
"cv::String::allocate(unsigned long)", referenced from: 
    cv::String::String(char const*) in main.o 
    cv::String::String(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) in main.o 
"cv::imread(cv::String const&, int)", referenced from: 
    _main in main.o 
"cv::imshow(cv::String const&, cv::_InputArray const&)", referenced from: 
    _main in main.o 
"cv::resize(cv::_InputArray const&, cv::_OutputArray const&, cv::Size_<int>, double, double, int)", referenced from: 
    _main in main.o 
"cv::waitKey(int)", referenced from: 
    _main in main.o 
"cv::fastFree(void*)", referenced from: 
    cv::Mat::~Mat() in main.o 
ld: symbol(s) not found for architecture x86_64 
clang: error: linker command failed with exit code 1 (use -v to see invocation) 

我在XCode中使用的架構是Universal。我建立並添加了openCV的搜索路徑,並且fast9方法位於fast.h頭文件中。我試圖刪除項目的派生數據,但沒有任何幫助。在這裏我的main.cpp中的代碼:

#include <vector> 
#include <iostream> 
#include <cmath> 
#include <string> 
#include "opencv2/opencv.hpp" 
#include "fast.h" 

using namespace std; 
using namespace cv; 

typedef unsigned char byte; 

Mat src; Mat dst; Mat tmp; 
int rows; int cols; 
byte* image; 
xy* nonmax; 

byte * matToBytes(Mat image){ 

int size = image.total() * image.elemSize(); 
byte * bytes = new byte[size]; 
std::memcpy(bytes, image.data, size * sizeof(byte)); 

return bytes; 

} 

xy* detectCornersFast(const byte* im, int xsize, int ysize, int stride, int b, int* ret_num_corners){ 

xy* corners; 
int num_corners; 
int* scores;  
xy* nonmax; 

corners = fast9_detect(im, xsize, ysize, stride, b, &num_corners); 
scores = fast9_score(im, stride, corners, num_corners, b);  
nonmax = nonmax_suppression(corners, scores, num_corners, ret_num_corners); 

free(corners); 
free(scores); 

return nonmax; 

} 

int main(int argc, const char * argv[]) { 

// insert code here... 

vector<Mat> img; 
vector<int> num_corners; 
vector<xy*> nms; 

src = imread(argv[1],1); 

img.push_back(src); 
double factor = sqrt(2); 

for(int i = 0; i < 5; i++){ 

    int ret_num_corners; 
    rows = src.rows/factor; 
    cols = src.cols/factor; 

    resize(src, tmp, Size(rows,cols),0,0, INTER_NEAREST); 

    GaussianBlur(tmp, dst, Size(5,5),0,0); 

    image = matToBytes(dst); 

    nonmax = detectCornersFast(image, tmp.rows, tmp.cols, tmp.rows, 20, &ret_num_corners); 

    img.push_back(dst); 
    nms.push_back(nonmax); 
    num_corners.push_back(ret_num_corners); 

    src = dst; 

} 

for(int i = 0; i <img.size(); i++){ 

    string name = "Display Window " + std::to_string(i); 
    cout << "Number of Corners" << num_corners[i] << endl; 
    cout << "Nonmax Supression" << nms[i] << endl; 
    namedWindow(name, WINDOW_AUTOSIZE); 
    imshow(name, img[i]); 
} 

waitKey(); 
return 0; 
} 

編輯: 對於OpenCV的幫助,從這個教程添加鏈接標誌:OpenCV 的方法,由快是一個.c文件是在我的項目夾。 .h文件也在那裏。我只是在這裏發佈.h文件,因爲.c文件太長,因爲它的機械生成。

SOLUTION: 這是我的快速方法的錯誤。因爲它調用C文件而不是C++文件,所以我完全忘記了在main.cpp中添加Extern "C" {}#include "fast.h"

fast.h

#ifndef FAST_H 
#define FAST_H 

typedef struct { int x, y; } xy; 
typedef unsigned char byte; 

int fast9_corner_score(const byte* p, const int pixel[], int bstart); 

xy* fast9_detect(const byte* im, int xsize, int ysize, int stride, int b, int* ret_num_corners); 

int* fast9_score(const byte* i, int stride, xy* corners, int num_corners, int b); 

xy* fast9_detect_nonmax(const byte* im, int xsize, int ysize, int stride, int b, int* ret_num_corners); 

xy* nonmax_suppression(const xy* corners, const int* scores, int num_corners, int* ret_num_nonmax); 

#endif 
+0

這將有助於查看生成錯誤的實際命令...但它看起來像是你沒有鏈接到你使用的庫,或者它們不是爲x86_64構建的 – Dmitri

+0

對於快速方法(這在我的項目文件夾中)可能是可能的,但是當我構建openCV時,我配置了cmake它將爲x86_64編譯 –

回答

2

這聽起來好像你只加在你的項目,這些依賴的頭搜索路徑「生成設置」 - >「搜索路徑」 - >「用戶頭搜索路徑「字段。這將使xcode知道這些類在開發過程中存在,但不會啓用您的項目。您還應該確保您的項目知道在哪裏找到正在使用openCV和fast9符號的庫(即,其中.so文件適用於這兩種依賴關係)的庫。您可以通過在「構建設置」 - >「鏈接」 - >「其他鏈接器標誌」下添加相應的鏈接器標誌來完成此操作。還要確保這些字段是爲您正在編譯的TARGET設置的,而不僅僅是PROJECT。

如果你不確定鏈接標誌是什麼,這裏是一個簡單的例子。我想鏈接以下庫:

/path/to/some/project/lib/libfoo.so

我將以下內容添加到「其他鏈接器標記」字段:

-L/path/to/some/project/lib -lfoo

基本上-L之前的目錄和-l之前的名稱沒有'lib'和'.so'的圖書館。注:每當我在xcode項目中包含外部依賴關係時,爲了防止任何依賴項包含標題,我還將「生成設置」 - >「搜索路徑」 - >「總是搜索用戶路徑」設置爲「是」尖括號如#include <header.h>

+0

編輯:好的,我添加了來自opencv教程的鏈接器標誌,現在我只用快速方法得到錯誤。我檢查了「總是搜索用戶路徑」是否是。快速方法和標題包含在我的項目文件夾中。我將添加.h文件到我的問題可能有某事。錯誤? –

+0

在編譯源中有快速的.c文件。 –

+0

沒關係我知道錯誤在哪裏! –

相關問題