3

I've just updated my system from ubuntu 11.04 to 11.10 and now I can't compile anymore any C program that contain references to OpenCV libraries

I've already tried to reinstall OpenCV (I use the 2.1 version) but I'm stuck with this error:

/tmp/ccArHTZL.o: In function `main':
z.c:(.text+0x59): undefined reference to `cvLoadImage'
z.c:(.text+0xa0): undefined reference to `cvNamedWindow'
z.c:(.text+0xb1): undefined reference to `cvShowImage'
z.c:(.text+0xbb): undefined reference to `cvWaitKey'
z.c:(.text+0xc5): undefined reference to `cvDestroyWindow'
z.c:(.text+0xd1): undefined reference to `cvReleaseImage'
collect2: ld returned 1 exit status

In order to install OpenCV I've always followed this procedure:

$ sudo apt-get install libcv2.1 libcv-dev libcvaux2.1 libcvaux-dev libhighgui2.1
     libhighgui-dev opencv-doc python-opencv

$ export LD_LIBRARY_PATH=/home/opencv/lib
$ export PKG_CONFIG_PATH=/home/opencv/lib/pkgconfig

$ pkg-config --cflags opencv
     -I/usr/include/opencv

$ pkg-config --libs opencv
     -lcxcore -lcv -lhighgui -lcvaux -lml

$ g++ -I/usr/include/opencv -lcxcore -lhighgui -lm hello.c

Anyone can help me?

karlphillip
  • 92,053
  • 36
  • 243
  • 426
Marco L.
  • 1,489
  • 4
  • 17
  • 25

5 Answers5

6

Why don't you use pkg-config to your favor?

g++ hello.c -o hello `pkg-config --cflags --libs opencv` 
karlphillip
  • 92,053
  • 36
  • 243
  • 426
  • 3
    You should always put the libraries at the end of the row : gcc test.c `pkg-config --cflags opencv` -o test `pkg-config opencv --libs` . Somehow this became important after I upgraded Ubuntu to 11.10. – Dimitar Slavchev Mar 06 '12 at 14:06
  • Guys, this advice, putting libraries to the END.. oh god, you saved me from another frustration! – Naomak Mar 13 '12 at 10:21
  • YEAH!!!!!!!! @DimitarSlavchev you just fixed nearly two weeks fo heartache, headache and pain! Thanks you! – Wes Miller Nov 19 '12 at 17:22
2

I think it is because of some changes from gcc 4.5 to gcc 4.6

Try this command instead (i.e., move the libraries to the end, instead of at the beginning of your command line) -- it works for me:

g++ -I/usr/include/opencv hello.c -lcxcore -lhighgui -lm

Jianxin Wu
  • 21
  • 1
0

I'm still on kubuntu 10.10 so I'm not really familiar how does 11.10 work, but the most common answer to problems with not finding libraries is to use ldconfig with sudo. It'll refresh libraries database. If that doesn't help, look into /usr/lib, /usr/lib64 and /usr/lib32, because its the default place where apt-get throws libraries in. When you find the libraries, change the LD_LIBRARY_PATH so it contains the directory. I don't think that /home/opencv/lib is where they are, but i don't know Your environment

morynicz
  • 2,322
  • 2
  • 20
  • 34
  • I've found all the libcv.so libraries in `/usr/lib`. Then I provided to change the path `export LD_LIBRARY_PATH=/usr/lib` but I continue to get the same error – Marco L. Oct 16 '11 at 12:46
  • Not a linker expert here, but have You tried to compile it without meddling with PKG_CONFIG and LD_LIBRARY_PATH? i belive that unless You've compiled some libraries from source the defaults are O.K. Another thing is that as I see You use g++ to compile a c program. There is a slight chance that this is the issue. You could also try if c++ api works. just to e sure that the problem lies on the linker-libraries-searching side not on the library itself. – morynicz Oct 16 '11 at 22:54
0

I just upgraded to 11.04 on my laptop and having similar issues. I would try building the latest version of OpenCV (2.3.1) and see if this fixes anything, this seemed to fix quite a few issues for me.

xamox
  • 2,599
  • 4
  • 27
  • 30
  • I previously tried the 2.3.1 but I encountered A LOT OF ISSUES that forced me to downgrade to the 2.1 version. So unfortunately for me the upgrade to 2.3.1 is not an option – Marco L. Oct 16 '11 at 20:26
0

Use the following command, it worked for me:

gcc pkg-config --cflags opencv opencv.c -o open_cv pkg-config --libs opencv

Dhruv Baldawa
  • 158
  • 1
  • 10