在C+&#x2B中编译OpENCV时,对CV :: MAT :: MAT的未定义引用
我正在尝试使用C ++中的OpenCV 4.6.0编译一个简单的程序(我在Ubuntu 20.04上,这是默认安装),该程序读取图像,运行一个Brisk功能检测,并在图像上使用图像的位置显示图像,并在图像上显示了图像的位置imshow。我可以在.o文件中进行预处理,编译和组装,但是G ++到目前为止拒绝执行链接步骤。
我最初对每个OPENCV函数呼叫都会遇到一个不确定的参考错误,但是经过大量的痛苦和反复试验,包括多次卸载和重新安装OpenCV(按照
#!/bin/bash
g++ $@ -lopencv_core -lopencv_highgui -lopencv_imgcodecs -lopencv_imgproc -lopencv_calib3d -lopencv_dnn -lopencv_features2d -lopencv_flann -lopencv_gapi -lopencv_ml -lopencv_objdetect -lopencv_photo -lopencv_stitching -lopencv_videoio -lopencv_video
我从命令行中运行它,因为
./opencv_linker brisk_test.cpp -I/usr/local/include/opencv4
(我最终会将其变成makefile,但是现在我只是想让OpenCV完全工作。我确定我不严格需要所有这些库来完成我的
工作命令行给出以下错误:
usr/bin/ld: /tmp/ccdVMcvM.o: in function `main':
<path_to_directory>/brisk_test.cpp:19: undefined reference to `cv::Mat::Mat()'
collect2: error: ld returned 1 exit status
我还尝试过运行g ++ brisk_test.cpp -i/usr/usr/local/local/includ/opencv4`pkg -config -libs opencv4`
,我认为这是等效的。相同的结果。交换-i参数和pkg -config参数的顺序没有改变任何内容。
关于如何消除最后一个未定义的参考,我已经用完了想法。我不明白为什么G ++在任何OpenCV库中都找不到CV :: mat :: mat()(除非我只是不知道的其他地方库)。我已经在线搜索了答案,但我发现的任何内容都没有解决这个问题 - post 和 ,and 这篇文章没有答案。如果有人对可能发生的事情有任何想法,我将永远感激不尽。
编辑:我认为我要链接库的顺序是一个问题(但我可能错了)。关于链接库的顺序,所有在线上的所有内容都只是说使用`pkg-config - flags - libs opencv4`
(有时没有-flags),显然应该照顾好订购?我尝试了以下六个命令:
g++ -g brisk_test.cpp -I/usr/local/include/opencv4 `pkg-config --libs opencv4`
g++ -g brisk_test.cpp `pkg-config --libs opencv4` -I/usr/local/include/opencv4
g++ -g brisk_test.cpp -I/usr/local/include/opencv4 `pkg-config --cflags --libs opencv4`
g++ -g brisk_test.cpp `pkg-config --cflags --libs opencv4` -I/usr/local/include/opencv4
g++ -g `pkg-config --cflags opencv4` brisk_test.cpp `pkg-config --libs opencv4` -I/usr/local/include/opencv4
g++ -g `pkg-config --cflags opencv4` brisk_test.cpp -I/usr/local/include/opencv4 `pkg-config --libs opencv4`
所有这些命令都给了我与上面相同的错误消息。
编辑2:这是nm -cd/usr/local/lib/libopencv_core.so |的输出GREP MAT :: MAT
:
$ nm -CD /usr/local/lib/libopencv_core.so | grep Mat::Mat
00000000001c2580 T cv::Mat::Mat(int, int, int)
00000000001c12e0 T cv::Mat::Mat(int, int, int, void*, unsigned long)
00000000001c25d0 T cv::Mat::Mat(int, int, int, cv::Scalar_<double> const&)
00000000001c2440 T cv::Mat::Mat(int, int const*, int)
00000000001c2ab0 T cv::Mat::Mat(int, int const*, int, void*, unsigned long const*)
00000000001c2490 T cv::Mat::Mat(int, int const*, int, cv::Scalar_<double> const&)
00000000001c2640 T cv::Mat::Mat(cv::Size_<int>, int)
00000000001c14e0 T cv::Mat::Mat(cv::Size_<int>, int, void*, unsigned long)
00000000001c2690 T cv::Mat::Mat(cv::Size_<int>, int, cv::Scalar_<double> const&)
00000000001c1790 T cv::Mat::Mat(cv::Mat&&)
00000000001c19e0 T cv::Mat::Mat(cv::Mat const&)
00000000001c3630 T cv::Mat::Mat(cv::Mat const&, cv::Range const*)
00000000001c38a0 T cv::Mat::Mat(cv::Mat const&, cv::Range const&, cv::Range const&)
00000000001c2800 T cv::Mat::Mat(cv::Mat const&, cv::Rect_<int> const&)
00000000001c3cb0 T cv::Mat::Mat(cv::Mat const&, std::vector<cv::Range, std::allocator<cv::Range> > const&)
00000000001c2740 T cv::Mat::Mat(std::vector<int, std::allocator<int> > const&, int)
00000000001c34e0 T cv::Mat::Mat(std::vector<int, std::allocator<int> > const&, int, void*, unsigned long const*)
00000000001c2790 T cv::Mat::Mat(std::vector<int, std::allocator<int> > const&, int, cv::Scalar_<double> const&)
00000000001c11c0 T cv::Mat::Mat()
00000000001c2580 T cv::Mat::Mat(int, int, int)
00000000001c12e0 T cv::Mat::Mat(int, int, int, void*, unsigned long)
00000000001c25d0 T cv::Mat::Mat(int, int, int, cv::Scalar_<double> const&)
00000000001c2440 T cv::Mat::Mat(int, int const*, int)
00000000001c2ab0 T cv::Mat::Mat(int, int const*, int, void*, unsigned long const*)
00000000001c2490 T cv::Mat::Mat(int, int const*, int, cv::Scalar_<double> const&)
00000000001c2640 T cv::Mat::Mat(cv::Size_<int>, int)
00000000001c14e0 T cv::Mat::Mat(cv::Size_<int>, int, void*, unsigned long)
00000000001c2690 T cv::Mat::Mat(cv::Size_<int>, int, cv::Scalar_<double> const&)
00000000001c1790 T cv::Mat::Mat(cv::Mat&&)
00000000001c19e0 T cv::Mat::Mat(cv::Mat const&)
00000000001c3630 T cv::Mat::Mat(cv::Mat const&, cv::Range const*)
00000000001c38a0 T cv::Mat::Mat(cv::Mat const&, cv::Range const&, cv::Range const&)
00000000001c2800 T cv::Mat::Mat(cv::Mat const&, cv::Rect_<int> const&)
00000000001c3cb0 T cv::Mat::Mat(cv::Mat const&, std::vector<cv::Range, std::allocator<cv::Range> > const&)
00000000001c2740 T cv::Mat::Mat(std::vector<int, std::allocator<int> > const&, int)
00000000001c34e0 T cv::Mat::Mat(std::vector<int, std::allocator<int> > const&, int, void*, unsigned long const*)
00000000001c2790 T cv::Mat::Mat(std::vector<int, std::allocator<int> > const&, int, cv::Scalar_<double> const&)
00000000001c11c0 T cv::Mat::Mat()
编辑3:我知道哪一行代码引起了问题,这要归功于我有一个我真正应该已经尝试过的东西的好主意(我很糟糕,对不起 - 我' M确实不是专家):即,在程序成功编译和链接之前,请评论代码行。当我将程序简化为以下代码行时,它会成功编译和链接以产生可执行文件:
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/features2d.hpp>
#include <iostream>
using namespace std;
using namespace cv;
int main(int argc, char* argv[])
{
Mat img = imread("test_image.jpg", IMREAD_GRAYSCALE);
Ptr<BRISK> detector = BRISK::create();
std::vector<KeyPoint> keypoints;
detector->detect(img, keypoints);
return EXIT_SUCCESS;
}
破坏代码的是绘制关键点所需的下一条行的添加:即,G ++在我时无法执行链接。添加行 MAT IMG_KEYPOINTS;
返回之前。我不明白为什么这条线会引起问题,尤其是因为我显然能够在第一行中创建一个垫子对象。我确实知道错误发生在链接步骤中,因为即使包括有问题的代码行,我也能够编译并将程序组装到.o文件中。
编辑4:更改上面的代码(有效)以将第一行分为两行也破坏了事物:也就是说,
int main(int argc, char* argv[])
{
Mat img;
img = imread("test_image.jpg", IMREAD_GRAYSCALE);
Ptr<BRISK> detector = BRISK::create();
std::vector<KeyPoint> keypoints;
detector->detect(img, keypoints);
return EXIT_SUCCESS;
}
也给我相同的链接器错误。
I'm trying to compile a simple program using OpenCV 4.6.0 in C++ (I'm on Ubuntu 20.04 and that's the default installation) that reads an image, runs a BRISK feature detection, and displays the keypoints' locations on the image using imshow. I can do proprocessing, compilation, and assembly into a .o file no problem, but g++ has so far flatly refused to do the linking step.
I was originally getting an undefined reference error for every OpenCV function call, but after much pain and trial-and-error, including uninstalling and reinstalling OpenCV multiple times (following the instructions at https://docs.opencv.org/4.x/d7/d9f/tutorial_linux_install.html#tutorial_linux_install_detailed_basic_verify), I realized that for some reason, g++ will only link to OpenCV libraries when explicitly told to do so with -l. I have now written a bash script to manually link every single OpenCV library I can find in /usr/local/lib. The script is called opencv_linker, and it contains the following:
#!/bin/bash
g++ $@ -lopencv_core -lopencv_highgui -lopencv_imgcodecs -lopencv_imgproc -lopencv_calib3d -lopencv_dnn -lopencv_features2d -lopencv_flann -lopencv_gapi -lopencv_ml -lopencv_objdetect -lopencv_photo -lopencv_stitching -lopencv_videoio -lopencv_video
I run it from the command line as
./opencv_linker brisk_test.cpp -I/usr/local/include/opencv4
(I will turn this into a makefile eventually, but for now I'm just trying to get OpenCV to work at all. Also, I'm sure I don't strictly need all of these libraries for what I'm doing, but I don't have a good reference for which libraries include which functions, so I'm taking the brute force route.)
Running the above in the command line gives the following error:
usr/bin/ld: /tmp/ccdVMcvM.o: in function `main':
<path_to_directory>/brisk_test.cpp:19: undefined reference to `cv::Mat::Mat()'
collect2: error: ld returned 1 exit status
I have also tried running g++ brisk_test.cpp -I/usr/local/include/opencv4 `pkg-config --libs opencv4`
, which I think is equivalent. Same result. Swapping the order of the -I argument and the pkg-config argument didn't change anything.
I've run out of ideas as to what to do to eliminate this last undefined reference. I don't understand why g++ would be unable to find cv::Mat::Mat() in any of the OpenCV libraries (unless there's another library somewhere that I just don't know about). I've searched for answers online, and nothing I've found has addressed the problem -- the answers to this post and this post didn't solve the problem, and this post doesn't have an answer. If anyone has any ideas as to what might be going on, I will be eternally grateful.
EDIT: I don't think the order in which I'm linking the libraries is an issue (but I could be wrong). Everything online about the order in which to link the libraries just says to use `pkg-config --cflags --libs opencv4`
(sometimes without --cflags), and apparently that should take care of the ordering? I've tried the following six commands:
g++ -g brisk_test.cpp -I/usr/local/include/opencv4 `pkg-config --libs opencv4`
g++ -g brisk_test.cpp `pkg-config --libs opencv4` -I/usr/local/include/opencv4
g++ -g brisk_test.cpp -I/usr/local/include/opencv4 `pkg-config --cflags --libs opencv4`
g++ -g brisk_test.cpp `pkg-config --cflags --libs opencv4` -I/usr/local/include/opencv4
g++ -g `pkg-config --cflags opencv4` brisk_test.cpp `pkg-config --libs opencv4` -I/usr/local/include/opencv4
g++ -g `pkg-config --cflags opencv4` brisk_test.cpp -I/usr/local/include/opencv4 `pkg-config --libs opencv4`
All of them gave me the same error message as above.
EDIT 2: Here's the output of nm -CD /usr/local/lib/libopencv_core.so | grep Mat::Mat
:
$ nm -CD /usr/local/lib/libopencv_core.so | grep Mat::Mat
00000000001c2580 T cv::Mat::Mat(int, int, int)
00000000001c12e0 T cv::Mat::Mat(int, int, int, void*, unsigned long)
00000000001c25d0 T cv::Mat::Mat(int, int, int, cv::Scalar_<double> const&)
00000000001c2440 T cv::Mat::Mat(int, int const*, int)
00000000001c2ab0 T cv::Mat::Mat(int, int const*, int, void*, unsigned long const*)
00000000001c2490 T cv::Mat::Mat(int, int const*, int, cv::Scalar_<double> const&)
00000000001c2640 T cv::Mat::Mat(cv::Size_<int>, int)
00000000001c14e0 T cv::Mat::Mat(cv::Size_<int>, int, void*, unsigned long)
00000000001c2690 T cv::Mat::Mat(cv::Size_<int>, int, cv::Scalar_<double> const&)
00000000001c1790 T cv::Mat::Mat(cv::Mat&&)
00000000001c19e0 T cv::Mat::Mat(cv::Mat const&)
00000000001c3630 T cv::Mat::Mat(cv::Mat const&, cv::Range const*)
00000000001c38a0 T cv::Mat::Mat(cv::Mat const&, cv::Range const&, cv::Range const&)
00000000001c2800 T cv::Mat::Mat(cv::Mat const&, cv::Rect_<int> const&)
00000000001c3cb0 T cv::Mat::Mat(cv::Mat const&, std::vector<cv::Range, std::allocator<cv::Range> > const&)
00000000001c2740 T cv::Mat::Mat(std::vector<int, std::allocator<int> > const&, int)
00000000001c34e0 T cv::Mat::Mat(std::vector<int, std::allocator<int> > const&, int, void*, unsigned long const*)
00000000001c2790 T cv::Mat::Mat(std::vector<int, std::allocator<int> > const&, int, cv::Scalar_<double> const&)
00000000001c11c0 T cv::Mat::Mat()
00000000001c2580 T cv::Mat::Mat(int, int, int)
00000000001c12e0 T cv::Mat::Mat(int, int, int, void*, unsigned long)
00000000001c25d0 T cv::Mat::Mat(int, int, int, cv::Scalar_<double> const&)
00000000001c2440 T cv::Mat::Mat(int, int const*, int)
00000000001c2ab0 T cv::Mat::Mat(int, int const*, int, void*, unsigned long const*)
00000000001c2490 T cv::Mat::Mat(int, int const*, int, cv::Scalar_<double> const&)
00000000001c2640 T cv::Mat::Mat(cv::Size_<int>, int)
00000000001c14e0 T cv::Mat::Mat(cv::Size_<int>, int, void*, unsigned long)
00000000001c2690 T cv::Mat::Mat(cv::Size_<int>, int, cv::Scalar_<double> const&)
00000000001c1790 T cv::Mat::Mat(cv::Mat&&)
00000000001c19e0 T cv::Mat::Mat(cv::Mat const&)
00000000001c3630 T cv::Mat::Mat(cv::Mat const&, cv::Range const*)
00000000001c38a0 T cv::Mat::Mat(cv::Mat const&, cv::Range const&, cv::Range const&)
00000000001c2800 T cv::Mat::Mat(cv::Mat const&, cv::Rect_<int> const&)
00000000001c3cb0 T cv::Mat::Mat(cv::Mat const&, std::vector<cv::Range, std::allocator<cv::Range> > const&)
00000000001c2740 T cv::Mat::Mat(std::vector<int, std::allocator<int> > const&, int)
00000000001c34e0 T cv::Mat::Mat(std::vector<int, std::allocator<int> > const&, int, void*, unsigned long const*)
00000000001c2790 T cv::Mat::Mat(std::vector<int, std::allocator<int> > const&, int, cv::Scalar_<double> const&)
00000000001c11c0 T cv::Mat::Mat()
EDIT 3: I know which line of code is causing a problem, thanks to me having the bright idea of trying something which I really should have tried already (my bad, sorry -- I'm really not an expert at this): namely, commenting out lines of code until the program successfully compiles and links. When I reduce my program to just the following lines of code, it compiles and links successfully to produce an executable:
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/features2d.hpp>
#include <iostream>
using namespace std;
using namespace cv;
int main(int argc, char* argv[])
{
Mat img = imread("test_image.jpg", IMREAD_GRAYSCALE);
Ptr<BRISK> detector = BRISK::create();
std::vector<KeyPoint> keypoints;
detector->detect(img, keypoints);
return EXIT_SUCCESS;
}
What breaks the code is the addition of the next line necessary to draw keypoints: namely, g++ is unable to do the linking when I add the lineMat img_keypoints;
before the return. I don't understand why that line would cause an issue, particularly since I am apparently able to create a Mat object in the first line. I do know that the error occurs in the linking step, though, as I am able to compile and assemble my program into a .o file, even with the problematic line of code included.
EDIT 4: Changing the code above (which works) to split the first line into two also breaks things: namely,
int main(int argc, char* argv[])
{
Mat img;
img = imread("test_image.jpg", IMREAD_GRAYSCALE);
Ptr<BRISK> detector = BRISK::create();
std::vector<KeyPoint> keypoints;
detector->detect(img, keypoints);
return EXIT_SUCCESS;
}
also gives me the same linker error.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论