在 OS X 上安装 C++ 库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34340578/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 14:21:55  来源:igfitidea点击:

Installing C++ Libraries on OS X

c++macosopencvinstallationlibraries

提问by Giesbrecht

I am trying to get my head around some basic concepts, but I can't seem to figure them out.

我试图了解一些基本概念,但我似乎无法弄清楚它们。

I am really confused over what it means to install (I think they are called libraries) for C++. I am trying to install OpenCV, but I don't know what needs to happen for it to be installed, how to check, or what really OpenCV is (Is it a library, framework, something else?).

我真的很困惑为 C++ 安装(我认为它们被称为库)意味着什么。我正在尝试安装 OpenCV,但我不知道安装它需要什么,如何检查,或者真正的 OpenCV 是什么(它是一个库、框架还是其他东西?)。

My understanding is that OpenCV (and other libraries/frameworks) is distributed as only the source code so that is is able to work cross-platform. Then, after you download it, you have to build it (I don't know what build means though), and then link your compiler against it so that it can access the files? I don't know how any of this would be done, or really what this means. I think a lot of this is done by CMake, but I don't know what CMake really does, how you would use it, or how you would then utilize the library/framework in your code. Also, where would the libraries like OpenCV be installed, how would clang (or any other compiler/linker) know where to find them, and what kind of files would they be (.a, .dylib, .cpp, .hpp, executables, or a collection of everything)? Is this structure specific to C++ and OS X or is it more widespread?

我的理解是 OpenCV(和其他库/框架)仅作为源代码分发,因此能够跨平台工作。然后,在你下载它之后,你必须构建它(虽然我不知道构建是什么意思),然后将你的编译器链接到它以便它可以访问文件?我不知道这将如何完成,或者这意味着什么。我认为其中很多都是由 CMake 完成的,但我不知道 CMake 真正做了什么,你将如何使用它,或者你将如何在代码中使用库/框架。此外,像 OpenCV 这样的库将安装在哪里,clang(或任何其他编译器/链接器)如何知道在哪里可以找到它们,以及它们将是什么类型的文件(.a、.dylib、.cpp、.hpp、可执行文件, 或所有东西的集合)?这种结构是特定于 C++ 和 OS X 的还是更普遍的?

I am not looking for a tutorial on how to install OpenCV or other libraries, but I am instead trying to learn how that actually works so I won't need tutorials in the future.

我不是在寻找关于如何安装 OpenCV 或其他库的教程,而是在尝试了解它的实际工作原理,这样我将来就不需要教程了。

回答by Mark Setchell

Before you can do any C/C++ development work on a Mac, you need to go to the App Store and download Xcodefor free - it is Apple's IDE - Integrated Development Environment. Without Xcode, you will have no compiler (i.e. clangor gccor g++) and no build tools, (i.e. make).

在 Mac 上进行任何 C/C++ 开发工作之前,你需要去 App StoreXcode免费下载——它是 Apple 的 IDE——集成开发环境。没有Xcode,您将没有编译器(即clanggccg++),也没有构建工具(即make)。

Install Xcode

安装 Xcode

If you are totally new to Mac, App Storelooks like this:

如果您完全不熟悉 Mac,则App Store如下所示:

enter image description here

在此处输入图片说明

and Xcodelooks like this:

Xcode看起来像这样:

enter image description here

在此处输入图片说明

Install Command Line Tools

安装命令行工具

Next you must install Xcode's command-line tools, so start a Terminal - by pressing +SPACEand starting to type Terminaland when it guesses correctly, just hit Enter/Return. Copy and paste the following into Terminal and hit Enter/Return.

接下来,您必须安装 Xcode 的命令行工具,因此启动终端 - 通过按+SPACE并开始键入Terminal,当它正确猜测时,只需点击Enter/Return。将以下内容复制并粘贴到终端中并点击Enter/Return

xcode-select --install

The above is called a "Spotlight Search"and is the easiest way to find anything on a Mac.

以上称为“Spotlight 搜索”,是在 Mac 上查找任何内容的最简单方法。

Install homebrew

安装自制软件

Then, if you want to install OpenCVon a Mac, install a package manager such as homebrewwhich is a matter of copying and pasting a single line from the homebrew websiteinto your Terminal. I will not show the line here in case it ever changes and someone looks at this in a few years, but it is easy to see if you go to the link above.

然后,如果您想OpenCV在 Mac 上安装,请安装一个包管理器,例如homebrew自制网站上的一行复制并粘贴到您的终端中。我不会在这里显示这条线,以防它发生变化并且有人在几年内查看它,但是如果您转到上面的链接,很容易看到。

Find Packages

查找包

Then you can find any packages you want with:

然后你可以找到任何你想要的包:

brew search opencv    # Look for packages called "opencv"

or

或者

brew search boost     # Look for "boost" libraries

Install OpenCV

安装 OpenCV

So, for a vanilla (no special options) installation and build of OpenCVdo this:

因此,对于香草(无特殊选项)安装和构建,OpenCV请执行以下操作:

brew install opencv

Remove Packages

删除包

You can later remove any packages you no longer want with:

您可以稍后删除不再需要的任何包:

brew rm opencv

Update Packages

更新包

You can also update all installed packages with:

您还可以使用以下命令更新所有已安装的软件包:

brew update && brew upgrade && brew cleanup

Build a Project

构建项目

Once you have got it installed, you can start compiling and building your own project. It helps if you use the pkg-configpackage to pick up all the necessary compiler/linker settings you need, so I would suggest:

一旦你安装了它,你就可以开始编译和构建你自己的项目。如果您使用该pkg-config包来获取您需要的所有必要的编译器/链接器设置,它会有所帮助,所以我建议:

brew install pkg-config

Now you can compile and link with a really simple command like:

现在您可以使用一个非常简单的命令进行编译和链接,例如:

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

Then you can go on to use XcodeIDE later if you want to once you get started.

然后,您可以在开始后继续使用XcodeIDE。

Build with Xcode

使用 Xcode 构建

Once you have got started with basic compilation, you may want to start using Xcodeto edit your programs, to do that, you must tell Xcodewhere the header files are and also where the libraries are and which libraries to link. This will vary with your OpenCV version, but you will need to alter the places marked in the two diagrams below. You will find these easily if you click them in order - green area first, then the yellow, then the blue, then the red.

一旦你开始了基本的编译,你可能想开始使用Xcode编辑你的程序,为此,你必须知道Xcode头文件在哪里,库在哪里以及链接哪些库。这将因您的 OpenCV 版本而异,但您需要更改下两个图中标记的位置。如果您按顺序单击它们,您会很容易找到它们 - 首先是绿色区域,然后是黄色区域,然后是蓝色区域,然后是红色区域。

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明

The actual information that will need to go in the Xcode settings areas I have marked above can be found by running the same pkg-configcommand I suggested in the previous section. So run:

通过运行pkg-config我在上一节中建议的相同命令,可以找到需要进入我在上面标记的 Xcode 设置区域中的实际信息。所以运行:

pkg-config --cflags opencv

to get the location of the header (include) files, and then run

获取头(包含)文件的位置,然后运行

pkg-config --libs opencv

to get the information you need to fill in for the linker in Xcode.

获取您需要为Xcode.