Build OpenCV Statically for Linux

This post describes how to build an static version of OpenCV for Linux operating systems. I am going to use OpenCV version 3.1.0 and Ubuntu 16.04 64-bit but the process should be more or less the same under CentOS or other flavors of Linux. So let’s start.



Following are the requirements for building OpenCV:

  • OpenCV 3.1.0 for Linux/Mac (can be found here)
  • CMake (just use Ubuntu Software to get it. I am going to use the CMake GUI to comfortably set all the options but if you feel more like a genius by typing some commands in the terminal then you can go ahead and type “sudo apt-get install cmake” and some similar commands to get it.)

Extract OpenCV archive and run CMake. Make sure you set the source and bin folders as seen below. (Of course you should use the folder paths from your own computer and PLEASE don’t use my name, please.) Just make sure build folder is under the source folder.

screenshot-from-2016-09-19-02-10-29


Now hit Configure button. You will be asked to approve creating the binaries folder as seen below. Just press Yes to continue.

screenshot-from-2016-09-19-02-10-43

Next you have to make sure Unix Makefiles are selected in the CMakeSetup dialog as seen here. Press Finish to proceed.

screenshot-from-2016-09-19-02-10-50

And Yes, now you have to wait for the following to appear. Be patient.

screenshot-from-2016-09-19-02-19-30

Go through the options in red and make sure to uncheck BUILD_SHARED_LIBS option.

Also change CMAKE_INSTALL_PREFIX to where you want to install your static libraries. I prefer my home folder so I set CMAKE_INSTALL_PREFIX option to “/home/amin/opencv”

After setting the above options make sure you press Configure once again. After it’s done, press Generate to create make files.

You can safely close CMake and forget it ever existed.



Now it’s time to start compiling OpenCV, so open up a terminal window and enter the following commands:

cd Downloads/opencv-3.1.0/build
sudo make

No need to repeat that you have to enter the same folder you did for binary folder in CMake instead of “Downloads/opencv-3.1.0/build” right?

screenshot-from-2016-09-19-02-23-39

Wait for a while until building is finished. Then enter one last command to finish it off.

sudo make install

Voila, now you have a static build of OpenCV 3.1.0 under the same folder you entered for CMAKE_INSTALL_PREFIX. Mine was “/home/amin/opencv” so if I check that folder you’ll see that I have all required files there.

screenshot-from-2016-09-19-10-33-16


9 Replies to “Build OpenCV Statically for Linux”

  1. Having read this I believed it was rather informative. I appreciate you finding the time and energy to put this information together. I once again find myself personally spending a significant amount of time both reading and commenting. But so what, it was still worthwhile!|

    1. Not sure what you’re looking for. This is a static build. You are not supposed to have any libs there. Do you get any error while building?

  2. Good job and thank you! 🙂

    Can you give a tutorial about how to use the opencv static libraries?

      1. I need to use the static libraries of the opencv as libs of a makefile project in eclipse.

        My makefile:

        INCDIR = ./include
        LIBDIR = ./lib
        SRCDIR = ./src
        OPENCVDIR = /home/spaf94/opencv_static/lib
        OPENCVINC = /home/spaf94/opencv_static/include

        CXXFLAGS = -O2 -g -Wall -fmessage-length=0 -I$(INCDIR) -I$(OPENCVINC) -L$(OPENCVDIR)

        VPATH = $(SRCDIR)
        OBJS = test20161105.o

        LIBS += $(OPENCVDIR)/libopencv_highgui.a $(OPENCVDIR)/libopencv_core.a

        TARGET = test20161105

        $(TARGET): $(OBJS)
        $(CXX) -o $(TARGET) $(OBJS) $(LIBS)

        all: $(TARGET)

        clean:
        rm -f $(OBJS) $(TARGET)

        With this, I create a simple cpp file to show an image:

        #include
        #include
        #include

        using namespace cv;

        int main(void)
        {
        Mat image;

        image = imread(“/home/spaf94/Imagens/opencv.png”, CV_LOAD_IMAGE_COLOR);

        return 0;
        }

        But when I compile, I got errors in the static libraries …
        “Indefinite reference to [functions name of the opencv]”

        1. I don’t have much experience with Eclipse but you should look for a method to add pkgconfig in Eclipse if you want the better option. If you still insist on manually entering all libs, then make sure you add them correctly. Here is an example of how I do it:

          LIBS += -L[OPENCV_LIBS_DIR] -lopencv_core -lopencv_imgproc -lopencv_imgcodecs -lopencv_videoio -lopencv_objdetect

          Obviously you should replace [OPENCV_LIBS_DIR] with the correct directory on your system.
          And one final note, don’t worry about the name of the library being “libopencv_core.a” and so on. “-L” and “-l” options are taking care of that.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.