ubuntu11.10 下编译 opencv2.4 所遇到的问题总结

来源:互联网 发布:php 序列化 编辑:程序博客网 时间:2024/05/18 18:14

按照http://www.samontab.com/web/2012/06/installing-opencv-2-4-1-ubuntu-12-04-lts/ 和http://www.ozbotz.org/opencv-installation/  这两个网址的方法,到最后,我编译运行 ./build_all.sh 这个命令时,遇到了 下列问题:


"

/usr/bin/ld: /tmp/cc5jLKOJ.o: undefined reference to symbol 'sin@@GLIBC_2.0'
/usr/bin/ld: note: 'sin@@GLIBC_2.0' is defined in DSO /lib/i386-linux-gnu/libm.so.6 so try adding it to the linker command line
/lib/i386-linux-gnu/libm.so.6: could not read symbols: Invalid operation
collect2: ld 返回 1

"

经过查找测试,不知道什么原因,非要在 build_all.sh 这个脚本内 每个 gcc 命令行内加上 -lm 这个语句,才可以重新编译成功,可是我以前在其他电脑里面编译这个脚本时,就从来没有碰到过这个怪问题,可能我把哪个编译环境给修改了吗?搞不懂,先记录下这个问题吧!


下面是我修改后的 build_all.sh 文件内容:


#!/bin/sh


if [ $# -gt 0 ] ; then
base=`basename $1 .c`
echo "compiling $base"
gcc -ggdb `pkg-config opencv --cflags --libs` $base.c -o $base -lm 
else
for i in *.c; do
   echo "compiling $i"
   gcc -ggdb `pkg-config --cflags opencv` -o `basename $i .c` $i `pkg-config --libs opencv` -lm;
done
for i in *.cpp; do
   echo "compiling $i"
   g++ -ggdb `pkg-config --cflags opencv` -o `basename $i .cpp` $i `pkg-config --libs opencv` -lm;
done
fi



后来,在Qt Creator 中编译一个例子程序时,有碰到下列问题:


"

 error while loading shared libraries: libopencv_highgui.so.2.4: cannot open shared object file: No such file or directory

"

经研究,在http://www.cnblogs.com/amboyna/archive/2008/02/06/1065322.html这个网站中找到了问题的所在:


./tests: error while loading shared libraries: xxx.so.0:cannot open shared object file: No such file or directory


那就表示系統不知道xxx.so 放在哪個目錄下。

這個時候就要在/etc/ld.so.conf中加入xxx.so所在的目錄。

 一般而言,有很多so檔會在/usr/local/lib這個目錄下,所以在/etc/ld.so.conf中加入/usr/local/lib這一行,可以解決此問題。

將 /etc/ld.so.conf存檔後,還要執行「/sbin/ldconfig –v」來更新一下才會生效。



如果遇到以下错误时:

“error while loading shared libraries: libGL.so.1: cannot open shared object file: No such file or directory ”

可以先使用语句 locate libGL.so.1 定位,如果没有输出,那就表示openGL的库没有安装上,需要使用以下语句 

sudo apt-get install --reinstall libgl1-mesa-glx

安装完成后,再使用语句 locate  libGL.so.1 , 输出为:

/usr/lib/i386-linux-gnu/mesa/libGL.so.1
/usr/lib/i386-linux-gnu/mesa/libGL.so.1.2


此时,就可以在 /etc/ld.so.conf 中加入 libGL.so 所在目录 : /usr/lib/i386-linux-gnu/mesa

然后,使用语句  sudo ldconfig -v   更新一下就OK了!


最后,编译opencv的程序使用下列命令:

g++ `pkg-config opencv --cflags` my_code.cpp  -o my_code `pkg-config opencv --libs` 

原创粉丝点击