未定义的符号引用,但库已链接
Posted
技术标签:
【中文标题】未定义的符号引用,但库已链接【英文标题】:Undefined reference to symbol but library is linked 【发布时间】:2015-10-08 15:38:47 【问题描述】:我正在尝试使用 opencv 3.0 构建应用程序,但出现“未定义的符号引用错误”
我的问题是符号是在我链接到的库中定义的。
编译命令:
g++ -o DisparityMap main.o tools.o emb.o moc_tools.o moc_emb.o -lopencv_highgui -lopencv_core -lopencv_imgproc -lopencv_imgcodecs -lopencv_calib3d -lQtGui -lQtCore -lpthread
我得到的错误:
/usr/bin/ld: twowindow.o: undefined reference to symbol '_ZN2cv6String10deallocateEv'
//usr/local/lib/libopencv_core.so.3.0: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
但有问题的符号实际上是在我链接到的库中定义的:
scanelf -l -s _ZN2cv6String10deallocateEv | grep _ZN2cv6String10deallocateEv
ET_DYN _ZN2cv6String10deallocateEv /usr/local/lib/libopencv_objdetect.so.3.0.0
ET_DYN _ZN2cv6String10deallocateEv /usr/local/lib/libopencv_photo.so.3.0.0
ET_DYN _ZN2cv6String10deallocateEv /usr/local/lib/libopencv_core.so.3.0.0
ET_DYN _ZN2cv6String10deallocateEv /usr/local/lib/libopencv_video.so.3.0.0
ET_DYN _ZN2cv6String10deallocateEv /usr/local/lib/libopencv_features2d.so.3.0.0
ET_DYN _ZN2cv6String10deallocateEv /usr/local/lib/libopencv_ml.so.3.0.0
ET_DYN _ZN2cv6String10deallocateEv /usr/local/lib/libopencv_highgui.so.3.0.0
ET_DYN _ZN2cv6String10deallocateEv /usr/local/lib/libopencv_videoio.so.3.0.0
ET_DYN _ZN2cv6String10deallocateEv /usr/local/lib/libopencv_stitching.so.3.0.0
ET_DYN _ZN2cv6String10deallocateEv /usr/local/lib/libopencv_videostab.so.3.0.0
ET_DYN _ZN2cv6String10deallocateEv /usr/local/lib/libopencv_superres.so.3.0.0
ET_DYN _ZN2cv6String10deallocateEv /usr/local/lib/libopencv_flann.so.3.0.0
ET_DYN _ZN2cv6String10deallocateEv /usr/local/lib/libopencv_imgproc.so.3.0.0
ET_DYN _ZN2cv6String10deallocateEv /usr/local/lib/libopencv_imgcodecs.so.3.0.0
ET_DYN _ZN2cv6String10deallocateEv /usr/local/lib/libopencv_calib3d.so.3.0.0
ET_DYN _ZN2cv6String10deallocateEv /usr/local/lib/libopencv_shape.so.3.0.0
ET_DYN _ZN2cv6String10deallocateEv /usr/local/lib/libopencv_objdetect.so.3.0.0
ET_DYN _ZN2cv6String10deallocateEv /usr/local/lib/libopencv_photo.so.3.0.0
ET_DYN _ZN2cv6String10deallocateEv /usr/local/lib/libopencv_core.so.3.0.0
ET_DYN _ZN2cv6String10deallocateEv /usr/local/lib/libopencv_video.so.3.0.0
ET_DYN _ZN2cv6String10deallocateEv /usr/local/lib/libopencv_features2d.so.3.0.0
ET_DYN _ZN2cv6String10deallocateEv /usr/local/lib/libopencv_ml.so.3.0.0
ET_DYN _ZN2cv6String10deallocateEv /usr/local/lib/libopencv_highgui.so.3.0.0
ET_DYN _ZN2cv6String10deallocateEv /usr/local/lib/libopencv_videoio.so.3.0.0
ET_DYN _ZN2cv6String10deallocateEv /usr/local/lib/libopencv_stitching.so.3.0.0
ET_DYN _ZN2cv6String10deallocateEv /usr/local/lib/libopencv_videostab.so.3.0.0
ET_DYN _ZN2cv6String10deallocateEv /usr/local/lib/libopencv_superres.so.3.0.0
ET_DYN _ZN2cv6String10deallocateEv /usr/local/lib/libopencv_flann.so.3.0.0
ET_DYN _ZN2cv6String10deallocateEv /usr/local/lib/libopencv_imgproc.so.3.0.0
ET_DYN _ZN2cv6String10deallocateEv /usr/local/lib/libopencv_imgcodecs.so.3.0.0
ET_DYN _ZN2cv6String10deallocateEv /usr/local/lib/libopencv_calib3d.so.3.0.0
ET_DYN _ZN2cv6String10deallocateEv /usr/local/lib/libopencv_shape.so.3.0.0
【问题讨论】:
OpenCV 和 Qt 使用相同的编译器构建? 另外,请确保您的库在链接行中的顺序正确。一个库只能依赖于命令行中紧随其后的库。 【参考方案1】:编译命令:
这不是一个编译命令,它是一个链接命令。
我得到的错误:
错误是引用twowindow.o
,这在您的链接命令中不是。
因此,很难相信错误真的来自您提供的链接命令。
很可能,错误来自一个不同的链接命令,而那个链接命令上没有-lopencv_core
。
【讨论】:
【参考方案2】:在链接库之前,请给出带有 -L 标志的库的路径。
【讨论】:
以上是关于未定义的符号引用,但库已链接的主要内容,如果未能解决你的问题,请参考以下文章