如何修复“致命错误:opencv2/core.hpp:manjaro中没有安装opencv 4的文件或目录

7tofc5zh  于 2023-05-29  发布在  其他
关注(0)|答案(5)|浏览(382)

本质上,我已经能够为python安装openCV,但我也希望能够为C++安装它。我可以使用Linux发行版的包管理器(pacman for manjaro,基于arch)安装它,但是我还没有得到下面的程序来测试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/opencv 4/opencv 2/core. hpp。但是,当我将include语句更改为

#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.

表示所有的头文件路径目录本质上都是错误的。我尝试用下面的符号链接来解决这个问题,但也不起作用:

$ 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

虽然我不知道这意味着什么,也不知道这是否对我有帮助。我不希望有其他间接的解决办法。有什么解决办法吗?

ecfdbz9o

ecfdbz9o1#

抱歉各位找到了我自己的答案。对我来说,这是正确的编译方式:

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

yhuiod9q2#

如果您正在运行CMake,您还可以要求

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

b4lqfgs43#

试着用这个编译它:

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

owfi6suc4#

我使用代码块,为了修复这个问题,我只是将/usr/include/opencv 4复制到/usr/include/opencv 2。懒如这样:)

wqsoz72f

wqsoz72f5#

在tasks.json中添加以下“args”:

"-I/usr/include/opencv4"

举个例子,在我的例子中,我通过vcpkg安装了它:

"args": [
        "-fdiagnostics-color=always",
        "-g",
        "${file}",
        "-o",
        "${fileDirname}\\${fileBasenameNoExtension}.exe",
        "-I/vcpkg/packages/opencv4_x86-windows/include"
    ]

相关问题