問題描述
我試圖了解一些基本概念,但我似乎無法弄清楚它們.
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
復制和粘貼一行即可一個 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
.
這篇關于在 OS X 上安裝 C++ 庫的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!