首页 文章

OpenCV应用程序(不是库)链接错误

提问于
浏览
2

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

我编写了一个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 /伊卡洛斯/项目/ ipcamcap $

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

/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库 .

Edited:

请注意,我开始使用“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

2 回答

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

    旗帜的顺序有时很重要 . 尝试:

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

    您可以获得更多信息here .

  • 4

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

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

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

    显然包括编译器的库:

    -I/usr/local/include/opencv
    

    并正确编译了你的代码片段 .

相关问题