gpt4 book ai didi

c++ - OpenCV 2.4.1 undefined reference

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:50:46 29 4
gpt4 key购买 nike

我最近在运行 Ubuntu 12.04(64 位)的桌面上按照 this tutorial 安装了 OpenCV 2.4.1。 ,但每次我尝试编译代码时,即使包含使用预构建编译脚本的示例,我也会从 cv 命名空间中获得关于每个函数的 undefined reference 。

我目前尝试编译的代码是 this example来自 OpenCV 文档。我尝试了 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

我发现了一些与我相关的其他问题,但他们的解决方案都不适合我。我真的很想了解是什么导致了这些 undefined reference ,因为所有库和 header 都在它们所在的位置应该是。

我将以永恒的爱和感激返回任何形式的帮助。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

最佳答案

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

我比较了@Gaurav Raj 链接的 Ubuntu 11.04 教程和您的 Ubuntu 12.04 教程。我会尝试安装这些包:

  • libpng12-dev
  • libpng++-dev
  • libpng3
  • libpnglite-dev
  • libpngwriter0-dev
  • zlib1g-dbg
  • zlib1g-dev
  • png工具
  • 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}.

关于c++ - OpenCV 2.4.1 undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16202156/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com