实践篇:Install Homebrew and Boost Library 以及 Xcode配置Boost库
Posted 小威威__
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了实践篇:Install Homebrew and Boost Library 以及 Xcode配置Boost库相关的知识,希望对你有一定的参考价值。
为什么写这篇文章?
1.在安装Homebrew的时候,我去网上找过相关资料,不过都是千篇一律,都是copy来copy去的。我按照了其中几篇博文的方法,却出现了莫名其妙的问题。于是,我写了这篇文章,来帮助与我遇到同样问题的童鞋!
2.在安装boost后,xcode的配置boost库是个大问题,网上没有较为详细教程。
1.Homebrew
“Homebrew installs the stuff you need that Apple didn’t.”——Homebrew
Homebrew作为OS X上强大的包管理器,为系统软件提供了非常方便的安装方式,独特式的解决了包的依赖问题,并不再需要烦人的sudo,一键式编译,无参数困扰。 —@李惟
安装方法:
我强烈推荐上Homebrew官网去下载,网上博客给出的链接:一是不安全,二是不稳定,可能会导致homebrew装偏(我遇到的问题)。
(以下内容截至本人终端,内容可能会因机而异, 但指令一般都是一样的)
用ruby指令安装,一般OS X自带ruby:
➜ ~ ruby --version
ruby 2.0.0p481 (2014-05-08 revision 45883) [universal.x86_64-darwin14]
将下面的指令复制到终端运行:
// 链接1
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
// 链接2
ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)
正常情况下是能够安装成功的,用brew语句安装你想安装的东西试试,是否能正常运行:
举个例子, 我下载boost库。
➜ cd /usr/local
➜ local brew install boost
如果正常的话,应当是:
➜ local brew install boost
==> Downloading https://homebrew.bintray.com/bottles/boost-1.60.0_2.yosemite.bottle.1.tar.gz
######################################################################## 100.0%
==> Pouring boost-1.60.0_2.yosemite.bottle.1.tar.gz
/usr/local/Cellar/boost/1.60.0_2: 11,139 files, 436.8M
如果不正常,会出现下面的情况:
➜ local brew install boost
fatal: could not create leading directories of '/usr/local/Library/Taps/homebrew/homebrew-core': Permission denied
Error: Failure while executing: git clone https://github.com/Homebrew/homebrew-core /usr/local/Library/Taps/homebrew/homebrew-core --config core.autocrlf=false --depth=1 -q
Error: Failure while executing: /usr/local/bin/brew tap homebrew/core -q
说明你的homebrew装偏了,具体原因我也说不出来,反正就是装失败了,这种情况下该怎么解决呢?
Of course, 进行修复:
在终端中输入下列语句:
➜ local sudo chown -R apple:staff *
➜ local brew doctor
然后就出现:
Please note that these warnings are just used to help the Homebrew maintainers
with debugging if you file an issue. If everything you use Homebrew for is
working fine: please don't worry and just ignore them. Thanks!
Warning: Python is installed at /Library/Frameworks/Python.framework
Homebrew only supports building against the System-provided Python or a
brewed Python. In particular, Pythons installed to /Library can interfere
with other software installs.
... // 这部分内容不同电脑是不同的
待指令执行结束,homebrew也就修复好了,试着安装你想安装的软件吧~
检测homebrew是否安装:
➜ ~ cd /usr/local
➜ local ls
CODEOFCONDUCT.md LICENSE.txt README.md homebrew lib share
Cellar Library bin include opt
找到homebrew说明安装成功。
homebrew 常用操作:
brew install xxx ——安装xxx
brew uninstall xxx ——卸载xxx
brew update ——更新package
brew doctor ——修复brew
详细操作如下:
➜ ~ brew
Example usage:
brew [info | home | options ] [FORMULA...]
brew install FORMULA...
brew uninstall FORMULA...
brew search [foo]
brew list [FORMULA...]
brew update
brew upgrade [FORMULA...]
brew pin/unpin [FORMULA...]
Troubleshooting:
brew doctor
brew install -vd FORMULA
brew [--env | config]
Brewing:
brew create [URL [--no-fetch]]
brew edit [FORMULA...]
https://github.com/Homebrew/homebrew/blob/master/share/doc/homebrew/Formula-Cookbook.md
Further help:
man brew
brew home
2.Boost库的安装
首先,贴出我在网上找到的方法:(不推荐,反正我是失败了!!!)
(新手看看就好,最好不要试,如果你成功了,求指点~)
安装Boost C++ Libraries.安装步骤如下:
1.在www.boost.org下载最新的Boost C++ Libraries,目前为55版本 http://jaist.dl.sourceforge.net/project/boost/boost/1.55.0/boost_1_55_0.tar.gz
2.解压后,cd boost_155_0 目录;
3../bootstrap.sh,默认的位置是在usr/local下面;
4../b2 install,进行安装;
大约需要40分钟。
最好使用最高权限来进行安装
在官网下载压缩包然后在本地解压,运行相关指令安装。起初在没用brew的情况下我也是用这种方法进行安装的。也许是我没有开到最高权限,导致安装后只找到头文件,没有找到library。有冒险精神的童鞋可以试试用sudo试着安装。还是那句话,如果成功,求指导~
下面的方法便是我极力推荐的: 用brew安装
(如果你没有安装过homebrew, 那赶紧按照我上面的方法安装吧~)
如果之前安装过boost,不过出现了和我一样的情况(只有Header而没有Library),那么首先需要将原来安装的boost移除:
➜ local rm -r /usr/local/include/boost
然后执行安装指令:
➜ local brew install boost
==> Downloading https://homebrew.bintray.com/bottles/boost-1.60.0_2.yosemite.bottle.1.tar.gz
######################################################################## 100.0%
==> Pouring boost-1.60.0_2.yosemite.bottle.1.tar.gz
/usr/local/Cellar/boost/1.60.0_2: 11,139 files, 436.8M
指令结束后,boost库也就安装完成了。
检测boost库是否安装成功:
(1)Header:
➜ local cd include
➜ include ls
boost
boost的头文件已安装;
(2)Library:
➜ ~ cd /usr/local/lib
➜ lib ls
libACE-5.6.9.dylib libboost_graph-mt.a libboost_random.dylib
libACE.dylib libboost_graph-mt.dylib libboost_regex-mt.a
libACEXML-5.6.9.dylib libboost_graph.a libboost_regex-mt.dylib
libACEXML.dylib libboost_graph.dylib libboost_regex.a
libACEXML_Parser-5.6.9.dylib libboost_iostreams-mt.a libboost_regex.dylib
libACEXML_Parser.dylib libboost_iostreams-mt.dylib libboost_serialization-mt.a
libCoreUtils.dylib libboost_iostreams.a libboost_serialization-mt.dylib
libImplAntiphishing.dylib libboost_iostreams.dylib libboost_serialization.a
libImplAntivirus.dylib libboost_locale-mt.a libboost_serialization.dylib
libImplBackupClient.dylib libboost_locale-mt.dylib libboost_signals-mt.a
libImplBrowser.dylib libboost_log-mt.a libboost_signals-mt.dylib
libImplDesktopSharing.dylib libboost_log-mt.dylib libboost_signals.a
libImplFirewall.dylib libboost_log.a libboost_signals.dylib
libImplHDEncryption.dylib libboost_log.dylib libboost_system-mt.a
libImplInstantMessenger.dylib libboost_log_setup-mt.a libboost_system-mt.dylib
libImplP2p.dylib libboost_log_setup-mt.dylib libboost_system.a
libImplPatchManagementAgent.dylib libboost_log_setup.a libboost_system.dylib
libImplSoftwareProduct.dylib libboost_log_setup.dylib libboost_test_exec_monitor-mt.a
libImplSystemManagement.dylib libboost_math_c99-mt.a libboost_test_exec_monitor.a
libImplUrlFiltering.dylib libboost_math_c99-mt.dylib libboost_thread-mt.a
libImplVirtualMachine.dylib libboost_math_c99.a libboost_thread-mt.dylib
libImplVpnClient.dylib libboost_math_c99.dylib libboost_timer-mt.a
libInodePortalPt.dylib libboost_math_c99f-mt.a libboost_timer-mt.dylib
libInodeSecurityAuth.dylib libboost_math_c99f-mt.dylib libboost_timer.a
libInodeUtility.dylib libboost_math_c99f.a libboost_timer.dylib
libInodeX1Pt.dylib libboost_math_c99f.dylib libboost_type_erasure-mt.a
libOesisCore.dylib libboost_math_c99l-mt.a libboost_type_erasure-mt.dylib
libboost_atomic-mt.a libboost_math_c99l-mt.dylib libboost_type_erasure.a
libboost_atomic-mt.dylib libboost_math_c99l.a libboost_type_erasure.dylib
libboost_chrono-mt.a libboost_math_c99l.dylib libboost_unit_test_framework-mt.a
libboost_chrono-mt.dylib libboost_math_tr1-mt.a libboost_unit_test_framework-mt.dylib
libboost_chrono.a libboost_math_tr1-mt.dylib libboost_unit_test_framework.a
libboost_chrono.dylib libboost_math_tr1.a libboost_unit_test_framework.dylib
libboost_container-mt.a libboost_math_tr1.dylib libboost_wave-mt.a
libboost_container-mt.dylib libboost_math_tr1f-mt.a libboost_wave-mt.dylib
libboost_container.a libboost_math_tr1f-mt.dylib libboost_wserialization-mt.a
libboost_container.dylib libboost_math_tr1f.a libboost_wserialization-mt.dylib
libboost_context-mt.a libboost_math_tr1f.dylib libboost_wserialization.a
libboost_context-mt.dylib libboost_math_tr1l-mt.a libboost_wserialization.dylib
libboost_coroutine-mt.a libboost_math_tr1l-mt.dylib libdnet.0.0.0.dylib
libboost_coroutine-mt.dylib libboost_math_tr1l.a libdnet.0.dylib
libboost_coroutine.a libboost_math_tr1l.dylib libdnet.dylib
libboost_coroutine.dylib libboost_prg_exec_monitor-mt.a libwx_base_carbon-2.8.0.dylib
libboost_date_time-mt.a libboost_prg_exec_monitor-mt.dylib libwx_base_carbon_net-2.8.0.dylib
libboost_date_time-mt.dylib libboost_prg_exec_monitor.a libwx_base_carbon_xml-2.8.0.dylib
libboost_date_time.a libboost_prg_exec_monitor.dylib libwx_mac_adv-2.8.0.dylib
libboost_date_time.dylib libboost_program_options-mt.a libwx_mac_aui-2.8.0.dylib
libboost_exception-mt.a libboost_program_options-mt.dylib libwx_mac_core-2.8.0.dylib
libboost_exception.a libboost_program_options.a libwx_mac_html-2.8.0.dylib
libboost_filesystem-mt.a libboost_program_options.dylib libwx_mac_qa-2.8.0.dylib
libboost_filesystem-mt.dylib libboost_random-mt.a libwx_mac_richtext-2.8.0.dylib
libboost_filesystem.a libboost_random-mt.dylib libwx_mac_xrc-2.8.0.dylib
libboost_filesystem.dylib libboost_random.a tables.dat
从输出内容可以看出有很多libboost_…,说明boost的库已安装成功。
下面就来试试boost库吧:
# include <iostream>
# include <cstdlib>
# include <boost/filesystem.hpp>
using namespace std;
int main(void) {
int a = system("mkdir Mac");
cout << a << endl;
return 0;
}
我要使用boost中的filesystem, 要编译这段代码,需要进行库的链接等操作。
首先我先查看编译器搜索头文件的路径:
➜ ~ c++ -x c++ -v -E /dev/null
Apple LLVM version 6.1.0 (clang-602.0.53) (based on LLVM 3.6.0svn)
Target: x86_64-apple-darwin14.5.0
Thread model: posix
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang" -cc1 -triple x86_64-apple-macosx10.10.0 -E -disable-free -disable-llvm-verifier -main-file-name null -mrelocation-model pic -pic-level 2 -mdisable-fp-elim -masm-verbose -munwind-tables -target-cpu core2 -target-linker-version 242.2 -v -dwarf-column-info -resource-dir /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/6.1.0 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk -stdlib=libc++ -fdeprecated-macro -fdebug-compilation-dir /Users/apple -ferror-limit 19 -fmessage-length 125 -stack-protector 1 -mstackrealign -fblocks -fobjc-runtime=macosx-10.10.0 -fencode-extended-block-signature -fcxx-exceptions -fexceptions -fmax-type-align=16 -fdiagnostics-show-option -fcolor-diagnostics -o - -x c++ /dev/null
clang -cc1 version 6.1.0 based upon LLVM 3.6.0svn default target x86_64-apple-darwin14.5.0
ignoring nonexistent directory "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/usr/include/c++/v1"
ignoring nonexistent directory "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/usr/local/include"
ignoring nonexistent directory "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/Library/Frameworks"
#include "..." search starts here:
#include <...> search starts here:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/6.1.0/include
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/usr/include
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/System/Library/Frameworks (framework directory)
End of search list.
# 1 "/dev/null"
# 1 "<built-in>" 1
# 1 "<built-in>" 3
# 326 "<built-in>" 3
# 1 "<command line>" 1
# 1 "<built-in>" 2
# 1 "/dev/null" 2
从输出内容可以看到,查找路径如下:
#include "..." search starts here:
#include <...> search starts here:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/6.1.0/include
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/usr/include
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/System/Library/Frameworks (framework directory)
查找路径中并没有/usr/local/include 与 /usr/local/lib,所以需要在g++中加入一些指令:
g++ main.cpp -L/usr/local/lib -I/usr/local/include -lboost_filesystem -lboost_system
这样也就编译成功了~
对于Xcode,按上面的指令安装完boost后还要进行设置,比较复杂:
(以我要使用的”boost::filesystem”为例, 其它的可以类推)
(以下图片皆为原创,转载请注明出处)
假如我们要编译含<boost/filesystem.hpp>
头文件的代码:
然后command+run, 有下面的错误提示:
错误信息表明,编译器不能找到<boost/filesystem.hpp>
的头文件!那么说明boost库及头文件所在位置不在编译器搜索范围内!那么我们就要对xcode进行设置,增加搜索路径:
步骤一:找到Bulid Settings
步骤二:找到Library Search Paths
步骤三:单击或双击方框中的位置
步骤四:增加方框中的内容
步骤五:找到Header Search Paths
步骤六:点击或双击方框中的位置
步骤七:增加方框中的内容
至此,便完成了增加头文件的操作。然后再次编译,但又出现下列错误信息:
错误信息表明,编译器不能找到boost库,也就是说,boost库不在编译器搜索的路径当中,因此需要再对xcode进行设置,增加库搜索路径:
步骤一: 找到Build Phases
步骤二:找到Link Binary With Libraries
步骤三:点击+号
我们发现因为方框内搜索不到boost, 因此执行步骤四/五:
步骤四/步骤五:点击 Add Other…
步骤六:因为库文件无法直接通过Add other弹出的窗口找到,所以调出”前往文件夹“的窗口(快捷键: command+shift+G), 然后输入方框内的内容
步骤七:找到并添加libboost_system-mt.dylib
步骤八:找到并添加libboost_filesystem-mt.dylib
添加完这两个lib后,代码就能正常编译啦~
以上就是我总结的内容,但是毕竟是我个人实践的,并不具有普遍性。如果你在我的文章中不能寻求到解决问题的方法,我提供给你另一篇我个人觉得挺不错的文章~或者你也可以google。
推荐:
Mac上Homebrew的使用 (Homebrew 使 OS X 更完整)
其它链接:
https://github.com/Homebrew/legacy-homebrew
https://github.com/Homebrew/legacy-homebrew
以上内容皆为本人观点,欢迎大家提出批评和指导,我们一起探讨!
以上是关于实践篇:Install Homebrew and Boost Library 以及 Xcode配置Boost库的主要内容,如果未能解决你的问题,请参考以下文章
sh [Homebrew Setup] #installer #homebrew #install