c – OpenCV架构x86_64的未定义符号:错误

在此之前被标记为重复的问题,请阅读结尾.谢谢你的看

我使用home-brew设置openCV.

这些是我使用的命令:

brew tap homebrew/science
brew install opencv

我使用以下简单的文件,我在网上找到来测试我的设置:

// Example showing how to read and write images
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;

int main(int argc, char** argv)
{   
    // Load an image from file - change this based on your image name
    Mat img = imread("my_image.jpg", CV_LOAD_IMAGE_UNCHANGED);

    if(img.empty())
    {
        fprintf(stderr, "failed to load input image\n");
        return -1;
    }

    // this is just to show, that you won't have to pre-alloc
    // result-images with c++ any more..
    Mat gray;
    cvtColor(img,gray,CV_BGR2GRAY);

    // Write the image to a file with a different name,
    // using a different image format -- .png instead of .jpg
    if( ! imwrite("my_image_copy.png", img) )
    {
        fprintf(stderr, "failed to write image file\n");
    }

    // no need to release anything with c++ !   
    return 0;
}

这是我尝试编译并运行起诉g test.cpp -o测试后遇到的错误.

Undefined symbols for architecture x86_64:
  "cv::_InputArray::_InputArray(cv::Mat const&)", referenced from:
      _main in test-41a30e.o
  "cv::namedWindow(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, int)", referenced from:
      _main in test-41a30e.o
  "cv::destroyWindow(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)", referenced from:
      _main in test-41a30e.o
  "cv::Mat::deallocate()", referenced from:
      cv::Mat::release() in test-41a30e.o
  "cv::imread(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, int)", referenced from:
      _main in test-41a30e.o
  "cv::imshow(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, cv::_InputArray const&)", referenced from:
      _main in test-41a30e.o
  "cv::waitKey(int)", referenced from:
      _main in test-41a30e.o
  "cv::fastFree(void*)", referenced from:
      cv::Mat::~Mat() in test-41a30e.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

我在堆栈溢出中发现了类似的问题,但没有使用自制软件,我不知道如何时间敏感(关于更新和更改OpenCV,我的操作系统等)的问题.我正在使用一个mac与10.9.4操作系统.这些答案对我来说并不是很清楚,应该改变什么.许多人只是建议一行代码,而不用说行应该去哪里.我是这些技术的新手.任何帮助将是非常感谢!

编辑:
马克·塞切尔(Mark Setchell)帮助了第一批错误.但我现在得到:
g $(pkg-config –cflags –libs opencv)test.cpp -o测试& ./测试

[3] 7187
dyld:库未加载:lib / libopencv_calib3d.2.4.dylib
  参考自:/usr/local/Cellar/opencv/2.4.9/include/./Test
  原因:图像未找到
[2]完成g $(pkg-config –cflags –libs opencv)test.cpp -o测试
痕迹/ BPT陷阱:5

brewdoctor给了我这个警告:
警告:您的酒窖中已取消链接桶
离开小桶不连接可能导致构建麻烦,并导致依赖的酿造
那些桶一旦建成就无法正常运行.在这些上运行brew链接:

opencv

所以我运行brew链接–overwrite opencv但得到这个:

Linking /usr/local/Cellar/opencv/2.4.9... 
Error: Could not symlink include/opencv/cv.h

需要覆盖,因为brew link opencv给了

Linking /usr/local/Cellar/opencv/2.4.9... 
Error: Could not symlink include/opencv/cv.h
Target /usr/local/include/opencv/cv.h
already exists. You may want to remove it:
  rm /usr/local/include/opencv/cv.h
您正在收到链接器错误.我认为这是因为您没有与opencv所需的正确库连接.

获取正确的库的最简单的方法是使用pkg-config,因此我建议:

brew install pkg-config

那么你可能需要重新安装opencv

brew reinstall opencv

或者可能

brew uninstall opencv
brew install opencv

那么你应该能够做到:

g++ $(pkg-config --cflags --libs opencv) test.cpp -o Test

你可以跑

pkg-config --cflags --libs opencv

看看它为g编译器输出什么,如果你有兴趣 – 它告诉编译器库和头文件的位置.在我的系统上,它输出:

pkg-config --cflags --libs opencv

-I/usr/local/Cellar/opencv/2.4.12_2/include/opencv \
-I/usr/local/Cellar/opencv/2.4.12_2/include \
-L/usr/local/Cellar/opencv/2.4.12_2/lib \
-lopencv_calib3d -lopencv_contrib -lopencv_core -lopencv_features2d -lopencv_flann -lopencv_gpu -lopencv_highgui -lopencv_imgproc -lopencv_legacy -lopencv_ml -lopencv_nonfree -lopencv_objdetect -lopencv_ocl -lopencv_photo -lopencv_stitching -lopencv_superres -lopencv_ts -lopencv_video -lopencv_videostab

如果你不熟悉pkgconfig,你可以要求它告诉你所有知道的包,如下所示:

pkg-config --list-all

我的系统给出如下输出:

libzmq                              libzmq - 0MQ c++ library
gio-unix-2.0                        GIO unix specific APIs - unix specific headers for glib I/O library
glibmm-2.4                          glibmm - C++ wrapper for GLib
libpostproc                         libpostproc - FFmpeg postprocessing library
libgsf-1                            libgsf-1 - A library for reading and writing structured files (eg MS OLE and Zip)
gobject-introspection-no-export-1.0 gobject-introspection - GObject Introspection
libtasn1                            libtasn1 - Library for ASN.1 and DER manipulation
libusb-1.0                          libusb-1.0 - C API for USB device access from Linux, Mac OS X, Windows and OpenBSD/NetBSD userspace
gio-2.0                             GIO - glib I/O library
libusb                              libusb - USB access library (libusb-1.0 compat wrapper)
libecpg_compat                      libecpg_compat - PostgreSQL libecpg_compat library
QtNetwork                           Qtnetwork - Qtnetwork Library
opencv                              OpenCV - Open Source Computer Vision Library

基本上,上面列表的每行中的第一个单词告诉您pkgconfig知道的包的名称,这是在运行命令时应该指定的名称,例如

pkg-config --cflags --libs opencv

如果您的系统有点混乱,您可以找到pkgconfig使用的.pc文件,如下所示:

find /usr/local/Cellar -name \*.pc

要么

find /usr/local/Cellar -name \*.pc | grep -i opencv

/usr/local/Cellar/opencv3/3.1.0_3/lib/pkgconfig/opencv.pc

然后直接使用配置文件,如下所示:

pkg-config --cflags --libs /usr/local/Cellar/opencv3/3.1.0_3/lib/pkgconfig/opencv.pc

如果所有这些都正确编译,可以运行它

./Test
相关文章
相关标签/搜索