How can I create a simple Makefile using the below command?
g++ -Wall -I/usr/include/opencv -I/usr/include/opencv2 -L/usr/lib/ -g -o exe sourc1.cpp sourc2.c
Maybe something like
# your Makefile
#### variables
RM= rm -vf
CXX= g++
CXXFLAGS= -Wall -g
CPPFLAGS= -I/usr/include/opencv -I/usr/include/opencv2
LDLIBS= -lopencv_core -lopencv_imgproc -lopencv_highgui \
-lopencv_ml -lopencv_video -lopencv_features2d \
-lopencv_calib3d -lopencv_objdetect -lopencv_contrib \
-lopencv_legacy -lv4l1 -lv4l2 -lv4lconvert
SOURCEFILES= sourc1.cpp sourc2.cpp sourc3.cpp
OBJECTFILES= $(patsubst %.cpp,%.o,$(SOURCEFILES))
PROGNAME= yourexe
### rules
.PHONY: all clean
all: $(PROGNAME)
$(PROGNAME): $(OBJECTFILES)
$(LINK.cpp) $^ $(LOADLIBES) $(LDLIBS) -o $@
clean:
$(RM) $(OBJECTFILES) $(PROGNAME)
Feel free to adapt. (You probably have header files, and you need to add dependencies of object files to source and to header files). Perhaps use pkg-config if it knows about opencv. Replace the initial many spaces in the rules with a tab. Read carefully GNU make documentation.
If you have sourc2.cpp
and if you have two header files header1.hh
and header2.hh
which are #include
-d by sourc2.cpp
you'll need to add the dependency rule:
sourc2.o: source2.cpp header1.hh header2.hh
and there is a way (by passing arguments like -M or -MD
to gcc
thru suitable rules) to get such dependencies automatically generated, see this question.
You may want to use remake to debug your Makefile
-s (e.g. as remake -x
). Run make -p
to understand which rules are known to make