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

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

基本上,我已经能够很好地为 python 安装 openCV,但我也希望能够为 C++ 安装它。我能够使用我的 linux 发行版的包管理器(基于 arch 的 manjaro 的 pacman)安装它,但我还没有得到以下程序来测试 openCV

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

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

我在尝试编译该程序时遇到的错误如下:

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

显然,这是一个常见的错误,但是我尝试了很多解决方案,但它们似乎都不起作用。我知道 core.hpp 在哪里,它实际上在 /usr/local/include/opencv4/opencv2/core.hpp。但是,当我将包含语句更改为

 #include opencv4/opencv2/core.hpp

我收到以下错误

 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.

说明所有的头文件路径目录本质上都是错误的。我尝试使用以下符号 link 来解决此问题,但这也不起作用:

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

我也试过用下面的方式编译:

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

那也没有用,给了我同样的错误。作为参考,我之前是这样编译的:

$ g++ cv.cpp -o cv

我也不知道这是否意味着什么,但是

$ pkg-config --libs opencv4

给我以下结果:

-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 

尽管我不知道这意味着什么,也不知道这是否对我有帮助。我宁愿没有任何其他间接解决方案。关于如何解决此问题的任何想法?

对不起,伙计们。找到了我自己的答案。对我来说,这是正确的编译方式:

  g++ cv.cpp -o cv -I/usr/include/opencv4

我使用代码块并进行热修复,我刚刚将 /usr/include/opencv4 复制到 /usr/include/opencv2。就这么懒:)

尝试用这个编译它:

FLAGS="$(pkg-config --cflags --libs opencv4)"
g++ cv.cpp -o cv $FLAGS

如果您是 运行 CMake,您也可以询问

find_package (OpenCV 4.0.0 REQUIRED)
include_directories ("/usr/include/opencv4/")