【问题标题】:Install OpenCV and compile C program using GCC安装 OpenCV 并使用 GCC 编译 C 程序
【发布时间】:2013-01-18 12:28:20
【问题描述】:

作为我正在进行的一个更大项目的一部分,我需要在 C 程序上使用 OpenCV 库。我在 Fedora 17 32 位中使用 yum 安装了 OpenCV 和 opencv-devel。我指示预处理器导入 opencv/cv.h 和 opencv/highgui.h 作为必要的头文件。

正如我所提到的,gcc 用于编译整个 C 项目。但是,链接器似乎有问题。

每当我尝试编译项目 (gcc opencv.c -o opencv) 时,我都会得到一个错误列表,类似于:

/tmp/ccLJWE0c.o: In function `cvRound': opencv.c:(.text+0x19):
undefined reference to `lrint' /tmp/ccLJWE0c.o: In function
`cvDecRefData': opencv.c:(.text+0xa5c): undefined reference to
`cvFree_' opencv.c:(.text+0xacd): undefined reference to `cvFree_'
/tmp/ccLJWE0c.o: In function `cvGetRow': opencv.c:(.text+0xbc3):
undefined reference to `cvGetRows' /tmp/ccLJWE0c.o: In function
`cvGetCol': opencv.c:(.text+0xbee): undefined reference to `cvGetCols'
/tmp/ccLJWE0c.o: In function `cvReleaseMatND': opencv.c:(.text+0xc01):
undefined reference to `cvReleaseMat' /tmp/ccLJWE0c.o: In function
`cvSubS': opencv.c:(.text+0xd21): undefined reference to `cvAddS'
/tmp/ccLJWE0c.o: In function `cvCloneSeq': opencv.c:(.text+0xd6f):
undefined reference to `cvSeqSlice' /tmp/ccLJWE0c.o: In function
`cvSetNew': opencv.c:(.text+0xdce): undefined reference to `cvSetAdd'
/tmp/ccLJWE0c.o: In function `cvGetSetElem': opencv.c:(.text+0xe61):
undefined reference to `cvGetSeqElem' /tmp/ccLJWE0c.o: In function
`cvEllipseBox': opencv.c:(.text+0xf61): undefined reference to
`cvEllipse' /tmp/ccLJWE0c.o: In function `cvFont':
opencv.c:(.text+0xfb1): undefined reference to `cvInitFont'
/tmp/ccLJWE0c.o: In function `cvReadIntByName':
opencv.c:(.text+0x103f): undefined reference to `cvGetFileNodeByName'
/tmp/ccLJWE0c.o: In function `cvReadRealByName':
opencv.c:(.text+0x10d0): undefined reference to `cvGetFileNodeByName'
/tmp/ccLJWE0c.o: In function `cvReadStringByName':
opencv.c:(.text+0x112a): undefined reference to `cvGetFileNodeByName'
/tmp/ccLJWE0c.o: In function `cvReadByName': opencv.c:(.text+0x115a):
undefined reference to `cvGetFileNodeByName' opencv.c:(.text+0x1170):
undefined reference to `cvRead' /tmp/ccLJWE0c.o: In function
`cvCreateSubdivDelaunay2D': opencv.c:(.text+0x11a3): undefined
reference to `cvCreateSubdiv2D' opencv.c:(.text+0x11cd): undefined
reference to `cvInitSubdivDelaunay2D' /tmp/ccLJWE0c.o: In function
`cvContourPerimeter': opencv.c:(.text+0x1307): undefined reference to
`cvArcLength' /tmp/ccLJWE0c.o: In function `cvCalcHist':
opencv.c:(.text+0x132f): undefined reference to `cvCalcArrHist'
/tmp/ccLJWE0c.o: In function `main': opencv.c:(.text+0x14cd):
undefined reference to `cvCreateImage' opencv.c:(.text+0x1510):
undefined reference to `cvGet2D' opencv.c:(.text+0x159e): undefined
reference to `cvSet2D' opencv.c:(.text+0x15df): undefined reference to
`cvSaveImage' collect2: error: ld returned 1 exit status

另外,当我使用以下方法编译程序时:

gcc opencv.c -o opencv `pkg-config --libs --cflags opencv` -ldl

我仍然得到:

/usr/bin/ld: /tmp/ccMRviO3.o: undefined reference to symbol 'lrint@@GLIBC_2.1'
/usr/bin/ld: note: 'lrint@@GLIBC_2.1' is defined in DSO /lib/libm.so.6 so try adding it to the linker command line
/lib/libm.so.6: could not read symbols: Invalid operation
collect2: error: ld returned 1 exit status

我一直在努力寻找解决方案,但似乎没有任何办法可以解决问题。在 OpenCV 文档中,他们提到了安装所需库的不同方式,但我并不真正理解我必须遵循的步骤。我认为 Fedora 开发了已编译并可以使用的软件包。无论如何,如果这是问题所在,有没有一种简单的方法可以让整个事情顺利进行?

我已经使用 Visual Basic 编码 6 年了,但我刚刚开始学习 C 作为我大学教育的一部分;因此我在操作 GCC 方面不是很有经验。 :(我会要求你尽可能解释清楚!:)

感谢任何帮助!提前致谢!!! :D

【问题讨论】:

    标签: c gcc opencv header linker


    【解决方案1】:

    尝试添加-lm,以包含提供lrint 的数学库(请参阅here

    【讨论】:

    • 谢谢!你完全正确!!! :D 它不再抛出错误。然而我不得不问:这个“魔法”-lm 参数是什么?在您附加的链接中,它被用来链接数学库......我们如何使用这些参数以及为什么?
    • -lm 告诉链接器链接到 math 库。如果您想了解更多信息,我建议您阅读有关链接器的一般信息,例如快速搜索“c 链接器教程”后出现的 thisthat 教程
    • 很抱歉利用了你的好意,但我可以问最后一个问题吗? ;) gcc 命令行的“pkg-config 及其参数”部分有什么作用?
    • 尝试从命令行运行pkg-config --libs --cflags opencv - 您会看到它会打印出适用于各种包含路径(请参阅-I invocation option)和linker commands 的命令行参数列表编译依赖于opencv 的程序。你可以阅读更多关于pkg-config herebackticks command substitution here
    • 好的,this forum posting 帮我解决了这个问题;似乎在当前的gcc 版本中,所有标志和选项都必须在输入和输出文件之后添加。我会将这些信息添加到my original SU question
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-07
    相关资源
    最近更新 更多