免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 10417 | 回复: 6
打印 上一主题 下一主题

链接时出现一个undefined reference to 问题,实在不懂为什么 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-12-22 23:07 |只看该作者 |倒序浏览
本帖最后由 _Genesis 于 2011-12-22 23:09 编辑

额。这个问题我实在没搞明白是怎么回事。
先是报错如下:

  1. || g++ -o main ex1.o header.o utils.o imgfeatures.o sift.o main.o -I. -I/home/web/include/opencv -L/home/web/lib -lm -lopencv_core -lopencv_ml -lopencv_highgui  -B/home/web/lib
  2. || main.o: In function `main':
  3. || main.cpp:(.text+0x85): undefined reference to `sift_features(_IplImage*, feature**)'
  4. || main.cpp:(.text+0x9d): undefined reference to `sift_features(_IplImage*, feature**)'
  5. || main.cpp:(.text+0xb5): undefined reference to `stack_imgs1(_IplImage*, _IplImage*)'
  6. || collect2: ld returned 1 exit status
  7. || make: *** [main] 错误 1
复制代码
看提示,是没有sift_features(_IplImage*, feature**)这个函数
下面是代码。
====main.cpp====

  1. #include "header.h"
  2. #include "main.h"
  3. #include "sift.h"
  4. #include "utils.h"
  5. int main(int argc,char* argv[]){
  6.         printf("hello OPENCV!!!\n========\n");
  7.         IplImage* img1;
  8.         IplImage* img2;
  9.         IplImage* stacked;
  10.         img1 = cvLoadImage(argv[1],1);
  11.         img2 = cvLoadImage(argv[2],1);
  12.         _showImg(img1,"img1");
  13.         _showImg(img2,"img2");
  14.         struct feature* ft1,*ft2,*ft;
  15.         int n1, n2;
  16.         n1 = sift_features(img1,&ft1);
  17.         n2 = sift_features(img2,&ft2);
  18.         stacked = stack_imgs1(img1,img2);
  19.         _showImg(stacked,"stacked");
  20. }
复制代码
==== sift.h ====

  1. /*#ifndef SIFT_H*/
  2. /*#define SIFT_H*/
  3. #include "cxcore.h"
  4. /******************************** Structures *********************************/
  5. /** holds feature data relevant to detection */
  6. struct detection_data
  7. {
  8.         int r;
  9.         int c;
  10.         int octv;
  11.         int intvl;
  12.         double subintvl;
  13.         double scl_octv;
  14. };

  15. struct feature;
  16. /******************************* Defs and macros *****************************/
  17. /** default number of sampled intervals per octave */
  18. #define SIFT_INTVLS 3
  19. /** default sigma for initial gaussian smoothing */
  20. #define SIFT_SIGMA 1.6
  21. /** default threshold on keypoint contrast |D(x)| */
  22. #define SIFT_CONTR_THR 0.04
  23. /** default threshold on keypoint ratio of principle curvatures */
  24. #define SIFT_CURV_THR 10
  25. /** double image size before pyramid construction? */
  26. #define SIFT_IMG_DBL 0
  27. /** default width of descriptor histogram array */
  28. #define SIFT_DESCR_WIDTH 4
  29. /** default number of bins per histogram in descriptor array */
  30. #define SIFT_DESCR_HIST_BINS 8
  31. /* assumed gaussian blur for input image */
  32. #define SIFT_INIT_SIGMA 0.5
  33. /* width of border in which to ignore keypoints */
  34. #define SIFT_IMG_BORDER 5
  35. /* maximum steps of keypoint interpolation before failure */
  36. #define SIFT_MAX_INTERP_STEPS 5
  37. /* default number of bins in histogram for orientation assignment */
  38. #define SIFT_ORI_HIST_BINS 36
  39. /* determines gaussian sigma for orientation assignment */
  40. #define SIFT_ORI_SIG_FCTR 1.5
  41. /* determines the radius of the region used in orientation assignment */
  42. #define SIFT_ORI_RADIUS 3.0 * SIFT_ORI_SIG_FCTR
  43. /* number of passes of orientation histogram smoothing */
  44. #define SIFT_ORI_SMOOTH_PASSES 2
  45. /* orientation magnitude relative to max that results in new feature */
  46. #define SIFT_ORI_PEAK_RATIO 0.8
  47. /* determines the size of a single descriptor orientation histogram */
  48. #define SIFT_DESCR_SCL_FCTR 3.0
  49. /* threshold on magnitude of elements of descriptor vector */
  50. #define SIFT_DESCR_MAG_THR 0.2
  51. /* factor used to convert floating-point descriptor to unsigned char */
  52. #define SIFT_INT_DESCR_FCTR 512.0
  53. /* returns a feature's detection data */
  54. #define feat_detection_data(f) ( (struct detection_data*)(f->feature_data) )
  55. /*************************** Function Prototypes *****************************/
  56. extern int sift_features( IplImage* img, struct feature** feat );
  57. extern int _sift_features( IplImage* img, struct feature** feat, int intvls,
  58.                                                   double sigma, double contr_thr, int curv_thr,
  59.                                                   int img_dbl, int descr_width, int descr_hist_bins );
  60. /*#endif*/
复制代码
==== sift.c====

  1. #include "sift.h"
  2. #include "imgfeatures.h"
  3. #include "utils.h"

  4. #include <cxcore.h>
  5. #include <cv.h>

  6. /************************* Local Function Prototypes *************************/

  7. IplImage* create_init_img( IplImage*, int, double );
  8. IplImage* convert_to_gray32( IplImage* );
  9. IplImage*** build_gauss_pyr( IplImage*, int, int, double );
  10. IplImage* downsample( IplImage* );
  11. IplImage*** build_dog_pyr( IplImage***, int, int );
  12. CvSeq* scale_space_extrema( IplImage***, int, int, double, int, CvMemStorage*);
  13. int is_extremum( IplImage***, int, int, int, int );
  14. struct feature* interp_extremum( IplImage***, int, int, int, int, int, double);
  15. void interp_step( IplImage***, int, int, int, int, double*, double*, double* );
  16. CvMat* deriv_3D( IplImage***, int, int, int, int );
  17. CvMat* hessian_3D( IplImage***, int, int, int, int );
  18. double interp_contr( IplImage***, int, int, int, int, double, double, double );
  19. struct feature* new_feature( void );
  20. int is_too_edge_like( IplImage*, int, int, int );
  21. void calc_feature_scales( CvSeq*, double, int );
  22. void adjust_for_img_dbl( CvSeq* );
  23. void calc_feature_oris( CvSeq*, IplImage*** );
  24. double* ori_hist( IplImage*, int, int, int, int, double );
  25. int calc_grad_mag_ori( IplImage*, int, int, double*, double* );
  26. void smooth_ori_hist( double*, int );
  27. double dominant_ori( double*, int );
  28. void add_good_ori_features( CvSeq*, double*, int, double, struct feature* );
  29. struct feature* clone_feature( struct feature* );
  30. void compute_descriptors( CvSeq*, IplImage***, int, int );
  31. double*** descr_hist( IplImage*, int, int, double, double, int, int );
  32. void interp_hist_entry( double***, double, double, double, double, int, int);
  33. void hist_to_descr( double***, int, int, struct feature* );
  34. void normalize_descr( struct feature* );
  35. int feature_cmp( void*, void*, void* );
  36. void release_descr_hist( double****, int );
  37. void release_pyr( IplImage****, int, int );


  38. /*********************** Functions prototyped in sift.h **********************/

  39. int sift_features( IplImage* img, struct feature** feat )
  40. {
  41.         return _sift_features( img, feat, SIFT_INTVLS, SIFT_SIGMA, SIFT_CONTR_THR,
  42.                                                         SIFT_CURV_THR, SIFT_IMG_DBL, SIFT_DESCR_WIDTH,
  43.                                                         SIFT_DESCR_HIST_BINS );
  44. }

  45. int _sift_features( IplImage* img, struct feature** feat, int intvls,
  46.                                    double sigma, double contr_thr, int curv_thr,
  47.                                    int img_dbl, int descr_width, int descr_hist_bins )
  48. {
  49.         IplImage* init_img;
  50.         IplImage*** gauss_pyr, *** dog_pyr;
  51.         CvMemStorage* storage;
  52.         CvSeq* features;
  53.         int octvs, i, n = 0;

  54.         /* check arguments */
  55.         if( ! img )
  56.                 fatal_error( "NULL pointer error, %s, line %d",  __FILE__, __LINE__ );

  57.         if( ! feat )
  58.                 fatal_error( "NULL pointer error, %s, line %d",  __FILE__, __LINE__ );

  59.         /* build scale space pyramid; smallest dimension of top level is ~4 pixels */
  60.         init_img = create_init_img( img, img_dbl, sigma );
  61.         octvs = log( MIN( init_img->width, init_img->height ) ) / log(2) - 2;
  62.         gauss_pyr = build_gauss_pyr( init_img, octvs, intvls, sigma );
  63.         dog_pyr = build_dog_pyr( gauss_pyr, octvs, intvls );

  64.         storage = cvCreateMemStorage( 0 );
  65.         features = scale_space_extrema( dog_pyr, octvs, intvls, contr_thr,
  66.                 curv_thr, storage );
  67.         calc_feature_scales( features, sigma, intvls );
  68.         if( img_dbl )
  69.                 adjust_for_img_dbl( features );
  70.         calc_feature_oris( features, gauss_pyr );
  71.         compute_descriptors( features, gauss_pyr, descr_width, descr_hist_bins );

  72.         /* sort features by decreasing scale and move from CvSeq to array */
  73.         cvSeqSort( features, (CvCmpFunc)feature_cmp, NULL );
  74.         n = features->total;
  75.         *feat = calloc( n, sizeof(struct feature) );
  76.         *feat = cvCvtSeqToArray( features, *feat, CV_WHOLE_SEQ );
  77.         for( i = 0; i < n; i++ )
  78.         {
  79.                 free( (*feat)[i].feature_data );
  80.                 (*feat)[i].feature_data = NULL;
  81.         }

  82.         cvReleaseMemStorage( &storage );
  83.         cvReleaseImage( &init_img );
  84.         release_pyr( &gauss_pyr, octvs, intvls + 3 );
  85.         release_pyr( &dog_pyr, octvs, intvls + 2 );
  86.         return n;
  87. }

  88. ...... 后面省略,应该没有问题吧
复制代码
==== Makefile ====

  1. CC = g++
  2. C_ = gcc
  3. CFLAGS = -c -Wall
  4. COM_H = header.h
  5. Objs =  ex1.o header.o utils.o imgfeatures.o sift.o main.o
  6. CVs = -I. -I/home/web/include/opencv -L/home/web/lib -lm -lopencv_core -lopencv_ml -lopencv_highgui

  7. main: $(Objs)
  8.         $(CC) -o main $(Objs) $(CVs) -B/home/web/lib

  9. header.o: header.cpp header.h
  10.         $(CC) $(CFLAGS) -o header.o header.cpp $(CVs) -B/home/web/lib

  11. ex1.o: ex1.cpp ex1.h $(COM_H)
  12.         $(CC) $(CFLAGS) -o ex1.o ex1.cpp $(CVs) -B/home/web/lib

  13. utils.o:utils.c
  14.         $(C_) $(CFLAGS) -o utils.o utils.c $(CVs) -B/home/web/lib

  15. imgfeatures.o:imgfeatures.c utils.c
  16.         $(C_) $(CFLAGS) -o imgfeatures.o imgfeatures.c $(CVs) -B/home/web/lib

  17. sift.o:sift.c utils.c imgfeatures.c
  18.         $(C_) $(CFLAGS) -o sift.o sift.c $(CVs) -B/home/web/lib

  19. main.o: main.cpp main.h $(COM_H)
  20.         $(CC) $(CFLAGS) -o main.o main.cpp $(CVs) -B/home/web/lib

  21. clean:
  22.         rm main $(Objs)
复制代码
然后是这个:

  1. web@pc-laptop:~/workTree/panorama/SIFT$ strings sift.o | grep feature
  2. sizeof(((features))->first[0]) == sizeof(CvSeqBlock) && ((features))->elem_size == sizeof(struct feature)
  3. calc_feature_scales
复制代码

论坛徽章:
1
天蝎座
日期:2013-12-06 18:23:58
2 [报告]
发表于 2011-12-22 23:11 |只看该作者
undefined reference to 没有找到这几个函数定义的地方。。。


ps:能把代码括起来吗?

论坛徽章:
0
3 [报告]
发表于 2011-12-22 23:45 |只看该作者
使用[code][/code]阔气来把?已经括了。实在不好意思,忘记了。

确实是 reference to 的问题。但是,我觉得,没有问题啊,在link需要的函数,在sift.c里面,sift.h 里面声明了extern 函数XXX 。为什么main.c里面还是 reference to的问题呢?

论坛徽章:
0
4 [报告]
发表于 2012-01-02 16:49 |只看该作者
楼主解决了问题吗?我也碰到这个情况,,,刚开始项目编译都没什么问题的,,但有时候编译就出现的这样的问题,,不知道怎么弄,

论坛徽章:
0
5 [报告]
发表于 2012-01-02 16:52 |只看该作者
我的qq413841175 交流

论坛徽章:
0
6 [报告]
发表于 2012-01-04 10:18 |只看该作者
extern "C" {
#include "sift.h"
}

论坛徽章:
0
7 [报告]
发表于 2012-01-04 14:40 |只看该作者
在c++的文件中编译.c文件,要在.c对应的.h文件中加上
#ifdef __cplusplus
extern "C"
{
#endif

函数声明

#ifdef __cplusplus
}
#endif

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP