编译 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

提问人:user786 提问时间:8/3/2022 最后编辑:halferuser786 更新时间:8/11/2022 访问量:186

问:

编译opencv程序程序简单包含时出错#include <opencv2/core.hpp>

我用这个命令编译了它

 g++ test.cpp -o app `pkg-config --cflags --libs opencv`

在 C++ 中编译 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
C++ opencv 未定义引用

评论

0赞 meaning-matters 8/3/2022
除非在您的目录中,否则您需要有一个指向它的包含路径。这可能是你的问题吗?opencv2/usr/local/lib/-I
0赞 user786 8/3/2022
@meaning 件事,我尝试了相同的错误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> | ^~~~~~~~~~~~~~~~~
0赞 G.M. 8/3/2022
您说您使用“g++ test.cpp -o app”进行编译,但随后显示“g++ -I/usr/local/lib test.cpp”的错误输出:您能澄清用于编译和链接的精确命令吗?另外,请编辑您的问题以显示 的输出。pkg-config --cflags --libs opencvpkg-config --cflags --libs opencv
1赞 john 8/3/2022
@user786 重要的不是 .so 文件,而是 .hpp 文件。通常情况下,他们不会/usr/local/include/usr/local/lib
1赞 user786 8/3/2022
@meaning问题,现在我已经包括了 -I 和 -L,请检查我更新的问题。感谢您的帮助

答:

1赞 meaning-matters 8/3/2022 #1

使用该选项,您应该指定库搜索路径。-L

然后,使用选项指定要链接的库名称。-l

所以就你而言,我希望看到.请注意,该名称省略了前缀和文件扩展名。(您可能需要更多 OpenCV 库。-L/usr/local/lib -lopencv_core-llib

看到你的挣扎,我认为最好阅读一个关于编译和链接 C/C++ 程序的一般教程(在你的平台上)。

评论

0赞 user786 8/3/2022
是的,我认为您的意思是 -lopencv_core 指定特定的库。另外,使用 -L/usr/local/lib 可以确认吗
0赞 meaning-matters 8/3/2022
@user786我不清楚你有什么不清楚的,你想让我确认什么。请更具体。
1赞 ZwergofPhoenix 8/4/2022 #2

当我将一些代码从 OpenCV2 移动到 OpenCV4 时,我遇到了类似的问题,看来您也在使用一些 OpenCV4。我的解决方法是不包括opencv2/,而是包括opencv4 / opencv2/。不完全确定是什么让它起作用,我很久以前就这样做了,但从那以后它就起作用了。