编译opencv程序时出错,程序simple包含#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>
我通过查看此页面编译并安装了opencvhttps://docs.opencv.org/2.4/doc/tutorials/introduction/linux_install/linux_install.html#building-使用命令行从源代码使用cmake打开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++程序(在你的平台上(的通用教程会很好。
当我将一些代码从OpenCV2移动到OpenCV4时,我遇到了类似的问题,看起来你也在使用一些OpenCV4。我的修复是不包括opencv2/,而是包括opencv4/opencv2/。不太确定是什么让它起作用,我很久以前就这样做了,但从那以后就起了作用。