将C可执行文件链接到OpenCV时出错

时间:2013-11-06 15:51:51

标签: opencv linker

我正使用OpenCVUbuntu 13.10下编译cMake

我已经编译了c ++程序,但效果很好。

现在我正在尝试使用此cMakeLists.txt

编译C文件
cmake_minimum_required (VERSION 2.8)
project (hello)
find_package (OpenCV REQUIRED)
add_executable (hello src/test.c)
target_link_libraries (hello ${OpenCV_LIBS})

这是test.c文件:

#include <stdio.h>
#include <stdlib.h>
#include <opencv/highgui.h>

int main (int argc, char* argv[])
{
  IplImage* img = NULL; 
  const char* window_title = "Hello, OpenCV!";

  if (argc < 2)
  {
    fprintf (stderr, "usage: %s IMAGE\n", argv[0]);
    return EXIT_FAILURE;
  }
  img = cvLoadImage(argv[1], CV_LOAD_IMAGE_UNCHANGED);

  if (img == NULL)
  {
    fprintf (stderr, "couldn't open image file: %s\n", argv[1]);
    return EXIT_FAILURE;
  }
  cvNamedWindow (window_title, CV_WINDOW_AUTOSIZE);
  cvShowImage (window_title, img);
  cvWaitKey(0);
  cvDestroyAllWindows();
  cvReleaseImage(&img);

  return EXIT_SUCCESS;
}

当项目运行cmake .然后make时,它会返回此错误:

Linking C executable hello
/usr/bin/ld: CMakeFiles/hello.dir/src/test.c.o: undefined reference to symbol «lrint@@GLIBC_2.1»
/lib/i386-linux-gnu/libm.so.6: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
make[2]: *** [hello] Erreur 1
make[1]: *** [CMakeFiles/hello.dir/all] Erreur 2
make: *** [all] Erreur 2

2 个答案:

答案 0 :(得分:0)

-lm /lib/i386-linux-gnu/libm.so.6添加到您的链接库

$ gcc `pkg-config --cflags opencv` -o <output> <input> `pkg-config --libs opencv` -lm /lib/i386-linux-gnu/libm.so.6

我遇到了同样的错误,并且与我合作

答案 1 :(得分:-1)

按如下方式修改CMakeLists.txt

cmake_minimum_required(VERSION 2.8)
project(test)
find_package( OpenCV REQUIRED )
add_executable( test test.c )
target_link_libraries( test ${OpenCV_LIBS} )

然后,$ cmake . 然后是$ make,最后是$ ./test