【问题标题】:OpenCV application (not library) linking errorsOpenCV 应用程序(不是库)链接错误
【发布时间】:2012-03-25 18:30:55
【问题描述】:

经过大量搜索,我仍然对发生的事情一无所知。

我编写了一个 OpenCV 简单应用程序(或多或少基于教程),但构建它一直是一个挑战。这是代码:-

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <cv.h>
#include <highgui.h>


int main()
{
    CvCapture *cam;

    cam = cvCaptureFromFile("http://icarus@cocodyn.dynip.net:2002/videostream.cgi");
    if (cam == NULL) {
        printf("camera is null\n");
    }
    else {
        printf("camera is non-null\n");
    }

    cvNamedWindow("Mycam", 0);

    while (cvWaitKey(10) != atoi("q")) {
        double t1 = (double) cvGetTickCount();
        IplImage *img = cvQueryFrame(cam);
        double t2 = (double) cvGetTickCount();
        printf("time: %gms  fps: %.2g\n", (t2-t1)/(cvGetTickFrequency() * 1000.), 1000./((t2-t1)/(cvGetTickFrequency() * 1000.)));
        cvShowImage("Mycam", img);
    }

    cvReleaseCapture(&cam);
}

这是我的 Makefile 的样子:

ipcamcap: ipcamcap.c
    g++ `pkg-config --cflags --libs opencv` -o ipcamcap ipcamcap.c

最后,这是我得到的错误 --

/home/icarus/Projects/ipcamcap$ make
g++ `pkg-config --cflags --libs opencv` -o ipcamcap ipcamcap.c
/tmp/ccV5YLqQ.o: In function `main':
ipcamcap.c:(.text+0xf): undefined reference to `cvCreateFileCapture'
ipcamcap.c:(.text+0x41): undefined reference to `cvNamedWindow'
ipcamcap.c:(.text+0x4b): undefined reference to `cvGetTickCount'
ipcamcap.c:(.text+0x61): undefined reference to `cvQueryFrame'
ipcamcap.c:(.text+0x6a): undefined reference to `cvGetTickCount'
ipcamcap.c:(.text+0x8c): undefined reference to `cvGetTickFrequency'
ipcamcap.c:(.text+0xd6): undefined reference to `cvGetTickFrequency'
ipcamcap.c:(.text+0x118): undefined reference to `cvShowImage'
ipcamcap.c:(.text+0x122): undefined reference to `cvWaitKey'
ipcamcap.c:(.text+0x147): undefined reference to `cvReleaseCapture'
collect2: ld returned 1 exit status
make: *** [ipcamcap] Error 1
/home/icarus/Projects/ipcamcap$ 

在主机上,我可以看到--

/home/icarus/Projects/ipcamcap$ echo `pkg-config --cflags --libs opencv`

-I/usr/local/include/opencv -I/usr/local/include -L/usr/local/lib -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_ml -lopencv_video -lopencv_features2d -lopencv_calib3d -lopencv_objdetect -lopencv_contrib -lopencv_legacy - lopencv_flann /home/icarus/Projects/ipcamcap$

而且,我在这台主机上也有库 --

/home/icarus/Projects/ipcamcap$ ls -l /usr/local/lib/libopencv_core.so*
lrwxrwxrwx 1 root root      21 2012-02-28 11:44 /usr/local/lib/libopencv_core.so -> libopencv_core.so.2.3
lrwxrwxrwx 1 root root      23 2012-02-28 11:44 /usr/local/lib/libopencv_core.so.2.3 -> libopencv_core.so.2.3.1
-rw-r--r-- 1 root root 2169691 2012-02-28 11:16 /usr/local/lib/libopencv_core.so.2.3.1

当然,除了所有其他 OpenCV 库。

已编辑:

请注意,我开始使用“gcc”编译器编译程序,但错误更多,例如 -

gcc `pkg-config --cflags --libs opencv` -o ipcamcap ipcamcap.c
/tmp/cc9zt8fA.o: In function `cvDecRefData':
ipcamcap.c:(.text+0x9f2): undefined reference to `cvFree_'
ipcamcap.c:(.text+0xa7a): undefined reference to `cvFree_'
/tmp/cc9zt8fA.o: In function `cvGetRow':
ipcamcap.c:(.text+0xb91): undefined reference to `cvGetRows'
/tmp/cc9zt8fA.o: In function `cvGetCol':
ipcamcap.c:(.text+0xbbf): undefined reference to `cvGetCols'
/tmp/cc9zt8fA.o: In function `cvReleaseMatND':
ipcamcap.c:(.text+0xbd9): undefined reference to `cvReleaseMat'
/tmp/cc9zt8fA.o: In function `cvSubS':
ipcamcap.c:(.text+0xd35): undefined reference to `cvAddS'
/tmp/cc9zt8fA.o: In function `cvCloneSeq':
ipcamcap.c:(.text+0xd6e): undefined reference to `cvSeqSlice'
/tmp/cc9zt8fA.o: In function `cvSetNew':
ipcamcap.c:(.text+0xddb): undefined reference to `cvSetAdd'
/tmp/cc9zt8fA.o: In function `cvGetSetElem':
ipcamcap.c:(.text+0xe84): undefined reference to `cvGetSeqElem'
/tmp/cc9zt8fA.o: In function `cvEllipseBox':
ipcamcap.c:(.text+0xf69): undefined reference to `cvEllipse'
/tmp/cc9zt8fA.o: In function `cvFont':
ipcamcap.c:(.text+0xfa6): undefined reference to `cvInitFont'
/tmp/cc9zt8fA.o: In function `cvReadIntByName':
ipcamcap.c:(.text+0x1094): undefined reference to `cvGetFileNodeByName'
/tmp/cc9zt8fA.o: In function `cvReadRealByName':
ipcamcap.c:(.text+0x1147): undefined reference to `cvGetFileNodeByName'
/tmp/cc9zt8fA.o: In function `cvReadStringByName':
ipcamcap.c:(.text+0x11bd): undefined reference to `cvGetFileNodeByName'
/tmp/cc9zt8fA.o: In function `cvReadByName':
ipcamcap.c:(.text+0x11fd): undefined reference to `cvGetFileNodeByName'
ipcamcap.c:(.text+0x1213): undefined reference to `cvRead'
/tmp/cc9zt8fA.o: In function `cvCreateSubdivDelaunay2D':
ipcamcap.c:(.text+0x124f): undefined reference to `cvCreateSubdiv2D'
ipcamcap.c:(.text+0x126a): undefined reference to `cvInitSubdivDelaunay2D'
/tmp/cc9zt8fA.o: In function `cvContourPerimeter':
ipcamcap.c:(.text+0x1442): undefined reference to `cvArcLength'
/tmp/cc9zt8fA.o: In function `cvCalcHist':
ipcamcap.c:(.text+0x1472): undefined reference to `cvCalcArrHist'
/tmp/cc9zt8fA.o: In function `main':
ipcamcap.c:(.text+0x15bb): undefined reference to `cvCreateFileCapture'
ipcamcap.c:(.text+0x15ed): undefined reference to `cvNamedWindow'
ipcamcap.c:(.text+0x15f7): undefined reference to `cvGetTickCount'
ipcamcap.c:(.text+0x160d): undefined reference to `cvQueryFrame'
ipcamcap.c:(.text+0x1616): undefined reference to `cvGetTickCount'
ipcamcap.c:(.text+0x1638): undefined reference to `cvGetTickFrequency'
ipcamcap.c:(.text+0x167e): undefined reference to `cvGetTickFrequency'
ipcamcap.c:(.text+0x16bf): undefined reference to `cvShowImage'
ipcamcap.c:(.text+0x16c9): undefined reference to `cvWaitKey'
ipcamcap.c:(.text+0x16e9): undefined reference to `cvReleaseCapture'
collect2: ld returned 1 exit status
make: *** [ipcamcap] Error 1

【问题讨论】:

  • 为什么要用c++编译器编译c文件?
  • 所有c方法都给你undefined reference错误,去C++...
  • @Banthar,我之前使用的是“gcc”,链接器错误是几个页面已满。我还尝试使用 'using namespace std;' 将 更改为 等等,将文件重命名为.cpp,当然,代码还是很C'ish。
  • 试试gcc -o ipcamcap ipcamcap.c `pkg-config --cflags --libs opencv`
  • @icarus74:是的,我的意思是这样。还要检查名称是否完全匹配(没有额外的下划线,没有 C++ 解构的名称等)

标签: c++ opencv linker


【解决方案1】:
gcc `pkg-config --cflags --libs opencv` -o ipcamcap ipcamcap.c

标志的顺序有时很重要。试试:

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

您可以here获取更多信息。

【讨论】:

  • 感谢分享参考资料,解释链接器选项的重要性及其排序的相关性。
【解决方案2】:

确保您正确包含和链接所有库文件。

我使用以下内容指定了库的搜索路径:

-L/usr/local/lib
-lopencv_core -lopencv_highgui

显然包括编译器的库:

-I/usr/local/include/opencv

并正确编译您的代码 sn-p。

【讨论】:

  • 谢谢马特奥。赞成,但我认为让我抓到的主要问题是编译器选项的顺序。
  • @icarus74 是的,这绝对是问题所在。我仍然建议传递给 OpenCV C++ 接口,它简洁易用,经验不足.. ;)
猜你喜欢
  • 2014-01-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-23
  • 2014-12-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多