一个GCC4.6.3的奇妙问题的糊涂解决方案

63 篇文章 0 订阅
52 篇文章 0 订阅

因为工作中需要OpenCV,又不想用Windows,所以我就在Linux下编译安装OpenCV了。一开始,由于我的计算机不能安装ubuntu12.04,所以我安装了ubuntu13.04,opencv的环境也配置的很顺利。


Ubuntu 13.04 的编译问题与解决方案

在13.04上安装完成opencv后,我便兴冲冲的实验了一把,其实我的程序什么也没有做,只是添加了包含了opencv的头文件而已。然后我就编译呀。

// file : test.c
#include <opencv/cv.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
    return 0;
}

编译命令:

$ gcc -c test.c 
$ gcc test.o -o test

结果就输出了一堆如下的信息:

test.o:test.c:function cvDecRefData: error: undefined reference to 'cvFree_'
test.o:test.c:function cvDecRefData: error: undefined reference to 'cvFree_'
test.o:test.c:function cvGetRow: error: undefined reference to 'cvGetRows'
test.o:test.c:function cvGetCol: error: undefined reference to 'cvGetCols'
test.o:test.c:function cvReleaseMatND: error: undefined reference to 'cvReleaseMat'
test.o:test.c:function cvSubS: error: undefined reference to 'cvAddS'
test.o:test.c:function cvCloneSeq: error: undefined reference to 'cvSeqSlice'
test.o:test.c:function cvSetNew: error: undefined reference to 'cvSetAdd'
test.o:test.c:function cvGetSetElem: error: undefined reference to 'cvGetSeqElem'
test.o:test.c:function cvEllipseBox: error: undefined reference to 'cvEllipse'
test.o:test.c:function cvFont: error: undefined reference to 'cvInitFont'
test.o:test.c:function cvReadIntByName: error: undefined reference to 'cvGetFileNodeByName'
test.o:test.c:function cvReadRealByName: error: undefined reference to 'cvGetFileNodeByName'
test.o:test.c:function cvReadStringByName: error: undefined reference to 'cvGetFileNodeByName'
test.o:test.c:function cvReadByName: error: undefined reference to 'cvGetFileNodeByName'
test.o:test.c:function cvReadByName: error: undefined reference to 'cvRead'
test.o:test.c:function cvContourPerimeter: error: undefined reference to 'cvArcLength'
test.o:test.c:function cvCalcHist: error: undefined reference to 'cvCalcArrHist'
collect2: error: ld returned 1 exit status

我就纳闷了呀,我的程序里只是包含了头文件啊,怎么会使用opencv的函数呢,所以每必要产生这么多对opencv库函数的依赖呀!

于是,我看了看编译时产生的汇编代码,看看是否有啥蹊跷。

gcc -S test.c

打开test.s文件,看看,果然又很多opencv的库函数呢。于是我就开始添加opencv的库。最后就这样解决了。

$ gcc test.o -o test -lopencv_core -L/usr/local/lib -lopencv_highgui

12.04下gcc的问题与解决


跟在13.04下差不多的情况,按照上面的过程就可以解决了。

正当我为解决了这个问题高兴的时候,我遇到了新的问题。

一般我习惯把输入文件放到编译命令的最后,结果这样照样会出现问题。

出现问题的编译命令是:

$ gcc  -o test -lopencv_core -L/usr/local/lib -lopencv_imgproc test.o

我发现 只要把test.o放到-l..这些选项的前面,就不会出现函数未定义的问题。


就这样算解决了把。以前我也遇到过由于链接库的位置不对而导致链接不正确的情况,看来编译器还不是那么智能啊,以后要小心了。









  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值