本质上,我已经能够为 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
虽然我不知道这意味着什么,也不知道这是否对我有帮助。我宁愿没有任何其他间接解决方案。有想法该怎么解决这个吗?