在Mac OS X Sierra上使用OpenCV C ++编译Hello World [英] Compiling hello world with opencv c++ on mac os x sierra

查看:138
本文介绍了在Mac OS X Sierra上使用OpenCV C ++编译Hello World的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我第一次无法编译hello world. 我已经阅读了大量教程,介绍如何安装opencv. 我有以下示例:

First time I cannot compile hello world. I've followed tons of tutorials how to install opencv. I just have following example:

#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

using namespace std;
using namespace cv;

int main()
{

    Mat imageMe = imread("test.jpg", 0);
    return 0;
}

来自opencv网站. 看起来很简单,但是不会编译.

from opencv website. Looks straightforward, but it won't compile.

g++ display_image.cpp `pkg-config --cflags --libs opencv` -l

这是错误:

display_image.cpp:2:10: fatal error: 'opencv2/core/core.hpp' file not found
#include <opencv2/core/core.hpp>
         ^
1 error generated.

正在运行:

pkg-config --cflags --libs opencv 

返回此:

-I/usr/local/Cellar/opencv3/3.2.0/include/opencv -I/usr/local/Cellar/opencv3/3.2.0/include/opencv2 -L/usr/local/Cellar/opencv3/3.2.0/lib -lopencv_calib3d -lopencv_imgproc -lopencv_contrib -lopencv_legacy -l 

这些文件存在.

运行ls /usr/local/Cellar/opencv3/3.2.0/include/opencv2/core | grep core.hpp 得到结果:

core.hpp

出什么问题了?

我一直在使用以下教程在计算机上安装opencv:

edit: I've been using following tutorials to install opencv on my machine:

https://www. Learnopencv.com/install-opencv-3-on-yosemite-osx-10-10-x/

https: //blogs.wcode.org/2014/10/howto-install-build-and-use-opencv-macosx-10-10/

http://seeb0h. github.io/howto/howto-install-homebrew-python-opencv-osx-el-capitan/

即使在所有这些答案之后,我也无法编译单个应用程序.即使可以某种方式找到直接包含在我的文件中的.hpp文件,也无法找到包含在其中的.hp文件.

Even after all these answers I cannot compile a single app. Even if it somehow can find .hpp file directly included in my file it fails finding includes inside.

可能我的配置错误. 在/usr/local/Cellar/opencv3中,我有2个目录: 3.2.0-可能已安装的二进制文件 HEAD-9053839-从源代码编译

Probably my configuration is wrong. In /usr/local/Cellar/opencv3 I have 2 directories: 3.2.0 - probably installed binaries HEAD-9053839 - compiled from source

现在我在/usr/local/lib/pkgconfig中也有opencv.pc文件:

Now I also have opencv.pc file located in /usr/local/lib/pkgconfig:

prefix=/usr/local/Cellar/opencv3/3.2.0       
exec_prefix=${prefix}
includedir=${prefix}/include
libdir=${exec_prefix}/lib

Name: opencv
Description: The opencv library
Version: 2.x.x
Cflags: -I${includedir}/opencv -I${includedir}/opencv2
Libs: -L${libdir} -lopencv_calib3d -lopencv_imgproc -lopencv_contrib -lopencv_legacy -l

作为前缀,我也尝试过HEAD-9053839.

As a prefix I've also tried HEAD-9053839.

我创建了一个更简单的示例:

I've created even simplier example:

#include <opencv2/opencv.hpp>
int main(){return 0; }

它仍然告诉我找不到"opencv2/opencv.hpp"文件. 然后我打印了pkg-config的内容: pkg-config --cflags --libs opencv

It's still telling me that 'opencv2/opencv.hpp' file not found. Then I printed what pkg-config says: pkg-config --cflags --libs opencv

-I/usr/local/Cellar/opencv3/3.2.0/include/opencv -I/usr/local/Cellar/opencv3/3.2.0/include/opencv2 -L/usr/local/Cellar/opencv3/3.2.0/lib -lopencv_calib3d -lopencv_imgproc -lopencv_contrib -lopencv_legacy -l 

并且发现它应该直接包含dir,而不是opencv/opencv2子目录.

And found out it'd rather should be directly include dir, not opencv/opencv2 subdirectories.

所以我已经像这样运行过g ++:

So I've ran g++ like this:

g++ -I/usr/local/Cellar/opencv3/3.2.0/include -L/usr/local/Cellar/opencv3/3.2.0/lib -lopencv_calib3d -lopencv_imgproc -lopencv_contrib -lopencv_legacy -l display_image.cpp

然后它告诉我:

ld: library not found for -lopencv_contrib

删除后说:

ld: library not found for -lopencv_legacy

然后我必须在display_image.cpp之前删除-l,但是然后出现另一个错误:

Then I had to remove -l before display_image.cpp, but then I've got another error:

Undefined symbols for architecture x86_64:
  "cv::String::deallocate()", referenced from:
      cv::String::~String() in display_image-9d8f86.o
      cv::String::operator=(cv::String const&) in display_image-9d8f86.o

这是地狱.

推荐答案

我没有用于重新生成错误的编译环境,但我认为问题与代码中的以下几行有关:

I don't have a compilation environment to regenerate your error but I think the problem is about these lines in your code:

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

因为从中搜索了opencv目录

since the opencv directory is being searched from

/usr/local/Cellar/opencv3/3.2.0/include/opencv2

就像您的"pkg-config --cflags --libs opencv"所暗示的那样.因此,我想将include行更改为

as your "pkg-config --cflags --libs opencv" implies. So, I suppose, changing the include lines into

#include <core/core.hpp>
#include <highgui/highgui.hpp>

会有所帮助,或者您可以像这样隐式声明要搜索的目录:

would help, or you can implicitly declare the directory you want to search like this:

g++ -I/usr/local/Cellar/opencv3/3.2.0/include display_image.cpp ...

这篇关于在Mac OS X Sierra上使用OpenCV C ++编译Hello World的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆