I am learning OpenCV using Learning OpenCV book.
One problem I am facing while compiling the code is that I have to write a long command to compile and get the executabl
You can create a file called Makefile
in you working directory like this:
CFLAGS = `pkg-config --cflags opencv`
LIBS = `pkg-config --libs opencv`
% : %.cpp
g++ $(CFLAGS) $(LIBS) -o $@ $<
then you can use this file for all your single-file programms. Just call make
with the basename of the file you want to compile. For facedetect.cpp
that would be
make facedetect
Here some more details:
The general format of a makefile is like this:
target : dependecy1 dependenc2 ...
command that generates the target
So for your example you could write:
facedetect : facedetect.cpp
g++ $(CFLAGS) $(LIBS) -o facedetect facedetect.cpp
For each new example you can now create a new target. But you can also make it more general:
% : %.cpp
g++ $(CFLAGS) $(LIBS) -o $@ $<
Here %
matches any nonempty substring. The automatic variables $@
and $<
substitute the names of the target file and the source file.
For more information you can consult the make documentation.
GNU Make is pretty smart and the Makefile you need for this doesn't need to be as verbose as in the other answers given so far. Here is a simple Makefile which you can use to compile the OpenCV examples:
CPPFLAGS = $(shell pkg-config --cflags opencv)
LDLIBS = $(shell pkg-config --libs opencv)
That's it. The Makefile can be this short thanks to Make's implicit rules.
Then run make
as usual:
make facedetect
This assumes there is a facedetect.c
or facedetect.cpp
in the same directory.
I recommend the following (free!) book if you want to learn Make: http://oreilly.com/catalog/make3/book/index.csp
Create a file named makefile
in your working directory that contains the following:
CFLAGS = $SHELL(pkg-config --cflags opencv)
LIBS = $SHELL(pkg-config --libs opencv)
facedetect : facedetect.cpp
g++ $(CFLAGS) $(LIBS) -o $@ $<
Then when you want to compile you just type:
$ make
(To answer your PS - note that CMake
is very different from make
- for now you should probaby just use make
.)