How To Compile OpenCV based programs in Linux

How to compile C on Linux ?

  • 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/source/code
    PKG_CONFIG_PATH=/where/you/have/installed/opencv/lib/pkgconfig:${PKG_CONFIG_PATH}
    export PKG_CONFIG_PATH
    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/where/you/have/installed/opencv/lib -lcxcore -lcv -lhighgui -lcvaux  

  • 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 as below:
    gcc `pkg-config --cflags opencv` `pkg-config --libs opencv` -o my-opencv-prgm my-opencv-prgm.c
    Simpler way is as below:
    gcc `pkg-config --cflags --libs opencv` -o my-opencv-prgm my-opencv-prgm.c
    If those two fails, try this:
    gcc -I/where/you/have/installed/opencv -L/where/you/have/installed/opencv -lcv -lhighgui -lstdc++ \
        -o my-opencv-prgm my-opencv-prgm.c

How to compile OpenCV with some libraries not in standard path on Linux?

  • The solution is to use the CFLAGS, CPPFLAGS and LDFLAGS environment variables at configure time. For example, if you have ffmpeg library in one of 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

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

  • If an error occurs that 'a library cannot be found' during compilation 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).
    • run ldconfig as root.
    Or, add the location of the OpenCV libraries to your LD_LIBRARY_PATH (should work on most systems including Fedora)

    For additional help on FFMpeg compilation: http://freeshells.ch/~phoenix/ocv.htm

OpenCV OS

你可能感兴趣的:(How To Compile OpenCV based programs in Linux)