Reputation: 1290
I have recently installed OpenCV 2.4.1 on a desktop running Ubuntu 12.04(64-bits) following this tutorial,but every time I try to compile a code,even the included samples that use a pre-built compiling script,I get undefined references regarding every function from the cv
namespace.
The code I am currently trying to compile is this example from the OpenCV documentation.I have tried 3 different approaches to compile this code:
I- Using CMake,as instructed by the documentation site;
II- Using gcc and pkg-config with the following command:
gcc `pkg-config --cflags opencv` display_image.cpp `pkg-config --libs opencv` -o DisplayImage
III- Using my own makefile and adding the library and header paths directly:
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)
And all of them produce the same output:
/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
I have found some other questions that where related to mine,but none of their solutions worked out for me.And I would really like to understand what is causing these undefined references,since all the libraries and headers are exactly where they are supposed to be.
Any kind of help would be repaid with my eternal love and gratitude. H546163809H
Adding the echo of some commands to clarify the question:
$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
Adding now a more verbose compilation output (with -Wall and -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
Upvotes: 2
Views: 8968
Reputation: 10699
My guess is that you are missing some *-dev
packages. The tutorial lists a whole bunch of them right in the beginning, and in my experience usually those are the immediate causes of failed compilations.
Unfortunately you can get an indirect message from the compiler. I guess the error message what you mention is just the tail of the build output, which can be far form the real cause. What you have to do is redirect the build output (with tee
you can split it and see it on the console and get a build log too), or you already have a build log. Study that build log and look at the first or second error message which appears during the compilation. That can indicate the missing library or dev package better.
This is just a guess, guideline. Provide us the whole build log. If the output is that short, try to increase output verbosity. Also, I might be totally wrong.
I compared the Ubuntu 11.04 tutorial linked by @Gaurav Raj and your Ubuntu 12.04 tutorial. I'd try to install these packages:
Before installing them, you can check if they installed by saying dpkg -l | grep ${pkg_name_to_check}
.
Upvotes: 1
Reputation: 708
Can you guide me through your OpenCV installation process you have taken? It means that your program is getting the header files but not getting opencv libraries. It may be either your opencv .so files are not getting generated or they are not in system paths. Check your build folder if the files are generated properly. If OpenCV is compiled properly, export the path of the build location and then try compiling. You can check if your system is able to find opencv installed location by echoing pkg-config --cflags opencv
and pkg-config --libs opencv
.
If any of them doesn't print appropriate values, you might have to export the location.
Check here if you have missed any installation steps.
Upvotes: 1