在 OS X 上安装 C++ 库
我试图了解一些基本概念,但我似乎无法弄清楚它们.
I am trying to get my head around some basic concepts, but I can't seem to figure them out.
我真的很困惑为 C++ 安装(我认为它们被称为库)意味着什么.我正在尝试安装 OpenCV,但我不知道要安装它需要做什么、如何检查或真正的 OpenCV 是什么(它是一个库、框架还是其他东西?).
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?).
我的理解是 OpenCV(和其他库/框架)仅作为源代码分发,因此能够跨平台工作.然后,在你下载它之后,你必须构建它(虽然我不知道构建是什么意思),然后将你的编译器链接到它以便它可以访问文件?我不知道这将如何完成,或者这意味着什么.我认为其中很多都是由 CMake 完成的,但我不知道 CMake 真正做了什么,你将如何使用它,或者你将如何在代码中利用库/框架.此外,像 OpenCV 这样的库将安装在哪里,clang(或任何其他编译器/链接器)如何知道在哪里可以找到它们,以及它们将是什么类型的文件(.a、.dylib、.cpp、.hpp、可执行文件,或所有内容的集合)?这种结构是特定于 C++ 和 OS X 的还是更普遍的?
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 或其他库的教程,而是在尝试了解它的实际工作原理,这样我以后就不需要教程了.
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.
推荐答案
在你可以在 Mac 上进行任何 C/C++ 开发工作之前,你需要去 App Store 下载 Xcode
for免费 - 它是 Apple 的 IDE - 集成开发环境.如果没有 Xcode
,您将没有编译器(即 clang
或 gcc
或 g++
),也没有构建工具,(即 make
).
Before you can do any C/C++ development work on a Mac, you need to go to the App Store and download Xcode
for free - it is Apple's IDE - Integrated Development Environment. Without Xcode
, you will have no compiler (i.e. clang
or gcc
or g++
) and no build tools, (i.e. make
).
安装 Xcode
如果您是 Mac 新手,App Store
看起来像这样:
If you are totally new to Mac, App Store
looks like this:
和 Xcode
看起来像这样:
and Xcode
looks like this:
安装命令行工具
接下来您必须安装 Xcode 的命令行工具,因此启动终端 - 通过按 ?+SPACE 并开始键入 Terminal
和当它猜对了时,只需点击 Enter/Return.将以下内容复制并粘贴到终端中,然后点击 Enter/Return.
Next you must install Xcode's command-line tools, so start a Terminal - by pressing ⌘+SPACE and starting to type Terminal
and when it guesses correctly, just hit Enter/Return. Copy and paste the following into Terminal and hit Enter/Return.
xcode-select --install
以上称为Spotlight Search",是在 Mac 上查找任何内容的最简单方法.
The above is called a "Spotlight Search" and is the easiest way to find anything on a Mac.
安装自制软件
然后,如果您想在 Mac 上安装 OpenCV
,请安装包管理器,例如 homebrew
,只需从 homebrew
复制和粘贴一行即可一个 href="http://brew.sh" rel="noreferrer">自制网站 进入您的终端.我不会在这里显示这条线,以防它发生变化并且几年后有人会看到它,但是如果您转到上面的链接,很容易看到.
Then, if you want to install OpenCV
on a Mac, install a package manager such as homebrew
which is a matter of copying and pasting a single line from the homebrew website into 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.
查找包裹
然后你可以找到任何你想要的包:
Then you can find any packages you want with:
brew search opencv # Look for packages called "opencv"
或
brew search boost # Look for "boost" libraries
安装 OpenCV
因此,对于 OpenCV
的普通(无特殊选项)安装和构建,请执行以下操作:
So, for a vanilla (no special options) installation and build of OpenCV
do this:
brew install opencv
删除包
您可以稍后删除不再需要的任何包:
You can later remove any packages you no longer want with:
brew rm opencv
更新包
您还可以使用以下命令更新所有已安装的软件包:
You can also update all installed packages with:
brew update && brew upgrade && brew cleanup
构建项目
安装完成后,您就可以开始编译和构建您自己的项目了.如果您使用 pkg-config
包来获取您需要的所有必要的编译器/链接器设置,它会有所帮助,所以我建议:
Once you have got it installed, you can start compiling and building your own project. It helps if you use the pkg-config
package to pick up all the necessary compiler/linker settings you need, so I would suggest:
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
然后你可以在以后继续使用 Xcode
IDE,如果你开始的话.
Then you can go on to use Xcode
IDE later if you want to once you get started.
使用 Xcode 构建
一旦你开始了基本的编译,你可能想开始使用 Xcode
来编辑你的程序,为此,你必须告诉 Xcode
头文件在哪里以及库所在的位置以及要链接的库.这将因您的 OpenCV 版本而异,但您需要更改下两个图中标记的位置.如果您按顺序单击它们,您会很容易找到它们 - 首先是绿色区域,然后是黄色区域,然后是蓝色区域,然后是红色区域.
Once you have got started with basic compilation, you may want to start using Xcode
to edit your programs, to do that, you must tell Xcode
where 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 设置区域的实际信息可以通过运行我在上一节中建议的相同的 pkg-config
命令找到.所以运行:
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-config
command I suggested in the previous section. So run:
pkg-config --cflags opencv
获取头(包含)文件的位置,然后运行
to get the location of the header (include) files, and then run
pkg-config --libs opencv
获取您需要为Xcode
中的链接器填写的信息.
to get the information you need to fill in for the linker in Xcode
.
相关文章