zoukankan      html  css  js  c++  java
  • 一个GCC4.6.3的奇妙问题的糊涂解决方案

    因为工作中需要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..这些选项的前面,就不会出现函数未定义的问题。


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









  • 相关阅读:
    分享一些书籍,方方面面,很多值得一读
    C#网络爬虫--多线程处理强化版
    图书管理系统
    jquery完成界面无刷新加载登陆注册
    springboot jar项目 改为war项目
    nginx 配置文件配置(ssl和代理80端口)
    linux 安装mysql8.0
    linux redis安装和启动,远程链接
    linux nginx 安装启动
    linux tar方式安装配置jdk
  • 原文地址:https://www.cnblogs.com/pangblog/p/3249264.html
Copyright © 2011-2022 走看看