Thursday, 3 October 2013

C++ linking error with Linux 3.x

If you are linking your C/C++ program on Linux 3.x (e.g., Ubuntu 12.04), you may need to put the libraries at the end of the shell command, i.e.,
$(CC) -o $@ $+ $(OPT) $(LIBDIRS) $(LIBS)
(This is like if you use Makefile, same rule applies if you don't use Makefile)

For my example with OpenCV, here
INCLUDES = -I/usr/include/opencv
LIBS = -lcxcore -lcv -lcvaux -lhighgui -lml
LIBDIRS = -L/usr/lib
OPT = -O3 -Wno-deprecated
CC=g++
If you put $(LIBDIRS) and $(LIBS) earlier in this command, it would successfully link with earlier distros (I suppose those with Linux kernel version 2.x, such as Ubuntu 10.04), but not on the newer distros. As a result, you would get the following error message:
*****.cpp:(.text+0x2db3): undefined reference to `cvLoadImage'
*****.cpp:(.text+0x2ef7): undefined reference to `cvCreateImage'
*****.cpp:(.text+0x2feb): undefined reference to `cvCreateImage'
*****.cpp:(.text+0x3041): undefined reference to `cvConvertScale'
*****.cpp:(.text+0x3088): undefined reference to `cvSaveImage'
*****.cpp:(.text+0x3099): undefined reference to `cvReleaseImage'
*****.cpp:(.text+0x30ca): undefined reference to `cvReleaseImage'
*****.cpp:(.text+0x30f9): undefined reference to `cvReleaseImage'
That is, every function call in the libraries will end up with "undefined reference". Pretty tricky! I would appreciate if anyone points to me the stuff under the bonnet here.

No comments:

Post a Comment