2006-08-21

How To Compile OpenCV in Linux

Under Linux, how to compile OpenCV with some libraries not in standard path ?

  • The solution is to use the CFLAGS, CPPFLAGS and LDFLAGS environment variables at configure time. For example, if you have ffmpeg library in one you your own directories, you can do (all on one command line):

  • ./configure CFLAGS=-I/where/is/ffmpeg/include CPPFLAGS=-I/where/is/ffmpeg/include LDFLAGS=-L/where/is/ffmpeg/lib

How to compile C samples under Linux ?

  • If OpenCV is installed in a standard path, you can quickly compile all the C samples with:

  • cd /where/you/have/the/c/samples

  • sh ./build_all.sh

    If OpenCV is not installed in a standard path, you need to setup the PKG_CONFIG_PATH variable. For example (assuming you are using a sh-based shell, like bash or zsh):

  • cd /where/you/have/the/c/samples

  • PKG_CONFIG_PATH=/where/you/have/installed/opencv/lib/pkgconfig:${PKG_CONFIG_PATH}

  • export PKG_CONFIG_PATH

  • sh ./build_all.sh

    You can check that the PKG_CONFIG_PATH is correct by doing either:

  • pkg-config --cflags opencv

  • pkg-config --libs opencv

    You must have something like:

  • $ pkg-config --cflags opencv

  • -I/where/you/have/installed/opencv/include/opencv

  • $ pkg-config --libs opencv

  • -L/com/softs/opencv-cvs/cvs-dev-i686/lib -lcxcore -lcv -lhighgui -lcvaux

How can I compile and link some OpenCV based program under Linux ?

  • The best way is to use pkg-config. Just define the correct PKG_CONFIG_PATH:

  • PKG_CONFIG_PATH=/where/you/have/installed/opencv/lib/pkgconfig:${PKG_CONFIG_PATH}

  • export PKG_CONFIG_PATH

    And then, compile your program like:

  • gcc `pkg-config --cflags opencv` `pkg-config --libs opencv` -o my-opencv-prgm my-opencv-prgm.c

    or simply:

  • gcc `pkg-config --cflags --libs opencv` -o my-opencv-prgm my-opencv-prgm.c

  • if that fails try:

  • gcc -I/home/intel/opencv/include -L/home/intel/opencv/lib -lopencv -lhighgui -lstdc++ opencv0.c -o opencv0

What if I get an error about OpenCV libraries when running a program?

If, after following the instructions above, your program compiles, but gives an error message that a library cannot be found when it is run, on Fedora systems:

  • create a file called opencv.conf in /etc/ld.so.conf.d/ which contains the path to your opencv libraries (by default /usr/local/lib).

  • become root and run ldconfig.

Or, add the location of the OpenCV libraries to your LD_LIBRARY_PATH (should work on most systems including Fedora)

没有评论:

发表评论