0

I've got the following makefile:

OBJS:=  main.o

CV_LIBS:= -I/usr/local/include/opencv4 -I/usr/local/include  -L/usr/local/lib/ -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_ml -lopencv_video -lopencv_features2d -lopencv_calib3d -lopencv_objdetect -lopencv_contrib -lopencv_legacy -lopencv_flann

main: $(OBJS) 
      g++ $(OBJS) $(CV_LIBS) -O3 -ffast-math -o main -Wall -g

main.o: main.h
        g++ -c main.cpp -Wall -g

My main.h file has the following line:

#include <opencv4/opencv2/objdetect.hpp>

I get the following error:

/usr/local/include/opencv4/opencv2/objdetect.hpp:47:10: fatal error: opencv2/core.hpp: No such file or directory
   47 | #include "opencv2/core.hpp"

I have tried the following as well with no luck:

#include <opencv2/objdetect.hpp>

Error:

main.h:4:10: fatal error: opencv2/objdetect.hpp: No such file or directory

I can confirm there is an objdetect.hpp in both:

  • /usr/local/include/opencv4/opencv2/ and
  • /usr/local/include/opencv4/opencv2/objdetect.hpp

I have looked at this question, but unfortunately I'm not using cmake. What am I doing wrong? Many thanks.

JJT
  • 167
  • 7

1 Answers1

1

If you look at the compile line that make prints out (which you didn't include in your question), it's pretty straightforward why you get this error.

The compile line, that compiles main.c into main.o, looks like this:

g++ -c main.cpp -Wall -g

You can clearly see that there is no reference to the directories you need to include header files here.

Your makefile has this:

CV_LIBS:= -I/usr/local/include/opencv4 -I/usr/local/include  -L/usr/local/lib/ -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_ml -lopencv_video -lopencv_features2d -lopencv_calib3d -lopencv_objdetect -lopencv_contrib -lopencv_legacy -lopencv_flann

where you are combining compiler flags, such as -I, with linker flags, such as -L and -l in the same variable, calling them all "LIBS", and adding them only to the link line.

You need to put the compiler flags into the compile command, and the linker flags into the link command:

CPPFLAGS = -I/usr/local/include/opencv4 -I/usr/local/include

CV_LIBS = -L/usr/local/lib/ -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_ml -lopencv_video -lopencv_features2d -lopencv_calib3d -lopencv_objdetect -lopencv_contrib -lopencv_legacy -lopencv_flann

main: $(OBJS) 
        g++ $(OBJS) $(CV_LIBS) -O3 -ffast-math -o main -Wall -g

main.o: main.h
        g++ $(CPPFLAGS) -c main.cpp -Wall -g

FYI, it's more correct for you to use #include <opencv2/objdetect.hpp> in your source code, not #include <opencv4/opencv2/objdetect.hpp>

MadScientist
  • 92,819
  • 9
  • 109
  • 136