Build Error with CalcOpticalFlowFarneback Function -> OpenCV -> C Language – Eclipse -> MacOSX-Collection of common programming errors

“undefined symbol” means that you have not linked against the proper library, or you have, but the library is for the wrong architecture (e.g. a 32-bit library while your machine is 64-bit).

You need to check the linking flags and paths, and the library installation path.

If you can get the linker log in Eclipse, and/or the command line that got passed to the linker, you can repeat the linking command from a terminal and see if you get a more helpful message (such as “library not found”). Or you can try running the make command from the terminal and do the same.

Editing the Makefile, you might find that some commands are prepended with the “@” symbol, which prevents them from being output (their output is visible, the command itself is not). You can usually safely remove the @’s and get a more verbose output.

Another possibility is that there is a name mangling error, and the “_” prefix to the function should not be there. I am not too familiar with Eclipse though, so I can’t follow you there. But see e.g. http://www.eclipse.org/forums/index.php/m/783606/ ; you might have built OpenCV with the wrong options, so that now it is not compatible with your code. Which is strange, seeing as how you call other OpenCV functions without problems; but maybe you built OpenCV in two steps?

In general (this is not limited to OpenCV!) you will have a build command such as

gcc -I/usr/local/include/opencv -L/usr/local/lib main.c -o Cattura_foto2 \
-lopencv_core.2.4.3 \
-lopencv_imgproc.2.4.3 \
-lopencv_highgui.2.4.3 \
-lopencv_gpu.2.4.3

and an error complaining of an undefined symbol:

Undefined symbols for architecture x86_64: "_cvCalcOpticalFlowFarneback"

This means that you are linking with the libraries openvc_core.2.4.3 etc. and the linker cannot find the symbol “_cvCalcOpticalFlowFarneback”.

Locate those libraries on your computer, and run (from Terminal) the following command against all the libraries you found. Note: your path will probably be different. I have /usr/lib64.

for lib in /usr/lib64/libopencv_*; do 
    echo "Examining $lib..."
    strings $lib | grep -i cvCalcOpticalFlowFarneback
done

You ought to see something like:

Examining /usr/lib/libopencv_pippo_pluto...
Examining /usr/lib/libopencv_blah_blah...
...
Examining /usr/lib64/libopencv_video.so...
cvCalcOpticalFlowFarneback
cvCalcOpticalFlowFarneback
Examining /usr/lib64/libopencv_video.so.2.4...
cvCalcOpticalFlowFarneback
cvCalcOpticalFlowFarneback
Examining /usr/lib64/libopencv_video.so.2.4.3...
cvCalcOpticalFlowFarneback
cvCalcOpticalFlowFarneback

The reason for the multiple matches is that libraries exist in multiple symbolic copies.

To be sure, let’s inspect the library:

nm -D /usr/lib64/libopencv_video.so.2.4 | grep Farneback

0000000000027e50 T cvCalcOpticalFlowFarneback

Now we know two important things:

  1. The symbol is really called cvCalcOpticalFlowFarneback. Your GCC is complaining it doesn’t find the symbol _cvCalcOpticalFlowFarneback, with an underscore. You either built the library incorrectly or you are missing a -fno-leading-underscore option to GCC.

But if that were the case, no OpenCV2 symbols at all would be recognized. So, I’m betting on #2:

  1. The symbol is in libopencv_video.so.2.4.3, and there is no reference to that library in your build command! You ought to have -lopencv_video.2.4.3 or something like that. Verify your makefile and options.

Let me know how it goes – and in bocca al lupo.

Originally posted 2013-11-06 03:08:22.