How to fix "fatal error: opencv2/core.hpp: No such file or directory" for opencv4 installed in manjaro
Asked Answered
E

6

16

Essentially, I've been able to install openCV fine for python but I also want to be able to do it for C++. I was able to install it using my linux distro's package manager (pacman for manjaro which is based on arch) but I haven't gotten the following program to work to test openCV

#include <iostream>
#include <opencv2/core.hpp>

int main() {
   std::cout << "OpenCV version: " << CV_VERSION << std::endl;
   return 0;
}

The error I get from trying to compile this program is the following:

cv.cpp:2:10: fatal error: opencv2/core.hpp: No such file or directory
    2 | #include <opencv2/core.hpp>
      |          ^~~~~~~~~~~~~~~~~~
 compilation terminated.

Apparently, this is a common error, however I have tried many solutions and they all seem to not work. I know where core.hpp is and it is actually at /usr/local/include/opencv4/opencv2/core.hpp. However, when I change the include statement to

 #include opencv4/opencv2/core.hpp

I get the following error

 In file included from cv.cpp:2:
 /usr/include/opencv4/opencv2/core.hpp:52:10: fatal error: opencv2/core                /cvdef.h: No such file or directory
   52 | #include "opencv2/core/cvdef.h"
      |          ^~~~~~~~~~~~~~~~~~~~~~
 compilation terminated.

Indicating that all the header files path directories are wrong essentially. I tried to fix this with the following symbolic link but that didn't work either:

$ sudo ln -s /usr/local/include/opencv4/opencv2/ /usr/local/include/opencv2

I also tried compiling in the following way:

$ g++ -o cv cv.cpp -I/usr/local/include/opencv4 -Lusr/local/lib -lopencv_core

That also didn't work and gave me the same errors. For reference, this is how I compiled before:

$ g++ cv.cpp -o cv

I also don't know if this means anything, but

$ pkg-config --libs opencv4

gives me the following results:

-lopencv_gapi -lopencv_stitching -lopencv_aruco -lopencv_bgsegm     -lopencv_bioinspired -lopencv_ccalib -lopencv_cvv -lopencv_dnn_objdetect -lopencv_dnn_superres -lopencv_dpm -lopencv_highgui -lopencv_face -lopencv_freetype -lopencv_fuzzy -lopencv_hdf -lopencv_hfs -lopencv_img_hash -lopencv_line_descriptor -lopencv_quality -lopencv_reg -lopencv_rgbd -lopencv_saliency -lopencv_stereo -lopencv_structured_light -lopencv_phase_unwrapping -lopencv_superres -lopencv_optflow -lopencv_surface_matching -lopencv_tracking -lopencv_datasets -lopencv_text -lopencv_dnn -lopencv_plot -lopencv_videostab -lopencv_video -lopencv_videoio -lopencv_viz -lopencv_xfeatures2d -lopencv_shape -lopencv_ml -lopencv_ximgproc -lopencv_xobjdetect -lopencv_objdetect -lopencv_calib3d -lopencv_imgcodecs -lopencv_features2d -lopencv_flann -lopencv_xphoto -lopencv_photo -lopencv_imgproc -lopencv_core 

Although I have no idea what this means nor do I know if this helps me at all. I'd rather not have any other indirect solutions. Any ideas on how to fix this?

Emikoemil answered 20/10, 2019 at 23:19 Comment(0)
E
11

Sorry, guys. Found my own answer. For me, this was the correct way to compile:

  g++ cv.cpp -o cv -I/usr/include/opencv4
Emikoemil answered 20/10, 2019 at 23:27 Comment(0)
C
7

In case you're running CMake, you can also ask to

find_package (OpenCV 4.0.0 REQUIRED)
include_directories ("/usr/include/opencv4/")
Cartography answered 26/4, 2021 at 14:30 Comment(2)
A little bit less hardcoded variant, would be include_directores(${OpenCV_INCLUDE_DIRS})Sherrell
You just have a typo in the word "directories"Cartography
A
1

Try compiling it with this:

FLAGS="$(pkg-config --cflags --libs opencv4)"
g++ cv.cpp -o cv $FLAGS
Aerospace answered 24/7, 2020 at 19:16 Comment(0)
S
0

I use codeblocks and to hot fix this I just copied /usr/include/opencv4 to /usr/include/opencv2. Lazy as that :)

Smoothbore answered 15/11, 2019 at 17:57 Comment(0)
C
0

Add to tasks.json the following inside "args":

"-I/usr/include/opencv4"

As an example, in my case I had it installed through vcpkg:

"args": [
        "-fdiagnostics-color=always",
        "-g",
        "${file}",
        "-o",
        "${fileDirname}\\${fileBasenameNoExtension}.exe",
        "-I/vcpkg/packages/opencv4_x86-windows/include"
    ]
Cist answered 25/5, 2023 at 15:43 Comment(0)
F
0

Using Makefile and make command

CC = g++
PROJECT = hist2
SRC = histogram.cpp
LIBS := $(shell pkg-config --libs opencv4)
INCL = -I/usr/include/opencv4
all : $(SRC)

$(CC) $(SRC) -o $(PROJECT) $(INCL) $(LIBS)
Fabiolafabiolas answered 26/5 at 12:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.