提问人:user786 提问时间:8/3/2022 最后编辑:halferuser786 更新时间:8/11/2022 访问量:186
编译 opencv 程序导致 gcc -I/usr/local/lib test.cpp test.cpp:1:10:致命错误:opencv2/core.hpp:没有这样的文件或目录
Compiling opencv program cause gcc -I/usr/local/lib test.cpp test.cpp:1:10: fatal error: opencv2/core.hpp: No such file or directory
问:
编译opencv程序程序简单包含时出错#include <opencv2/core.hpp>
我用这个命令编译了它
g++ test.cpp -o app `pkg-config --cflags --libs opencv`
这是完全错误
gcc -I/usr/local/lib test.cpp
test.cpp:1:10: fatal error: opencv2/core.hpp: No such file or directory
1 | #include <opencv2/core.hpp>
我通过查看此页面来编译和安装 opencv https://docs.opencv.org/2.4/doc/tutorials/introduction/linux_install/linux_install.html#building-opencv-from-source-using-cmake-using-the-command-line
代码来自 https://docs.opencv.org/4.x/d3/d50/group__imgproc__colormap.html
我的 opencv 所以文件位于 /usr/local/lib
错误还说
Perhaps you should add the directory containing `opencv.pc'
to the PKG_CONFIG_PATH environment variable
但是我搜索了/usr目录,没有opencv.pc文件
更新
更新编译命令后编译程序 抛出此错误
$g++ -I/usr/local/include/opencv4/ test.cpp
/usr/bin/ld: /tmp/ccuJvWgF.o: in function `main':
test.cpp:(.text+0xb4): undefined reference to `cv::imread(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
/usr/bin/ld: test.cpp:(.text+0xde): undefined reference to `cv::Mat::empty() const'
/usr/bin/ld: test.cpp:(.text+0x154): undefined reference to `cv::Mat::Mat()'
/usr/bin/ld: test.cpp:(.text+0x1a1): undefined reference to `cv::applyColorMap(cv::_InputArray const&, cv::_OutputArray const&, int)'
/usr/bin/ld: test.cpp:(.text+0x21d): undefined reference to `cv::imshow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&)'
/usr/bin/ld: test.cpp:(.text+0x254): undefined reference to `cv::waitKey(int)'
/usr/bin/ld: test.cpp:(.text+0x265): undefined reference to `cv::Mat::~Mat()'
/usr/bin/ld: test.cpp:(.text+0x274): undefined reference to `cv::Mat::~Mat()'
/usr/bin/ld: test.cpp:(.text+0x33d): undefined reference to `cv::Mat::~Mat()'
/usr/bin/ld: test.cpp:(.text+0x355): undefined reference to `cv::Mat::~Mat()'
collect2: error: ld returned 1 exit status
$
更新 2
现在编译时
g++ -L/usr/local/lib/libopencv_core.so -I/usr/local/include/opencv4/ test.cpp
它会抛出此错误。/usr/local/lib 中有很多 opencv 所以文件我需要包含特定的 opencv 所以文件来编译链接中的代码
in function `main':
test.cpp:(.text+0xb4): undefined reference to `cv::imread(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
/usr/bin/ld: test.cpp:(.text+0xde): undefined reference to `cv::Mat::empty() const'
/usr/bin/ld: test.cpp:(.text+0x154): undefined reference to `cv::Mat::Mat()'
/usr/bin/ld: test.cpp:(.text+0x1a1): undefined reference to `cv::applyColorMap(cv::_InputArray const&, cv::_OutputArray const&, int)'
/usr/bin/ld: test.cpp:(.text+0x21d): undefined reference to `cv::imshow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&)'
/usr/bin/ld: test.cpp:(.text+0x254): undefined reference to `cv::waitKey(int)'
/usr/bin/ld: test.cpp:(.text+0x265): undefined reference to `cv::Mat::~Mat()'
/usr/bin/ld: test.cpp:(.text+0x274): undefined reference to `cv::Mat::~Mat()'
/usr/bin/ld: test.cpp:(.text+0x33d): undefined reference to `cv::Mat::~Mat()'
/usr/bin/ld: test.cpp:(.text+0x355): undefined reference to `cv::Mat::~Mat()'
collect2: error: ld returned 1 exit sta
答:
使用该选项,您应该指定库搜索路径。-L
然后,使用选项指定要链接的库名称。-l
所以就你而言,我希望看到.请注意,该名称省略了前缀和文件扩展名。(您可能需要更多 OpenCV 库。-L/usr/local/lib -lopencv_core
-l
lib
看到你的挣扎,我认为最好阅读一个关于编译和链接 C/C++ 程序的一般教程(在你的平台上)。
评论
当我将一些代码从 OpenCV2 移动到 OpenCV4 时,我遇到了类似的问题,看来您也在使用一些 OpenCV4。我的解决方法是不包括opencv2/,而是包括opencv4 / opencv2/。不完全确定是什么让它起作用,我很久以前就这样做了,但从那以后它就起作用了。
评论
opencv2/
usr/local/lib/
-I
g++ -I/usr/local/lib test.cpp test.cpp:1:10: fatal error: opencv2/core.hpp: No such file or directory 1 | #include <opencv2/core.hpp> | ^~~~~~~~~~~~~~~~~
pkg-config --cflags --libs opencv
pkg-config --cflags --libs opencv
/usr/local/include
/usr/local/lib