Click here to Skip to main content
15,880,427 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I tried to compile my program in terminal. It works perfectly that way. But now I have several files and would rather use makefile instead, I tried to create makefile for my c++ program, but I encounter error "opencv2/opencv.hpp file not found". But I can compile correctly with the first method (manually) and failed with Makefile. What did I do wrong with my makefile ? thank you

What I have tried:

here's how I manually compile it one by one :

g++ $(pkg-config --cflags --libs opencv4) -std=c++11  one.cpp -o one


and here's how I create my Makefile where I got library error :
BIN_DIR= .
CC = g++
CFLAGS = -std=c++11

OPENCV = pkg-config --cflags --libs opencv4
LIBS = $(OPENCV)

all: $(BIN_DIR)/one $(BIN_DIR)/two

$(BIN_DIR)/one: one.o
    ${CC} -o $(BIN_DIR)/one one.o $(LIBS)

$(BIN_DIR)/two: two.o
    ${CC} -o $(BIN_DIR)/two two.o $(LIBS)

one.o: one.cpp
    ${CC} $(CFLAGS) -c one.cpp

two.o: two.cpp
    ${CC} $(CFLAGS) -c two.cpp

clean:
    rm -f *.o
    rm -f $(BIN_DIR)/one
    rm -f $(BIN_DIR)/two

allclean:
    rm -f *.o
    rm -f $(BIN_DIR)/one
    rm -f $(BIN_DIR)/two
    rm -f Makefile
Posted
Updated 30-Jan-19 5:16am
Comments
Richard MacCutchan 30-Jan-19 4:03am    
You need to set a variable for the location of the opencv header files, so the compiler knows where to find it.

1 solution

The line
OPENCV = pkg-config --cflags --libs opencv4
doesn't evaluate the command, for that you need to wrap with $(shell ...). Also, you need to tell the compiler for each module where to find the include files for opencv. I think the following should do the trick.
CFLAGS = -std=c++11 $(shell pkg-config --cflags opencv4)
LIBS = $(shell pkg-config --libs opencv4)
 
Share this answer
 
v2
Comments
lock&_lock 30-Jan-19 12:06pm    
Oh I see, your solution works, thank you !
k5054 30-Jan-19 13:03pm    
Just a quick note - make does have implicit rules for compiling and linking c++ sources. The default C++ compiler is CXX, which uses the CXXFLAGS variable to pass C++ specific flags to the compiler. If you had a Makefile that looked like
CXXFLAGS = -std=c++11 $(shell pkg-config --cflags opencv4)
LDLIBS=$(shell pkg-config --libs opencv4)

Then you could compile one.cpp to an executable by saying make one. Note that make doesn't know that multiple .o files from C++ sources need to be linked with libstdc++ so you still need to let make know it needs to use the C++ compiler to link with. In that case your target can be simplified to
foo: foo.o bar.o baz.o
        $(CXX) $^ $(LDFLAGS) -o $@

This will generate foo.o, bar.o and baz.o from foo.cpp, bar.cpp and baz.cpp as needed, and then link them all together with into the executable foo.
lock&_lock 30-Jan-19 22:48pm    
oh, so this is how I do it instead of do it one by one ! This is genius ! I wasn't aware of this method. I'll try it, thanks again !

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900