c++ - 编译opencv程序导致gcc -I/usr/local/lib test.cpp test.cpp :1:10: fatal error: opencv2/core. hpp: No such file or directory

标签 c++ opencv undefined-reference

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

我用这个命令编译了它

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

compiling opencv in c++

这是一个完整的错误

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>

我通过查看此页面https://docs.opencv.org/2.4/doc/tutorials/introduction/linux_install/linux_install.html#building-opencv-from-source-using-cmake-using-the-command-line来编译并安装opencv

代码来自https://docs.opencv.org/4.x/d3/d50/group__imgproc__colormap.html

我的 opencv so 文件位于/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 so文件,我是否需要包含特定的opencv so文件来编译链接中的代码

 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 选项指定要链接的库名称。

因此,在您的情况下,我希望看到-L/usr/local/lib -lopencv_core。请注意,-l 名称省略了 lib 前缀和文件扩展名。 (您可能需要更多 OpenCV 库。)

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

关于c++ - 编译opencv程序导致gcc -I/usr/local/lib test.cpp test.cpp :1:10: fatal error: opencv2/core. hpp: No such file or directory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73219741/

相关文章:

c++ - 如何测量C math.h库函数的执行时间?

c++ - 如何在 Qt 中获得与 CSS 规则 `display: inline-block` 相同的行为?

python - 使用 Quantlib Python 引导

c++ - 在 C++ 中读取索引调色板图像

c++ - OpenCV 2.4.1 与 Qt 的静态链接

linux - 对符号 'pthread_key_delete@@GLIBC_2.2.5 的 undefined reference

c++ - C++ 中的无符号整数 for 循环

python - 如何将倾斜的指纹图像旋转到垂直直立位置

c++ - 是否可以使用 TensorFlow 训练自己的模块,然后在 OpenCV 中使用它进行检测?

c++ - 什么是 undefined reference /未解析的外部符号错误,我该如何解决?