6

As part of a bigger project I am working on, I need to use the OpenCV library on a C program. I installed OpenCV and opencv-devel using yum in Fedora 17 32-bit. I instructed the preprocessor to import opencv/cv.h and opencv/highgui.h, as the necessary header files.

As I've mentioned, gcc is used to compile the whole C project. However, there seems to be a problem with the linker.

Whenever I try to compile the project (gcc opencv.c -o opencv), I get a list of errors, similar to:

/tmp/ccLJWE0c.o: In function `cvRound': opencv.c:(.text+0x19):
undefined reference to `lrint' /tmp/ccLJWE0c.o: In function
`cvDecRefData': opencv.c:(.text+0xa5c): undefined reference to
`cvFree_' opencv.c:(.text+0xacd): undefined reference to `cvFree_'
/tmp/ccLJWE0c.o: In function `cvGetRow': opencv.c:(.text+0xbc3):
undefined reference to `cvGetRows' /tmp/ccLJWE0c.o: In function
`cvGetCol': opencv.c:(.text+0xbee): undefined reference to `cvGetCols'
/tmp/ccLJWE0c.o: In function `cvReleaseMatND': opencv.c:(.text+0xc01):
undefined reference to `cvReleaseMat' /tmp/ccLJWE0c.o: In function
`cvSubS': opencv.c:(.text+0xd21): undefined reference to `cvAddS'
/tmp/ccLJWE0c.o: In function `cvCloneSeq': opencv.c:(.text+0xd6f):
undefined reference to `cvSeqSlice' /tmp/ccLJWE0c.o: In function
`cvSetNew': opencv.c:(.text+0xdce): undefined reference to `cvSetAdd'
/tmp/ccLJWE0c.o: In function `cvGetSetElem': opencv.c:(.text+0xe61):
undefined reference to `cvGetSeqElem' /tmp/ccLJWE0c.o: In function
`cvEllipseBox': opencv.c:(.text+0xf61): undefined reference to
`cvEllipse' /tmp/ccLJWE0c.o: In function `cvFont':
opencv.c:(.text+0xfb1): undefined reference to `cvInitFont'
/tmp/ccLJWE0c.o: In function `cvReadIntByName':
opencv.c:(.text+0x103f): undefined reference to `cvGetFileNodeByName'
/tmp/ccLJWE0c.o: In function `cvReadRealByName':
opencv.c:(.text+0x10d0): undefined reference to `cvGetFileNodeByName'
/tmp/ccLJWE0c.o: In function `cvReadStringByName':
opencv.c:(.text+0x112a): undefined reference to `cvGetFileNodeByName'
/tmp/ccLJWE0c.o: In function `cvReadByName': opencv.c:(.text+0x115a):
undefined reference to `cvGetFileNodeByName' opencv.c:(.text+0x1170):
undefined reference to `cvRead' /tmp/ccLJWE0c.o: In function
`cvCreateSubdivDelaunay2D': opencv.c:(.text+0x11a3): undefined
reference to `cvCreateSubdiv2D' opencv.c:(.text+0x11cd): undefined
reference to `cvInitSubdivDelaunay2D' /tmp/ccLJWE0c.o: In function
`cvContourPerimeter': opencv.c:(.text+0x1307): undefined reference to
`cvArcLength' /tmp/ccLJWE0c.o: In function `cvCalcHist':
opencv.c:(.text+0x132f): undefined reference to `cvCalcArrHist'
/tmp/ccLJWE0c.o: In function `main': opencv.c:(.text+0x14cd):
undefined reference to `cvCreateImage' opencv.c:(.text+0x1510):
undefined reference to `cvGet2D' opencv.c:(.text+0x159e): undefined
reference to `cvSet2D' opencv.c:(.text+0x15df): undefined reference to
`cvSaveImage' collect2: error: ld returned 1 exit status

Also, when I compile the program using:

gcc opencv.c -o opencv `pkg-config --libs --cflags opencv` -ldl

I still get:

/usr/bin/ld: /tmp/ccMRviO3.o: undefined reference to symbol 'lrint@@GLIBC_2.1'
/usr/bin/ld: note: 'lrint@@GLIBC_2.1' is defined in DSO /lib/libm.so.6 so try adding it to the linker command line
/lib/libm.so.6: could not read symbols: Invalid operation
collect2: error: ld returned 1 exit status

I've been trying to find a solution, but nothing seems to fix the problem. In the OpenCV Documentation, they mention a different way of installing the libraries needed, but I don't really understand the steps I have to follow. I thought that Fedora devel Packages where compiled and ready to use. Anyway, if this is the problem, is there an easy way to make the whole thing work?

I've been coding in Visual Basic for 6 years now, but I've just started learning C as part of my University Education; thus I am not very experienced in manipulating GCC. :( I would ask you to be as explanatory as possible! :)

Any help is appreciated! Thanks in Advance!!! :D

sinelaw
  • 16,205
  • 3
  • 49
  • 80
someone
  • 361
  • 2
  • 3
  • 13

1 Answers1

7

Try adding -lm, to include the math library that provides lrint (see here)

sinelaw
  • 16,205
  • 3
  • 49
  • 80
  • Thanks! You are absolutely correct!!! :D It doesn't throw an Error anymore. Yet I have to ask: What is this "MAGIC" -lm Argument? In the Link you attached, it is being used to link the math Library... How do we use those arguments and why? – someone Feb 03 '13 at 19:06
  • `-lm` tells the linker to link with the `math` library. If you want to learn more I recommend you read about linkers in general, such as [this](http://www.lurklurk.org/linkers/linkers.html) and [that](http://www.yolinux.com/TUTORIALS/LibraryArchives-StaticAndDynamic.html) tutorials which came up after a quick search for "c linker tutorial" – sinelaw Feb 03 '13 at 19:09
  • Sorry for exploiting your kindness, but can I ask a last question? ;) What does the 'pkg-config and its arguments' part of the gcc command line do? – someone Feb 03 '13 at 21:56
  • Try running `pkg-config --libs --cflags opencv` from the command line - you'll see that it prints a list of command line arguments for the various include paths (see [-I invocation option](http://gcc.gnu.org/onlinedocs/cpp/Invocation.html#Invocation)) and [linker commands](http://gcc.gnu.org/onlinedocs/gcc/Link-Options.html) that are appropriate for compiling a program that depends on `opencv`. You can read more about [pkg-config here](http://www.freedesktop.org/wiki/Software/pkg-config) and [backticks command substitution here](http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_03_04.html) – sinelaw Feb 04 '13 at 00:33
  • That does not seem to help; adding `-lm` does not change anything for me about the undefined reference to `lrint`, even though the rest of the error message (save for the name of the file being compiled) is the same. – O. R. Mapper May 20 '13 at 09:56
  • 1
    Ok, [this forum posting](http://tech.groups.yahoo.com/group/OpenCV/message/85129) helped me solve that issue; it seems that in the current `gcc` version, all the flags and options have to be added *after* the input and output files. I will add this information to [my original SU question](http://superuser.com/questions/585809/tool-for-automatically-blurring-people-in-photos/594239). – O. R. Mapper May 20 '13 at 10:08
  • This approach seems not to fix the similar issue for OpenCV 3.1.0. I tried "gcc MY_FILE.c -o MY_BUILD 'pkg-config --libs --cflags opencv' -lm" but still get "In function cvPointFrom32f: undefined reference to cvRound". Is there any suggestion or do I misunderstand anything? Thank you for taking time on my question. – willSapgreen Apr 09 '16 at 00:14