OpenCV g++编译在一台机器上工作但在另一台机器上工作时未定义引用
我面临着 OpenCV 与 g++ 链接的明显非常典型的未定义参考。我在两台机器上运行,都安装了 opencv/2.3.14.6,linux 操作系统,使用 pkg-config 编译相同的代码。我检查了“pkg-config opencv --libs”,两者的结果相同
-L/usr/local/lib -lopencv_calib3d -lopencv_contrib -lopencv_core -lopencv_features2d -lopencv_flann -lopencv_gpu -lopencv_highgui -lopencv_imgproc -lopencv_legacy -lopencv_ml -lopencv_nonfree -lopencv_objDetect -lopencv_ocl -lopencv_photo -lopencv_stitching -lopencv_superres -lopencv_ts -lopencv_video -lopencv_videostab -lrt -lpthread -lm -ldl
失败的机器具有 CentOS Linux 版本 7.9.2009(将称为 centos 机器),编译通过的 Linux 版本 4.19.0-9 -amd64(将其称为 debian 机器)。 在我的代码中,我使用 #include
,它应该包含所有 opencv 库,但是,我尝试也包含特定的库,结果相同,debian 机器一切都好,centos机器出现故障。我什至尝试运行 OpenCV 文档中的示例之一,其中使用了我标记的大多数函数,得到了相同的结果。这是错误堆栈的一部分,imread、imshow、namedwindow 和 videowriter 也出现同样的错误,其他一切看起来都很好:
test.cpp:(.text+0x1af4): 对 `cv::putText(cv::Mat&, std::cxx11::basic_string
const&, cv::Point , int, double, cv::Scalar, int, int,布尔)'
我检查过,所有这些函数确实存在于所包含的库中,例如, putText 包含在 opencv2/core/core.hpp 中
CV_EXPORTS_W void putText( Mat& img, const string& text, Point org, int fontFace、double fontScale、标量颜色、 int 厚度=1,int lineType=8, 布尔bottomLeftOrigin = false);
这是我用来编译的命令行
g++ -Wall -std=c++11 -o test test.cpp `pkg-config --cflags --libs opencv`
还尝试了下一个,以防万一
g++ -Wall -std=c++11 -o test test.cpp `pkg-config opencv --cflags --libs`
这
g++ -Wall -std=c++11 -o test test.cpp $(pkg-config opencv --cflags --libs)
也尝试用直接库列表替换 pkg-config opencv --cflags --libs ,仍然没有能够在centos机器上编译,我确实需要在centos机器上运行,但我无法,我检查过的大多数帖子都是通过添加缺少的pkg-config opencv --cflags --libs
来解决的到命令行,代码语法错误要编译的文件、丢失文件或重新安装 opencv,但这些都没有帮助我。还尝试按照此处的建议使用不同的gcc版本,但结果相同。我想知道是否还有其他我可以尝试的东西,或者我是否注定要使用 debian 机器(centos 机器是 HPC 机器,代码需要一周才能在 debian 机器上运行!)。
编辑:对于遇到类似问题的任何人,请参阅评论并检查 双重 ABI ,就我而言,我尝试使用使用不同 gcc 版本编译的外部库,解决方案是确保它们全部使用兼容的编译器版本进行编译。
I'm facing the apparently very typical undefined reference with OpenCV linking with g++. I'm running in two machines, both with opencv/2.3.14.6 installed, linux OS, same code to compile, using pkg-config. I checked 'pkg-config opencv --libs' and in both is the same result
-L/usr/local/lib -lopencv_calib3d -lopencv_contrib -lopencv_core -lopencv_features2d -lopencv_flann -lopencv_gpu -lopencv_highgui
-lopencv_imgproc -lopencv_legacy -lopencv_ml -lopencv_nonfree -lopencv_objdetect -lopencv_ocl -lopencv_photo
-lopencv_stitching -lopencv_superres -lopencv_ts -lopencv_video -lopencv_videostab -lrt -lpthread -lm -ldl
The machine where it fails has CentOS Linux release 7.9.2009 (will call it centos machine), the one were compilation passes Linux version 4.19.0-9-amd64 (will call it debian machine).
In my code, I use #include <opencv2/opencv.hpp>
, which should include all opencv libs, however, I tried also including the specific libs, same result, debian machine all good, centos machine fails. I even tried running one of the examples from OpenCV documentation where most of the functions I get flagged are being used, got the same result. This is part of the error stack, same error goes for imread, imshow, namedwindow and videowriter, everything else seems fine:
test.cpp:(.text+0x1af4): undefined reference to `cv::putText(cv::Mat&, std::cxx11::basic_string<char, std::char_traits, std::allocator > const&, cv::Point, int, double, cv::Scalar, int, int, bool)'
I checked and all those functions do exist in the libs being included, for example, putText is contained in opencv2/core/core.hpp
CV_EXPORTS_W void putText( Mat& img, const string& text, Point org,
int fontFace, double fontScale, Scalar color,
int thickness=1, int lineType=8,
bool bottomLeftOrigin=false );
This is the command line I'm using to compile
g++ -Wall -std=c++11 -o test test.cpp `pkg-config --cflags --libs opencv`
Also tried this next one, just in case
g++ -Wall -std=c++11 -o test test.cpp `pkg-config opencv --cflags --libs`
And this
g++ -Wall -std=c++11 -o test test.cpp $(pkg-config opencv --cflags --libs)
Also tried replacing pkg-config opencv --cflags --libs
with the direct libs list, still not able to compile in centos machine, and I do need to run in centos machine, but am unable to, most posts I've checked are solved with adding missing pkg-config opencv --cflags --libs
to command line, code syntax error in files to compile, missing file or reinstalling opencv, but none of those help me. Also tried using different gcc versions as recommended here, but same result. I wonder if there is something else I can try or if I'm doomed to use debian machine (centos machine is an HPC machine, code takes a week to run in debian machine!).
EDIT: for anyone having similar issues, see comments and check dual ABI, in my case, I was trying to use external libraries compiled with different gcc versions, solution is to ensure they are all compiled using compatible compiler versions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论