C++ 架构 x86_64 的 OpenCV 未定义符号:错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24985713/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
OpenCV Undefined symbols for architecture x86_64: error
提问by Bren
before this gets flagged as a repeat question please read the end. Thanks for looking.
在这被标记为重复问题之前,请阅读结尾。谢谢你看。
I set up openCV using home-brew.
我使用自制软件设置了 openCV。
These were the commands I used:
这些是我使用的命令:
brew tap homebrew/science
brew install opencv
I am using the following simple file I found online to test my set up:
我正在使用我在网上找到的以下简单文件来测试我的设置:
// 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;
}
This is the error I am getting after trying to compile and run suing g++ test.cpp -o test
.
这是我在尝试编译和运行 suing 后遇到的错误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)
I did find similar issues on stack overflow but none using homebrew and I'm not sure how time- sensitive (with regards to updates and changes to OpenCV, my OS etc) the problem is. I'm using a mac with the 10.9.4 OS. Also those answers weren't really clear to me what should be changed. Many just suggested one line of code without saying where the line should go. I'm extremely novice with these technologies. Any help would be great thanks!
我确实在堆栈溢出中发现了类似的问题,但没有使用自制软件,而且我不确定问题的时间敏感性(关于 OpenCV、我的操作系统等的更新和更改)。我使用的是 10.9.4 操作系统的 Mac。这些答案对我来说也不是很清楚应该改变什么。许多人只是建议了一行代码,而没有说明该行应该去哪里。我对这些技术非常陌生。任何帮助都会非常感谢!
Edit: Mark Setchell helped with the first batch of errors. But I am now getting: g++ $(pkg-config --cflags --libs opencv) test.cpp -o Test & ./Test
编辑:Mark Setchell 帮助解决了第一批错误。但我现在得到: g++ $(pkg-config --cflags --libs opencv) test.cpp -o Test & ./Test
[3] 7187 dyld: Library not loaded: lib/libopencv_calib3d.2.4.dylib Referenced from: /usr/local/Cellar/opencv/2.4.9/include/./Test Reason: image not found [2] Done g++ $(pkg-config --cflags --libs opencv) test.cpp -o Test Trace/BPT trap: 5
[3] 7187 dyld:库未加载:lib/libopencv_calib3d.2.4.dylib 引用自:/usr/local/Cellar/opencv/2.4.9/include/./Test Reason: image not found [2] Done g++ $( pkg-config --cflags --libs opencv) test.cpp -o 测试跟踪/BPT 陷阱:5
brewdoctor gave me this warning:
Warning: You have unlinked kegs in your Cellar
Leaving kegs unlinked can lead to build-trouble and cause brews that depend on
those kegs to fail to run properly once built. Run brew link
on these:
brewdoctor 给了我这个警告:警告:您的酒窖中的酒桶未链接 将酒桶未链接会导致构建问题,并导致依赖于这些酒桶的啤酒在构建后无法正常运行。运行brew link
这些:
opencv
So I ran brew link --overwrite opencv but got this:
所以我运行了 brew link --overwrite opencv 但得到了这个:
Linking /usr/local/Cellar/opencv/2.4.9...
Error: Could not symlink include/opencv/cv.h
needed the overwrite because brew link opencv gave
需要覆盖,因为 brew 链接 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
回答by Mark Setchell
You are getting linker errors. I think that is because you are not linking with the correct libraries that opencv
requires.
您收到链接器错误。我认为这是因为您没有链接到所需的正确库opencv
。
The easiest way to get the correct libraries is with pkg-config
, so I would recommend:
获得正确库的最简单方法是使用pkg-config
,因此我建议:
brew install pkg-config
Then you may have to reinstall opencv
那么你可能需要重新安装opencv
brew reinstall opencv
or maybe
或者可能
brew uninstall opencv
brew install opencv
Then you should be able to do:
那么你应该能够做到:
g++ $(pkg-config --cflags --libs opencv) test.cpp -o Test
You can run
你可以跑
pkg-config --cflags --libs opencv4
to see what it outputs for the g++
compiler if you are interested - it tells the compiler where the libraries and header files are. On my system, it outputs this:
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
If you are unfamiliar with pkgconfig
, you can ask it to tell you all the packages it knows about like this:
如果你不熟悉pkgconfig
,你可以让它告诉你它知道的所有包,如下所示:
pkg-config --list-all
My system gives output like this:
我的系统给出这样的输出:
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
Basically, the first word in each line of the list above tells you the names of the packages pkgconfig
knows about and that is the name you should specify when running commands such as
基本上,上面列表中每一行的第一个词告诉你pkgconfig
知道的包的名称,这是你在运行命令时应该指定的名称,例如
pkg-config --cflags --libs opencv4
If your system is a bit messed up, you can find the .pc
file that pkgconfig
uses like this:
如果你的系统有点乱,你可以找到.pc
这样pkgconfig
使用的文件:
find /usr/local/Cellar -name \*.pc
or
或者
find /usr/local/Cellar -name \*.pc | grep -i opencv
/usr/local/Cellar/opencv3/3.1.0_3/lib/pkgconfig/opencv.pc
And then use the config file directly, like this:
然后直接使用配置文件,像这样:
pkg-config --cflags --libs /usr/local/Cellar/opencv3/3.1.0_3/lib/pkgconfig/opencv.pc
If it all compiles correctly, you can run it with
如果一切都正确编译,您可以运行它
./test
回答by Kevin Katzke
You are getting these errors because you are using OpenCVmethods in your code that are not linked to your project.
您收到这些错误是因为您在代码中使用了未链接到您的项目的OpenCV方法。
So far you have only included:
到目前为止,您只包括:
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
But to run your code you need far more then these two OpenCVlibraries. For example let's take the following error when trying to Build your project in XCODE:
但是要运行您的代码,您需要的远不止这两个OpenCV库。例如,让我们尝试在 XCODE 中构建项目时出现以下错误:
Undefined symbols for architecture x86_64:
"cv::imread(cv::String const&, int)", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
which corresponds to this part of your error:
这对应于您错误的这一部分:
... "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 ...
This occurs because XCODE is looking to execute the following function in your code:
发生这种情况是因为 XCODE 希望在您的代码中执行以下函数:
Mat img = imread("my_image.jpg", CV_LOAD_IMAGE_UNCHANGED);
but the way you've implemented this, it doesn't know what imread()is, because the reference to the opencvlibrary is missing! the Library that solves this particular problem is:
但是你实现这个的方式,它不知道imread()是什么,因为缺少对opencv库的引用!解决这个特定问题的库是:
libopencv_imgcodecs.3.1.0.dylib
libopencv_imgcodecs.3.1.0.dylib
(Of course the version depends on whatever version of OpenCVyou are using.)
(当然,版本取决于您使用的OpenCV版本。)
To resolve this particular error you have to do 2 steps:
要解决此特定错误,您必须执行 2 个步骤:
- Add the library to your project: Right-click on your project file. Click on "Add files to blabla" and then browse to your OpenCV directory. The libraries should be located in
- 将库添加到您的项目:右键单击您的项目文件。单击“将文件添加到 blabla”,然后浏览到您的 OpenCV 目录。这些库应该位于
/Users/YOURNAME/YOURFOLDER/opencv-3.1.0/build/lib
/Users/YOURNAME/YOURFOLDER/opencv-3.1.0/build/lib
Caution! This could depend on how you've installed OpenCV! If you have installed it using Homebrew.The directory shoulb be here:
警告!这可能取决于您如何安装 OpenCV!如果你已经使用 Homebrew 安装了它。目录应该在这里:
/usr/local/Cellar/opencv/2.4.9/lib
Select the following library (libopencv_imgcodecs.3.1.0.dylib) and add it to your project:
选择以下库 (libopencv_imgcodecs.3.1.0.dylib) 并将其添加到您的项目中:
After you have added the Library to your Project it should appear in the list on the left. Double Check that you've picked the right library. Some of them have quite similar names!
将库添加到项目后,它应该出现在左侧的列表中。仔细检查您是否选择了正确的库。其中一些名字非常相似!
- Include the Library in your C++ File
- 在 C++ 文件中包含库
As you've done it with:
正如你所做的那样:
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
You should now go on and include the new Library as well:
您现在应该继续并包含新库:
#include "opencv2/imgcodecs/imgcodecs.hpp"
- Run your program
- 运行你的程序
When you build your program again, you should have one error less. And XCODE shouldn't have a problem to run cv::imread.
当你再次构建你的程序时,你应该少一个错误。XCODE 运行cv::imread应该没有问题 。
As you have plenty of errors, you should now go on by checking which libraries you need to successfully build your program.
由于您有很多错误,您现在应该继续检查成功构建程序所需的库。
I would recommend to Link these libraries as they are very common:
我建议链接这些库,因为它们很常见:
#include <iostream>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/core/core.hpp"
#include "opencv2/video/video.hpp"
#include "opencv2/imgcodecs/imgcodecs.hpp"
Before going crazy over all the errors you could just link all the libraries and see if it builds correctly.
在对所有错误发疯之前,您可以链接所有库并查看它是否正确构建。
回答by bmsmith
My problem was that brew compiled and linked with Xcode, and I was using a different version of g++ from macports. I just added the Xcode g++ location to my $PATH
variable in my .bash_profile
file and unlinked the wrong g++ binary with sudo port select --set gcc none
. You can find the Xcode g++ location by running xcodebuild -find g++
, mine was /Applications/Xcode.app/Contents/Developer/usr/bin/g++
.
我的问题是 brew 编译并与 Xcode 链接,我使用的是与 macports 不同版本的 g++。我刚刚$PATH
在我的.bash_profile
文件中将 Xcode g++ 位置添加到我的变量中,并将错误的 g++ 二进制文件与sudo port select --set gcc none
. 您可以通过运行找到 Xcode g++ 位置xcodebuild -find g++
,我的是/Applications/Xcode.app/Contents/Developer/usr/bin/g++
.