OpenCV 2.4.1未定义的引用

时间:2013-04-24 21:04:01

标签: c++ opencv g++

我最近在this tutorial之后运行Ubuntu 12.04(64位)的桌面上安装了OpenCV 2.4.1,但每次我尝试编译代码时,甚至包含使用预构建编译的示例脚本,我得到了关于cv命名空间中每个函数的未定义引用。

我目前正在尝试编译的代码是来自OpenCV文档的this example。我已经尝试了3种不同的方法来编译这段代码:

I-按照文档站点的说明使用CMake;

II-使用gcc和pkg-config并使用以下命令:

gcc `pkg-config --cflags opencv` display_image.cpp `pkg-config --libs opencv` -o DisplayImage

III-使用我自己的makefile并直接添加库和标题路径:

CC = g++
INCLUDE = -I/usr/local/include -I/usr/local/include/opencv

LIBS = -L/usr/local/lib -lopencv_core -lopencv_highgui 

%: %.cpp 
    $(CC) -o $@ $< $(INCLUDE) $(LIBS) 

所有这些都产生相同的输出:

/tmp/ccufJQVm.o: In function `main':
display_image.cpp:(.text+0x9b): undefined reference to `cv::imread(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
display_image.cpp:(.text+0x13f): undefined reference to `cv::namedWindow(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
display_image.cpp:(.text+0x1a1): undefined reference to `cv::imshow(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&)'
collect2: ld returned 1 exit status
make: *** [display_image] Error 1

我发现了一些与我有关的问题,但是他们的解决方案都没有为我解决。我真的很想了解导致这些未定义引用的原因,因为所有的库和头文件都在它们的确切位置。应该是。

任何形式的帮助都会以我永恒的爱和感激来偿还。 H546163809H

添加一些命令的回声以澄清问题:

$pkg-config --cflags opencv
-I/usr/local/include/opencv -I/usr/local/include 
$pkg-config --libs opencv
/usr/local/lib/libopencv_calib3d.so /usr/local/lib/libopencv_contrib.so /usr/local/lib/libopencv_core.so /usr/local/lib/libopencv_features2d.so /usr/local/lib/libopencv_flann.so /usr/local/lib/libopencv_gpu.so /usr/local/lib/libopencv_highgui.so /usr/local/lib/libopencv_imgproc.so /usr/local/lib/libopencv_legacy.so /usr/local/lib/libopencv_ml.so /usr/local/lib/libopencv_nonfree.so /usr/local/lib/libopencv_objdetect.so /usr/local/lib/libopencv_photo.so /usr/local/lib/libopencv_stitching.so /usr/local/lib/libopencv_ts.so /usr/local/lib/libopencv_video.so /usr/local/lib/libopencv_videostab.so

现在添加更详细的编译输出(使用-Wall和-v)

g++ -v -Wall -o display_image display_image.cpp -I/usr/local/include -I/usr/local/include/opencv -L/usr/local/lib -lopencv_core -lopencv_highgui  
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.6/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.6.3-1ubuntu5' --with-bugurl=file:///usr/share/doc/gcc-4.6/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.6 --enable-shared --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.6 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --enable-plugin --enable-objc-gc --disable-werror --with-arch-32=i686 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5) 
COLLECT_GCC_OPTIONS='-v' '-Wall' '-o' 'display_image' '-I' '/usr/local/include' '-I' '/usr/local/include/opencv' '-L/usr/local/lib' '-shared-libgcc' '-mtune=generic' '-march=x86-64'
/usr/lib/gcc/x86_64-linux-gnu/4.6/cc1plus -quiet -v -I /usr/local/include -I /usr/local/include/opencv -imultilib . -imultiarch x86_64-linux-gnu -D_GNU_SOURCE display_image.cpp -quiet -dumpbase display_image.cpp -mtune=generic -march=x86-64 -auxbase display_image -Wall -version -fstack-protector -o /tmp/ccDB7q8G.s
GNU C++ (Ubuntu/Linaro 4.6.3-1ubuntu5) version 4.6.3 (x86_64-linux-gnu)
    compiled by GNU C version 4.6.3, GMP version 5.0.2, MPFR version 3.1.0-p3, MPC version 0.9
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
ignoring nonexistent directory "/usr/local/include/x86_64-linux-gnu"
ignoring nonexistent directory "/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../../x86_64-linux-gnu/include"
ignoring duplicate directory "/usr/local/include"
  as it is a non-system directory that duplicates a system directory
#include "..." search starts here:
#include <...> search starts here:
 /usr/local/include/opencv
 /usr/include/c++/4.6
 /usr/include/c++/4.6/x86_64-linux-gnu/.
 /usr/include/c++/4.6/backward
 /usr/lib/gcc/x86_64-linux-gnu/4.6/include
 /usr/local/include
 /usr/lib/gcc/x86_64-linux-gnu/4.6/include-fixed
 /usr/include/x86_64-linux-gnu
 /usr/include
End of search list.
GNU C++ (Ubuntu/Linaro 4.6.3-1ubuntu5) version 4.6.3 (x86_64-linux-gnu)
compiled by GNU C version 4.6.3, GMP version 5.0.2, MPFR version 3.1.0-p3, MPC version 0.9
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: 65b5171ac1bd7b3f07dbea6bdb24be3d
COLLECT_GCC_OPTIONS='-v' '-Wall' '-o' 'display_image' '-I' '/usr/local/include' '-I' '/usr/local/include/opencv' '-L/usr/local/lib' '-shared-libgcc' '-mtune=generic' '-march=x86-64'
 as --64 -o /tmp/cchdK0Uq.o /tmp/ccDB7q8G.s
COMPILER_PATH=/usr/lib/gcc/x86_64-linux-gnu/4.6/:/usr/lib/gcc/x86_64-linux-gnu/4.6/:/usr/lib/gcc/x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/4.6/:/usr/lib/gcc/x86_64-linux-gnu/
LIBRARY_PATH=/usr/lib/gcc/x86_64-linux-gnu/4.6/:/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../../lib/:/lib/x86_64-linux-gnu/:/lib/../lib/:/usr/lib/x86_64-linux-gnu/:/usr/lib/../lib/:/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-v' '-Wall' '-o' 'display_image' '-I' '/usr/local/include' '-I' '/usr/local/include/opencv' '-L/usr/local/lib' '-shared-libgcc' '-mtune=generic' '-march=x86-64'
 /usr/lib/gcc/x86_64-linux-gnu/4.6/collect2 --sysroot=/ --build-id --no-add-needed --as-needed --eh-frame-hdr -m elf_x86_64 --hash-style=gnu -dynamic-linker /lib64/ld-linux-x86-64.so.2 -z relro -o display_image /usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crt1.o /usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crti.o /usr/lib/gcc/x86_64-linux-gnu/4.6/crtbegin.o -L/usr/local/lib -L/usr/lib/gcc/x86_64-linux-gnu/4.6 -L/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu -L/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../../lib -L/lib/x86_64-linux-gnu -L/lib/../lib -L/usr/lib/x86_64-linux-gnu -L/usr/lib/../lib -L/usr/lib/gcc/x86_64-linux-gnu/4.6/../../.. /tmp/cchdK0Uq.o -lopencv_core -lopencv_highgui -lstdc++ -lm -lgcc_s -lgcc -lc -lgcc_s -lgcc /usr/lib/gcc/x86_64-linux-gnu/4.6/crtend.o /usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crtn.o
/tmp/cchdK0Uq.o: In function `main':
display_image.cpp:(.text+0x9b): undefined reference to `cv::imread(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
display_image.cpp:(.text+0x13f): undefined reference to `cv::namedWindow(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
display_image.cpp:(.text+0x1a1): undefined reference to `cv::imshow(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&)'
collect2: ld returned 1 exit status
make: *** [display_image] Error 1

2 个答案:

答案 0 :(得分:1)

您能指导我完成您的OpenCV安装过程吗?这意味着您的程序正在获取头文件,但没有获得opencv库。它可能是您的opencv .so文件未生成或它们不在系统路径中。如果文件生成正确,请检查构建文件夹。如果正确编译了OpenCV,请导出构建位置的路径,然后尝试编译。您可以通过回显pkg-config --cflags opencvpkg-config --libs opencv来检查您的系统是否能够找到opencv已安装的位置。 如果其中任何一个未打印适当的值,您可能必须导出该位置。 如果您错过了任何安装步骤,请检查here

答案 1 :(得分:1)

我的猜测是你遗漏了一些* -dev个包裹。本教程在开始时列出了大量的内容,根据我的经验,这些通常是编译失败的直接原因。 不幸的是,您可以从编译器获得间接消息。我想你提到的错误信息只是构建输出的尾部,这可能远远不是真正的原因。您需要做的是重定向构建输出(使用tee可以拆分它并在控制台上查看它并获得构建日志),或者您已经有了构建日志。研究构建日志并查看编译期间出现的第一个第二个错误消息。这可以更好地表明缺少的库或开发包。 这只是一个猜测,指南。为我们提供整个构建日志。如果输出很短,请尝试增加输出详细程度。而且,我可能完全错了。

我比较了由@Gaurav Raj和你的Ubuntu 12.04教程链接的Ubuntu 11.04教程。 我试着安装这些软件包:

  • 填实:libpng12-dev的
  • 的libpng ++ - dev的
  • libpng3之间
  • libpnglite-dev的
  • libpngwriter0-dev的
  • 的zlib1g-dbg的
  • 的zlib1g-dev的
  • pngtools
  • 中的libjpeg8-dev的
  • 中的libjpeg8-dbg的
  • 的libjpeg-progs的
  • 的ffmpeg
  • libgstreamer0.xy-0-dbg(xy应该是Ubuntu 12.04的版本,这可能是Ubuntu 11.04的0.10版本)
  • libgstreamer0.xy-dev的
  • libxine-dev的
  • libunicap2-dev的
  • 痛饮
  • libdc1394-utils的

在安装它们之前,您可以通过说dpkg -l | grep ${pkg_name_to_check}来检查它们是否已安装。