Ask Your Question

Why do we need to compile opencv with cMake

asked 2018-11-24 01:49:53 -0500

Joeraid gravatar image

updated 2018-11-24 02:04:02 -0500

I just got an internship and I was tasked to generate dll files from c++ class and function definitions that call to the opencv library. At first, I just wanted to follow the instructions to install opencv on windows and move on since I thought the task itself is pretty straightforward, but then I got hit with some serious looking error messages when I tried to "compile" c++ opencv (step 6.1 in this tutorial). Python opencv is just a bunch of shell commands away, but the c++ library is a nightmare especially that it involves working with visual studio which, on its own, is pain to get to work properly. I want to be able to debug the errors from the compiling phase and for that I need to know why the special treatment for the c++ library compared to the python one? Why do I need to compile opencv and why does it need to be done with cmake? Does pip install opencv-contrib-python also compile opencv behind the scene? Is that systematic for all libraries? I followed this tutorial but it teaches how to install opencv per project, so is that it? Does compiling allow you to add opencv globally?

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted

answered 2018-11-26 04:53:47 -0500

kbarni gravatar image

CMake is not for compiling OpenCV, it's just a configuration utility. It generates the project files (SLN for Visual Studio or Makefile for make/gcc) according to your needs (you can select the modules you need), the installed libraries (e.g. video codecs) and the available hardware in your machine (e.g. CUDA support).

The compilation is handled by the Visual Studio compiler called cl.

Unfortunately I agree that external libraries in Visual Studio and compiling big projects is a real PITA.

Downloading the OpenCV library for windows from the site will give you a library that's ready to be employed. Just make sure that the include folders and the library (opencv_worldXXX.lib) are added to your Visual Studio project, and the OpenCV DLL file (opencv_worldXXX.dll) is accessible by the executable. This is valid for OpenCV library, no matter if you built it yourself or downloaded it.

Using the Python library (with pip install...) will get you a precompiled library too.

If you need to customize OpenCV, then you have to recompile it. Normally you won't get errors while compiling, so you don't need to debug. The errors might be caused by missing dependencies, so be sure that you follow every step of the tutorial.

edit flag offensive delete link more


So why doesn't the opencv team provide a precompiled download for the c++ library as well? Is it the extra configurations that you can introduce with cMake? The python library doesn't allow for that. Why is it different for python? Sorry for too many questions, but I've been trying to install opencv for 5 days now.

Joeraid gravatar imageJoeraid ( 2018-11-26 05:43:10 -0500 )edit

Here you can find the precompiled OpenCV for Windows.

So you can either use a prebuilt OpenCV or a library built by you from the sources.

Using a prebuilt library (Python or C++) you'll have to stick with the default build options.

CMake is used to configure the build process. It provides literally hundreds of switches you can activate to configure OpenCV for your needs. Of course you don't have to use all of them... If you build the library, it can create a C++, Python and Matlab compatible library too - and all of them with your build options.

Hope this explains your questions.

kbarni gravatar imagekbarni ( 2018-11-26 06:53:32 -0500 )edit

answered 2018-11-26 17:24:55 -0500

need to compile opencv and why does it need to be done with cmake? Does pip install opencv-contrib-python also compile opencv behind the scene?

edit flag offensive delete link more


I didn't see this until now. I will follow up on kbarni's already great answer for anyone who might still be confused with this. Cmake creates a project (a VS project aka the folder with the .sln file) and allows you to customise the files that you're gonna generate during compiling (compiling means generating files that represents your code in this case the code is already written by opencv contributors. what is only needed next is the user of such library to decide on what or what not to install because you can install everything (and wind up with a 7 gigs folder) or pick and choose. Pre-compiled opencv from the download site is the same as what you would have created but with less files generated because they probably figured it'll be a pain to download 7 gigs.

Joeraid gravatar imageJoeraid ( 2019-01-02 18:56:31 -0500 )edit

After compilation, you're gonna receive .lib files that you will have to include in every project that will call to stuff defined in opencv like classes and functions just like how you do import in python but dirtier. Hope this helps anyone who's new to this. Thank @kbarni again for your help.

Joeraid gravatar imageJoeraid ( 2019-01-02 18:58:51 -0500 )edit
Login/Signup to Answer

Question Tools


Asked: 2018-11-24 01:49:53 -0500

Seen: 110 times

Last updated: Nov 26 '18