0

If I try to compile a small opencv test program under Ubuntu the linker can't resolve the symbols. The programm is:

root@lynx:/root(13)# cat ~rose/Txt/projects/OpenCV/Examples/cv_version.c
#include "cv.h"
#include "highgui.h"
#include "opencv2/core/version.hpp"

#include <stdio.h>

int main( int argc, char** argv ) {
    fprintf(stderr,"CV_MAJOR_VERSION=[%%d]%d    CV_MINOR_VERSION=[%%d]%d\n",
                                    CV_MAJOR_VERSION,CV_MINOR_VERSION);

#if defined CV_MAJOR_VERSION == 3
    fprintf(stderr,"CV_MAJOR_VERSION=3 is defined\n");
#endif

#ifdef CV_MAJOR_VERSION
    fprintf(stderr,"CV_MAJOR_VERSION is defined: %d\n",CV_MAJOR_VERSION);
#endif

#if CV_MAJOR_VERSION == 3
    fprintf(stderr,"CV_MAJOR_VERSION=3  (if CV_MAJOR_VERSION == 3)\n");
#endif

    return 0;
}

The output of gcc is:

rose@lynx-ubuntu:/home/rose/Txt/projects/OpenCV/Examples(32)$ gcc -v `pkg-config --cflags opencv` `pkg-config --libs opencv` -lm -ocv_version   cv_version.c 
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.8/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 4.8.4-2ubuntu1~14.04' --with-bugurl=file:///usr/share/doc/gcc-4.8/README.Bugs --enable-languages=c,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.8 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.8 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --disable-libmudflap --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-4.8-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-4.8-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-4.8-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --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.8.4 (Ubuntu 4.8.4-2ubuntu1~14.04) 
COLLECT_GCC_OPTIONS='-v' '-I' '/usr/include/opencv' '-o' 'cv_version' '-mtune=generic' '-march=x86-64'
 /usr/lib/gcc/x86_64-linux-gnu/4.8/cc1 -quiet -v -I /usr/include/opencv -imultiarch x86_64-linux-gnu cv_version.c -quiet -dumpbase cv_version.c -mtune=generic -march=x86-64 -auxbase cv_version -version -fstack-protector -Wformat -Wformat-security -o /tmp/cc1aYSh7.s
GNU C (Ubuntu 4.8.4-2ubuntu1~14.04) version 4.8.4 (x86_64-linux-gnu)
        compiled by GNU C version 4.8.4, GMP version 5.1.3, MPFR version 3.1.2-p3, MPC version 1.0.1
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.8/../../../../x86_64-linux-gnu/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/include/opencv
 /usr/lib/gcc/x86_64-linux-gnu/4.8/include
 /usr/local/include
 /usr/lib/gcc/x86_64-linux-gnu/4.8/include-fixed
 /usr/include/x86_64-linux-gnu
 /usr/include
End of search list.
GNU C (Ubuntu 4.8.4-2ubuntu1~14.04) version 4.8.4 (x86_64-linux-gnu)
        compiled by GNU C version 4.8.4, GMP version 5.1.3, MPFR version 3.1.2-p3, MPC version 1.0.1
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: 8e5167dd5049e4eb768f2a6a3a56b723
COLLECT_GCC_OPTIONS='-v' '-I' '/usr/include/opencv' '-o' 'cv_version' '-mtune=generic' '-march=x86-64'
 as -v -I /usr/include/opencv --64 -o /tmp/ccz4qy9b.o /tmp/cc1aYSh7.s
GNU assembler version 2.24 (x86_64-linux-gnu) using BFD version (GNU Binutils for Ubuntu) 2.24
COMPILER_PATH=/usr/lib/gcc/x86_64-linux-gnu/4.8/:/usr/lib/gcc/x86_64-linux-gnu/4.8/:/usr/lib/gcc/x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/4.8/:/usr/lib/gcc/x86_64-linux-gnu/
LIBRARY_PATH=/usr/lib/gcc/x86_64-linux-gnu/4.8/:/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../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.8/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-v' '-I' '/usr/include/opencv' '-o' 'cv_version' '-mtune=generic' '-march=x86-64'
 /usr/lib/gcc/x86_64-linux-gnu/4.8/collect2 --sysroot=/ --build-id --eh-frame-hdr -m elf_x86_64 --hash-style=gnu --as-needed -dynamic-linker /lib64/ld-linux-x86-64.so.2 -z relro -o cv_version /usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crt1.o /usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crti.o /usr/lib/gcc/x86_64-linux-gnu/4.8/crtbegin.o -L/usr/lib/gcc/x86_64-linux-gnu/4.8 -L/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu -L/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../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.8/../../.. /usr/lib/x86_64-linux-gnu/libopencv_calib3d.so /usr/lib/x86_64-linux-gnu/libopencv_contrib.so /usr/lib/x86_64-linux-gnu/libopencv_core.so /usr/lib/x86_64-linux-gnu/libopencv_features2d.so /usr/lib/x86_64-linux-gnu/libopencv_flann.so /usr/lib/x86_64-linux-gnu/libopencv_gpu.so /usr/lib/x86_64-linux-gnu/libopencv_highgui.so /usr/lib/x86_64-linux-gnu/libopencv_imgproc.so /usr/lib/x86_64-linux-gnu/libopencv_legacy.so /usr/lib/x86_64-linux-gnu/libopencv_ml.so /usr/lib/x86_64-linux-gnu/libopencv_objdetect.so /usr/lib/x86_64-linux-gnu/libopencv_ocl.so /usr/lib/x86_64-linux-gnu/libopencv_photo.so /usr/lib/x86_64-linux-gnu/libopencv_stitching.so /usr/lib/x86_64-linux-gnu/libopencv_superres.so /usr/lib/x86_64-linux-gnu/libopencv_ts.so /usr/lib/x86_64-linux-gnu/libopencv_video.so /usr/lib/x86_64-linux-gnu/libopencv_videostab.so -lopencv_calib3d -lopencv_contrib -lopencv_core -lopencv_features2d -lopencv_flann -lopencv_gpu -lopencv_highgui -lopencv_imgproc -lopencv_legacy -lopencv_ml -lopencv_objdetect -lopencv_ocl -lopencv_photo -lopencv_stitching -lopencv_superres -lopencv_ts -lopencv_video -lopencv_videostab -lm /tmp/ccz4qy9b.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/lib/gcc/x86_64-linux-gnu/4.8/crtend.o /usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crtn.o
/tmp/ccz4qy9b.o: In function `cvDecRefData':
cv_version.c:(.text+0xa6a): undefined reference to `cvFree_'
cv_version.c:(.text+0xaf2): undefined reference to `cvFree_'
/tmp/ccz4qy9b.o: In function `cvGetRow':
cv_version.c:(.text+0xc08): undefined reference to `cvGetRows'
/tmp/ccz4qy9b.o: In function `cvGetCol':
cv_version.c:(.text+0xc36): undefined reference to `cvGetCols'
/tmp/ccz4qy9b.o: In function `cvReleaseMatND':
cv_version.c:(.text+0xc50): undefined reference to `cvReleaseMat'
/tmp/ccz4qy9b.o: In function `cvSubS':
cv_version.c:(.text+0xdac): undefined reference to `cvAddS'
/tmp/ccz4qy9b.o: In function `cvCloneSeq':
cv_version.c:(.text+0xde5): undefined reference to `cvSeqSlice'
/tmp/ccz4qy9b.o: In function `cvSetNew':
cv_version.c:(.text+0xe52): undefined reference to `cvSetAdd'
/tmp/ccz4qy9b.o: In function `cvGetSetElem':
cv_version.c:(.text+0xefa): undefined reference to `cvGetSeqElem'
/tmp/ccz4qy9b.o: In function `cvEllipseBox':
cv_version.c:(.text+0xfe6): undefined reference to `cvEllipse'
/tmp/ccz4qy9b.o: In function `cvFont':
cv_version.c:(.text+0x1030): undefined reference to `cvInitFont'
/tmp/ccz4qy9b.o: In function `cvReadIntByName':
cv_version.c:(.text+0x1126): undefined reference to `cvGetFileNodeByName'
/tmp/ccz4qy9b.o: In function `cvReadRealByName':
cv_version.c:(.text+0x11d0): undefined reference to `cvGetFileNodeByName'
/tmp/ccz4qy9b.o: In function `cvReadStringByName':
cv_version.c:(.text+0x1263): undefined reference to `cvGetFileNodeByName'
/tmp/ccz4qy9b.o: In function `cvReadByName':
cv_version.c:(.text+0x12a3): undefined reference to `cvGetFileNodeByName'
cv_version.c:(.text+0x12b9): undefined reference to `cvRead'
/tmp/ccz4qy9b.o: In function `cvContourPerimeter':
cv_version.c:(.text+0x12ed): undefined reference to `cvArcLength'
/tmp/ccz4qy9b.o: In function `cvCalcHist':
cv_version.c:(.text+0x132f): undefined reference to `cvCalcArrHist'
collect2: error: ld returned 1 exit status


rose@lynx-ubuntu:/home/rose/Txt/projects/OpenCV/Examples(33)$ cat /etc/os-release 
NAME="Ubuntu"
VERSION="14.04.3 LTS, Trusty Tahr"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 14.04.3 LTS"
VERSION_ID="14.04"
HOME_URL="http://www.ubuntu.com/"
SUPPORT_URL="http://help.ubuntu.com/"
BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"

1 Answer 1

0

No, it's not broken: you just need to pay attention to the library link order. In particular, since objects in your cv_version.c translation unit depend on objects in the OpenCV library, it must come ahead of the pkg-config --libs opencv on your command line, e.g.

gcc -v `pkg-config --cflags opencv` cv_version.c `pkg-config --libs opencv` -lm -ocv_version

The gcc manual page Options for Linking mentsions this explicitly:

-llibrary
-l library

Search the library named library when linking. (The second alternative with the library as a separate argument is only for POSIX compliance and is not recommended.)

It makes a difference where in the command you write this option; the linker searches and processes libraries and object files in the order they are specified. Thus, ‘foo.o -lz bar.o’ searches library ‘z’ after file foo.o but before bar.o. If bar.o refers to functions in ‘z’, those functions may not be loaded.

2
  • Thank you very much for the hint. I really did not expect this solution because my command line worked perfectly under Gentoo and Debian.
    – user49472
    Sep 22, 2015 at 7:34
  • AFAIK the link order requirement is a feature of all recent gcc/ld versions - it shouldn't matter what Linux distribution you are using. If my answer worked for you, then please consider accepting it. Sep 22, 2015 at 11:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .