OpenCV MinGW Makefile

OpenCV MinGW Makefile

Its a bit interesting that so many of my tutorials have to do with setting something up on Windows because I much prefer to develop on Linux and actively avoid working on Windows whenever possible. Anyways this tutorial is a follow up to something my supervisor asked me to try and do ages ago which was to setup minGW and OpenCV on Windows for his course ELEC 474. I originally thought he wanted it to be command line based and I tried for a while to try and make this happen but was not successful - turns out he was fine with using a minimal IDE so we went with Code::Blocks for the course. Fast forward to now and it seems my software engineering skills have improved because for a different project I was able to get command line compiling with OpenCV and minGW working no problem.

So without further ado here is a simple Makefile for minGW and OpenCV:

CC = g++
CFLAGS = -g -Wall

SRCS = HelloWorld.cpp
PROG = HelloWorld.exe

OPENCV = -I"C:\opencv\build\include" -L"C:\opencv\build\x86\mingw\lib" -lopencv_core243 -lopencv_highgui243

$(PROG) : $(SRCS)
	$(CC) $(CFLAGS) -o $(PROG) $(SRCS) $(OPENCV)

Here is a breakdown of what each part is:

SRCS is the source files of your program

PROG is the name of your compiled program

OPENCV this line lets us use OpenCV in our program. The -I tells the compiler where to look for include files while the -L tells it where to look for the libraries when linking. I haven’t linked all the OpenCV libs in this example but it is easy to add more. For example to add the imgproc module simply add -lopencv_imgproc243 to the list.

To build and run code you’ll also need to make sure that C:/minGW/bin and C:\opencv\build\x86\mingw\lib are added to your system path. To add a variable to your system path navigate to Control Panel -> System -> Advanced System Settings -> Environment Variables (you can see my other tutorial for visuals). Click add new variable and name it mingw then paste the proper value. You can then add to the path by editing your path variable and adding a new entry. Type a semicolon after the last entry and then type %mingw%. The percent signs signal a variable and will add the variable named mingw you created to the path. I like to use variables like this when possible because it makes it easier to check and change things down the line.

Finally all you need to do to build your program is in the command prompt in the proper directory run mingw32-make and ta da up and running.

Maybe this is trivial for experienced windows developers but for me it wasn’t all that clear at the outset, makes good sense now though! Hopefully you find this useful.