在此问题被标记为重复问题之前,请阅读结尾。感谢观看。
我使用 Homebrew 软件设置了 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 test
后遇到的错误。
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)
我确实在堆栈溢出方面发现了类似的问题,但没有使用 Homebrew 软件,而且我不确定问题的时间敏感性(关于 OpenCV、我的操作系统等的更新和更改)。我正在使用带有 10.9.4 操作系统的 mac。这些答案对我来说也不是很清楚应该改变什么。许多人只是建议了一行代码,而没有说明该行应该去哪里。我对这些技术非常陌生。任何帮助都会非常感谢!
编辑: Mark Setchell 帮助解决了第一批错误。但我现在得到: g++ $(pkg-config --cflags --libs opencv) test.cpp -o Test & ./Test
[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 link
:
opencv
所以我运行了 brew link --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 opencv4
如果您有兴趣查看它为 g++
编译器输出的内容 - 它会告诉编译器库和头文件在哪里。在我的系统上,它输出:
pkg-config --cflags --libs opencv4
-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
opencv4 OpenCV - Open Source Computer Vision Library
基本上,上面列表中每一行的第一个单词告诉你pkgconfig
知道的包的名称,这是你在运行诸如
pkg-config --cflags --libs opencv4
如果你的系统有点乱,你可以像这样找到 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
关于c++ - OpenCV 架构 x86_64 的 undefined symbol : error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24985713/