13

本质上,我已经能够为 python 安装 openCV,但我也希望能够为 C++ 安装它。我能够使用我的 linux 发行版的包管理器(基于 arch 的 pacman for manjaro)安装它,但我还没有得到以下程序来测试 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.

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

$ 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 

虽然我不知道这意味着什么,也不知道这是否对我有帮助。我宁愿没有任何其他间接解决方案。有想法该怎么解决这个吗?

4

4 回答 4

10

对不起大家。找到了我自己的答案。对我来说,这是编译的正确方法:

  g++ cv.cpp -o cv -I/usr/include/opencv4
于 2019-10-20T23:27:20.377 回答
2

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

find_package (OpenCV 4.0.0 REQUIRED)
include_directories ("/usr/include/opencv4/")
于 2021-04-26T14:30:08.937 回答
1

尝试用这个编译它:

FLAGS="$(pkg-config --cflags --libs opencv4)"
g++ cv.cpp -o cv $FLAGS
于 2020-07-24T19:16:12.137 回答
0

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

于 2019-11-15T17:57:35.910 回答