I have successfully installed OpenCV 2.3.1 on Raspbian image and I would like to build/run some of the C/C++ programs located under the ~/OpenCV-2.3.1/samples directory.

For example, if I wanted to create an executable for ~/OpenCV-2.3.1/samples/cpp/kmeans.cpp how do I do this?

If I do g++ kmeans.cpp I get a whole bunch of compiler errors saying "undefined reference" to OpenCV functions. So obviously I need to pass it in libraries, but I'm not sure how?


You should use the build system provided by OpenCV and enable EXAMPLES compilation. By just issuing the g++ kmeans.cpp command you are not linking against any OpenCV library, that's why the compiler complaints about the undefined references (which are defined in the library files).

You may find this tutorial handy: it will guide you through the process of building the OpenCV library on a RPi and tell you how to enable examples compilation by setting BUILD_EXAMPLES=ON.

| improve this answer | |
  • thanks. Also, just wanted to point out that running the following command generated a binary for me: g++ convexhull.cpp -o convexhull -I /usr/local/include/opencv -L /usr/local/lib -lm -lopencv_core -lopencv_highgui -lopencv_imgproc – user1068636 Sep 28 '12 at 3:25
  • Adding -D BUILD_EXAMPLES=ON to cmake helped me build the examples on OS X too. Thanks! – Joseph Sheedy May 4 '16 at 18:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.