在此之前,如果您认为这是一个重复的问题,请阅读结尾。感谢您的关注。
我使用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 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)
我在stackoverflow上找到类似的问题,但没有使用homebrew,而且我不确定问题有多么紧急(关于OpenCV、我的操作系统等更新和更改)。我正在使用运行OS 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] Done g++ $(pkg-config --cflags --libs opencv) test.cpp -o Test Trace/BPT trap: 5
brewdoctor给出了以下警告:
警告:您在Cellar中有未链接的kegs。
留下未链接的kegs可能会导致构建问题,并使依赖于这些kegs的软件无法正确运行。在这些kegs上运行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