1.通用规则
多数库是不需要预先编译的,include hpp文件就能用。如果出现链接失败,那就是可能需要编译库了。
boost自带一套编译工具bjam,bjam本身是跨平台的,并且也要自行编译出来。在boost目录下有bootstrap.sh和bootstrap.bat两个脚本分别用来编译*nix和windows下的bjam。bootstrap脚本可以传入参数,以在编译bjam过程中生成特定的编译boost的配置。这些配置保存在新生成的project-config.jam里,但还可以在运行bjam的时候再传入参数来覆盖。同时生成的b2是bjam的代理,运行哪个的效果都差不多。
在终端下运行
bjam --show-libraries
会列出所有要编译的库。
真正编译时,可以传入–with-xxx来选择编译哪些库,或者传入–without-xxx来选择不编译哪些库。如果不传则会读取project-config.jam的设置,如果也没有则是编译全部的库。
更多的参数可以用
bjam --help
来查看。例如编译成静态库还是动态库,运行时库是静态的还是动态的,编译完后要不要安装等。
注意:
旧版本的boost可能会存在编译问题,尽量用新的就好。bjam在*nix和windows支持的参数有不同。
2. iOS平台编译
环境:
OS X Yosemite 10.10.3
Xcode 6.3.1(6D1002)。如果用Xcode 7在模拟器版链接失败,请参考评论区4楼里的解决方案!
boost 1.57/1.58
步骤:
把下面的脚本保存成build_boost.sh,直接运行即可:
<code class="language-bash hljs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-shebang" style="color: rgb(0, 102, 102); box-sizing: border-box;">#!/bin/bash</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># http://blog.csdn.net/hursing/article/details/45439087</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># 请自行修改路径,cd到boost解压后的目录下</span> dir=`dirname <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$0</span>` <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cd</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$dir</span>/../../third_party/boost_1_57_0"</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># 如果库文件已存在,直接退出</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> [ <span class="hljs-operator" style="box-sizing: border-box;">-e</span> ./stage/lib/libboost_date_time.a ]; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">then</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">echo</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"libraries exist. no need to build."</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">exit</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">fi</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># 以下代码参考 https://gist.github.com/rsobik/7513324 ,原文使用的boost版本比较旧,不能使用。</span> : <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">${COMPILER:="clang++"}</span> : <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">${IPHONE_SDKVERSION:=`xcodebuild -showsdks | grep iphoneos | egrep "[[:digit:]]+\.[[:digit:]]+" -o | tail -1`}</span> : <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">${XCODE_ROOT:=`xcode-select -print-path`}</span> : <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">${EXTRA_CPPFLAGS:="-DBOOST_AC_USE_PTHREADS -DBOOST_SP_USE_PTHREADS -stdlib=libc++"}</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">echo</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"IPHONE_SDKVERSION: <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$IPHONE_SDKVERSION</span>"</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">echo</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"XCODE_ROOT: <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$XCODE_ROOT</span>"</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">echo</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"COMPILER: <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$COMPILER</span>"</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">echo</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"bootstrap"</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># 此脚本如果是被Xcode调用的话,会因为xcode export的某些变量导致失败,所以加了env -i。直接在命令行运行此脚本可以把env -i 去掉</span> env -i bash ./bootstrap.sh <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">echo</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"write project-config.jam"</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># 默认生存的project-config.jam是编译Mac版的,这里直接调换掉</span> rm project-config.jam cat >> project-config.jam <<EOF using darwin : <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">${IPHONE_SDKVERSION}</span>~iphone : <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$XCODE_ROOT</span>/Toolchains/XcodeDefault.xctoolchain/usr/bin/<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$COMPILER</span> -arch armv7 -arch arm64 <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$EXTRA_CPPFLAGS</span> : <striper> <root><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$XCODE_ROOT</span>/Platforms/iPhoneOS.platform/Developer : <architecture>arm <target-os>iphone ; using darwin : <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">${IPHONE_SDKVERSION}</span>~iphonesim : <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$XCODE_ROOT</span>/Toolchains/XcodeDefault.xctoolchain/usr/bin/<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$COMPILER</span> -arch i386 -arch x86_64 <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$EXTRA_CPPFLAGS</span> : <striper> <root><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$XCODE_ROOT</span>/Platforms/iPhoneSimulator.platform/Developer : <architecture>ia64 <target-os>iphone ; EOF <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># 上面的代码里,两个using darwin分别是编译真机版和模拟器版的设置。每多一种CPU架构就要再加一个-arch xxx。</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">echo</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"build boost iphone dev"</span> ./bjam -j16 --with-date_time --with-filesystem --with-system --with-thread --build-dir=iphone-build --stagedir=iphone-build/stage toolset=darwin architecture=arm target-os=iphone macosx-version=iphone-<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">${IPHONE_SDKVERSION}</span> define=_LITTLE_ENDIAN link=static stage <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">echo</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"build boost iphone sim"</span> ./bjam -j16 --with-date_time --with-filesystem --with-system --with-thread --build-dir=iphonesim-build --stagedir=iphonesim-build/stage --toolset=darwin-<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">${IPHONE_SDKVERSION}</span>~iphonesim architecture=ia64 target-os=iphone macosx-version=iphonesim-<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">${IPHONE_SDKVERSION}</span> link=static stage <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">echo</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"lipo"</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># 把各架构下的库文件合一,以便在xcode里可以少设置些搜索路径。做得更彻底些是各个分库合成一个大库。不过除非是把静态库加入到代码仓库,否则是浪费时间了。要合成的大库话请参考https://gist.github.com/rsobik/7513324原文。</span> mkdir -p stage/lib lipo -create iphone-build/stage/lib/libboost_date_time.a iphonesim-build/stage/lib/libboost_date_time.a -output stage/lib/libboost_date_time.a lipo -create iphone-build/stage/lib/libboost_filesystem.a iphonesim-build/stage/lib/libboost_filesystem.a -output stage/lib/libboost_filesystem.a lipo -create iphone-build/stage/lib/libboost_system.a iphonesim-build/stage/lib/libboost_system.a -output stage/lib/libboost_system.a lipo -create iphone-build/stage/lib/libboost_thread.a iphonesim-build/stage/lib/libboost_thread.a -output stage/lib/libboost_thread.a <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># 库文件最终放在./stage/lib/下</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">echo</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Completed successfully"</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; opacity: 0.0289368; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li><li style="box-sizing: border-box; padding: 0px 5px;">33</li><li style="box-sizing: border-box; padding: 0px 5px;">34</li><li style="box-sizing: border-box; padding: 0px 5px;">35</li><li style="box-sizing: border-box; padding: 0px 5px;">36</li><li style="box-sizing: border-box; padding: 0px 5px;">37</li><li style="box-sizing: border-box; padding: 0px 5px;">38</li><li style="box-sizing: border-box; padding: 0px 5px;">39</li><li style="box-sizing: border-box; padding: 0px 5px;">40</li><li style="box-sizing: border-box; padding: 0px 5px;">41</li><li style="box-sizing: border-box; padding: 0px 5px;">42</li><li style="box-sizing: border-box; padding: 0px 5px;">43</li><li style="box-sizing: border-box; padding: 0px 5px;">44</li><li style="box-sizing: border-box; padding: 0px 5px;">45</li><li style="box-sizing: border-box; padding: 0px 5px;">46</li><li style="box-sizing: border-box; padding: 0px 5px;">47</li><li style="box-sizing: border-box; padding: 0px 5px;">48</li><li style="box-sizing: border-box; padding: 0px 5px;">49</li><li style="box-sizing: border-box; padding: 0px 5px;">50</li><li style="box-sizing: border-box; padding: 0px 5px;">51</li><li style="box-sizing: border-box; padding: 0px 5px;">52</li><li style="box-sizing: border-box; padding: 0px 5px;">53</li><li style="box-sizing: border-box; padding: 0px 5px;">54</li><li style="box-sizing: border-box; padding: 0px 5px;">55</li><li style="box-sizing: border-box; padding: 0px 5px;">56</li><li style="box-sizing: border-box; padding: 0px 5px;">57</li><li style="box-sizing: border-box; padding: 0px 5px;">58</li><li style="box-sizing: border-box; padding: 0px 5px;">59</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li><li style="box-sizing: border-box; padding: 0px 5px;">33</li><li style="box-sizing: border-box; padding: 0px 5px;">34</li><li style="box-sizing: border-box; padding: 0px 5px;">35</li><li style="box-sizing: border-box; padding: 0px 5px;">36</li><li style="box-sizing: border-box; padding: 0px 5px;">37</li><li style="box-sizing: border-box; padding: 0px 5px;">38</li><li style="box-sizing: border-box; padding: 0px 5px;">39</li><li style="box-sizing: border-box; padding: 0px 5px;">40</li><li style="box-sizing: border-box; padding: 0px 5px;">41</li><li style="box-sizing: border-box; padding: 0px 5px;">42</li><li style="box-sizing: border-box; padding: 0px 5px;">43</li><li style="box-sizing: border-box; padding: 0px 5px;">44</li><li style="box-sizing: border-box; padding: 0px 5px;">45</li><li style="box-sizing: border-box; padding: 0px 5px;">46</li><li style="box-sizing: border-box; padding: 0px 5px;">47</li><li style="box-sizing: border-box; padding: 0px 5px;">48</li><li style="box-sizing: border-box; padding: 0px 5px;">49</li><li style="box-sizing: border-box; padding: 0px 5px;">50</li><li style="box-sizing: border-box; padding: 0px 5px;">51</li><li style="box-sizing: border-box; padding: 0px 5px;">52</li><li style="box-sizing: border-box; padding: 0px 5px;">53</li><li style="box-sizing: border-box; padding: 0px 5px;">54</li><li style="box-sizing: border-box; padding: 0px 5px;">55</li><li style="box-sizing: border-box; padding: 0px 5px;">56</li><li style="box-sizing: border-box; padding: 0px 5px;">57</li><li style="box-sizing: border-box; padding: 0px 5px;">58</li><li style="box-sizing: border-box; padding: 0px 5px;">59</li></ul>
集成到Xcode
做iOS的同学还是习惯完全用Xcode的吧。所以可以把上面的脚本集成到xcode里。在某个工程中,Xcode菜单File->New->Target…->Other->Aggregate->取名boost,Finish->在这个target的属性中选择Build
Phases->点击左边的+号->New Run Script Phase->填入bash ./build_boost.sh->自己再填好库文件搜索路径和链接各个boost库。主target还要设置依赖boost target,免得boost编译前就链接以致错误。
3.Android平台编译
Android的编译方法有两种:一种是和iOS一样,自己覆盖project-config.jam引入NDK的编译工具设置,然后用bjam编译;另一种是自己写Android.mk。自己写project-config.jam因为要兼顾很多种CPU架构而(无论是写jam还是主工程的Android.mk都)较为麻烦,所以本文用后者。
环境
Ubuntu 14.04 64bit / Linux Mint 17 64bit
NDK r10c
boost 1.57/1.58
步骤
想要哪个库,自己把那个库下的cpp文件加到mk里,都在libs/xxxx/下。如果在libs找不到,说明这个库是不需要额外编译的。
以下是编译四个库的Android.mk的内容:
<code class="language-Android.mk hljs tex has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">include <span class="hljs-formula" style="box-sizing: border-box; font-style: italic; background-color: rgb(238, 238, 238);">$(CLEAR_VARS) LOCAL_PATH := $</span>(ROOT_PATH)/third_party/boost_1_57_0 LOCAL_MODULE := boost <span class="hljs-special" style="box-sizing: border-box; color: rgb(102, 102, 0);">#</span> 需要编译哪个库,自行把libs目录下的cpp加进来即可。 LOCAL_SRC_FILES += <span class="hljs-command" style="box-sizing: border-box; color: rgb(0, 0, 136);">\ </span> libs/filesystem/src/path.cpp <span class="hljs-command" style="box-sizing: border-box; color: rgb(0, 0, 136);">\ </span> libs/filesystem/src/path_traits.cpp <span class="hljs-command" style="box-sizing: border-box; color: rgb(0, 0, 136);">\ </span> libs/filesystem/src/operations.cpp <span class="hljs-command" style="box-sizing: border-box; color: rgb(0, 0, 136);">\ </span> libs/filesystem/src/codecvt_error_category.cpp <span class="hljs-command" style="box-sizing: border-box; color: rgb(0, 0, 136);">\ </span> libs/filesystem/src/portability.cpp <span class="hljs-command" style="box-sizing: border-box; color: rgb(0, 0, 136);">\ </span> libs/filesystem/src/utf8_codecvt_facet.cpp <span class="hljs-command" style="box-sizing: border-box; color: rgb(0, 0, 136);">\ </span> <span class="hljs-command" style="box-sizing: border-box; color: rgb(0, 0, 136);">\ </span> libs/date_time/src/gregorian/date_generators.cpp <span class="hljs-command" style="box-sizing: border-box; color: rgb(0, 0, 136);">\ </span> libs/date_time/src/gregorian/greg_month.cpp <span class="hljs-command" style="box-sizing: border-box; color: rgb(0, 0, 136);">\ </span> libs/date_time/src/gregorian/greg_weekday.cpp <span class="hljs-command" style="box-sizing: border-box; color: rgb(0, 0, 136);">\ </span> libs/date_time/src/gregorian/gregorian_types.cpp <span class="hljs-command" style="box-sizing: border-box; color: rgb(0, 0, 136);">\ </span> libs/date_time/src/posix_time/posix_time_types.cpp <span class="hljs-command" style="box-sizing: border-box; color: rgb(0, 0, 136);">\ </span> <span class="hljs-command" style="box-sizing: border-box; color: rgb(0, 0, 136);">\ </span> libs/system/src/error_code.cpp <span class="hljs-command" style="box-sizing: border-box; color: rgb(0, 0, 136);">\ </span> <span class="hljs-command" style="box-sizing: border-box; color: rgb(0, 0, 136);">\ </span> libs/thread/src/future.cpp <span class="hljs-command" style="box-sizing: border-box; color: rgb(0, 0, 136);">\ </span> libs/thread/src/pthread/once.cpp <span class="hljs-command" style="box-sizing: border-box; color: rgb(0, 0, 136);">\ </span> libs/thread/src/pthread/once_atomic.cpp <span class="hljs-command" style="box-sizing: border-box; color: rgb(0, 0, 136);">\ </span> libs/thread/src/pthread/thread.cpp <span class="hljs-special" style="box-sizing: border-box; color: rgb(102, 102, 0);">#</span> 如果要把boost集成到动态库里,-fPIC是必须的,不然会有链接错误。原因请自行Google LOCAL_CFLAGS += -fPIC -frtti -fexceptions include <span class="hljs-formula" style="box-sizing: border-box; font-style: italic; background-color: rgb(238, 238, 238);">$(BUILD_STATIC_LIBRARY)</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; opacity: 0.0289368; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li></ul>
Application.mk的内容:
<code class="language-makefile hljs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-constant" style="box-sizing: border-box;">APP_PLATFORM</span> := android-19 <span class="hljs-constant" style="box-sizing: border-box;">APP_OPTIM</span> := release <span class="hljs-constant" style="box-sizing: border-box;">APP_CFLAGS</span> += -Wall <span class="hljs-constant" style="box-sizing: border-box;">APP_STL</span> := gnustl_static <span class="hljs-constant" style="box-sizing: border-box;">APP_ABI</span> := armeabi-v7a</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; opacity: 0.0289368; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li></ul>
这是最普通的做法,如果要加编译参数,还得自己琢磨下。例如不要rtti,就得LOCAL_CFLAGS += -DBOOST_NO_RTTI
。
还是想用boost原生的编译方法的话,可参考这里 http://stackoverflow.com/questions/30491158/compile-and-use-boost-for-android-ndk-r10e
4.Windows平台编译
环境
Windows 7 旗舰版 64bit SP1
Visual Studio Ultimate 2013
boost 1.57/1.58
步骤
以下是build_boost.bat的内容,直接执行即可:
<code class="language-bat hljs lasso has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">cd <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">..</span><span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">\</span><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">..</span><span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">\</span>third_party<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">\</span>boost_1_57_0 <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">not</span> exist <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">.</span><span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">\</span>b2<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">.</span>exe ( call <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">.</span><span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">\</span>bootstrap<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">.</span>bat <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">.</span><span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">\</span>b2<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">.</span>exe <span class="hljs-attribute" style="box-sizing: border-box;">-j5</span> <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">--</span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">with</span><span class="hljs-attribute" style="box-sizing: border-box;">-date_time</span> <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">--</span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">with</span><span class="hljs-attribute" style="box-sizing: border-box;">-filesystem</span> <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">--</span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">with</span><span class="hljs-attribute" style="box-sizing: border-box;">-system</span> <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">--</span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">with</span><span class="hljs-attribute" style="box-sizing: border-box;">-thread</span> <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">--</span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">with</span><span class="hljs-attribute" style="box-sizing: border-box;">-regex</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">link</span><span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span>static runtime<span class="hljs-attribute" style="box-sizing: border-box;">-link</span><span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span>static )</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; opacity: 0.0289368; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li></ul>
注意这里生成的是MTd和MT。MD的话runtime-link=shared。
集成到VS
工程属性->Configuration Properties->Build Events->Pre-Link Event->Command Line->填入build_boost.bat
还有设置好库搜索路径和依赖的库。
Windows的boost有个特点,代码里有#pragma comment(lib, "xxxx")
,所以可以不设置依赖的库。但是如果工程设置不对而有link错误,那还是手动加进去吧。
5.Linux平台编译
环境
Ubuntu 14.04 64bit / Linux Mint 17 64bit
gcc version 4.8.2 (Ubuntu 4.8.2-19ubuntu1)
boost 1.57/1.58
步骤
直接编译。以下是终端命令
cd boost_1_57_0
bash bootstrap.sh
./b2
<code class="language-shell hljs bash has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cd</span> boost_1_57_0 bash bootstrap.sh ./b2</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; opacity: 0.0289368; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul>
这样就能在boost_1_57_0/stage/lib/下找到所有的库文件。
如果用cmake来组织编译,以下是主工程的CMakeLists.txt里的部分内容:
<code class="language-cmake hljs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">include</span>(ExternalProject) ExternalProject_Add(boost SOURCE_DIR <span class="hljs-envvar" style="color: rgb(102, 102, 0); box-sizing: border-box;">${ROOT_DIR}</span>/third_party/boost_1_57_0 CONFIGURE_COMMAND bash <span class="hljs-envvar" style="color: rgb(102, 102, 0); box-sizing: border-box;">${ROOT_DIR}</span>/third_party/boost_1_57_0/bootstrap.sh --with-libraries=date_time,thread,filesystem,system <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">BUILD_COMMAND</span> <span class="hljs-envvar" style="color: rgb(102, 102, 0); box-sizing: border-box;">${ROOT_DIR}</span>/third_party/boost_1_57_0/b2 -j10 cflags=-fPIC cxxflags=-fPIC <font color=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"red"</span>>cxxflags=-fvisibility=hidden cxxflags=-fvisibility-inlines-hidden</font> cxxflags=-fexceptions cxxflags=-DBOOST_NO_RTTI BUILD_IN_SOURCE <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> INSTALL_COMMAND <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">""</span> ) <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">set</span>( BOOST_LIBRARIES <span class="hljs-envvar" style="color: rgb(102, 102, 0); box-sizing: border-box;">${ROOT_DIR}</span>/third_party/boost_1_57_0/stage/lib/libboost_date_time.a <span class="hljs-envvar" style="color: rgb(102, 102, 0); box-sizing: border-box;">${ROOT_DIR}</span>/third_party/boost_1_57_0/stage/lib/libboost_system.a <span class="hljs-envvar" style="color: rgb(102, 102, 0); box-sizing: border-box;">${ROOT_DIR}</span>/third_party/boost_1_57_0/stage/lib/libboost_thread.a <span class="hljs-envvar" style="color: rgb(102, 102, 0); box-sizing: border-box;">${ROOT_DIR}</span>/third_party/boost_1_57_0/stage/lib/libboost_filesystem.a ) <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># ...</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># 下面是设置链接和依赖</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">target_link_libraries</span>(your_target <span class="hljs-envvar" style="color: rgb(102, 102, 0); box-sizing: border-box;">${BOOST_LIBRARIES}</span>) <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">add_dependencies</span>(your_target boost)</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; opacity: 0.0289368; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li></ul>
可留意
b2 -j10 cflags=-fPIC cxxflags=-fPIC cxxflags=-fvisibility=hidden cxxflags=-fvisibility-inlines-hiddencxxflags=-fexceptions cxxflags=-DBOOST_NO_RTTI
如果要传递多个cflags,需要重复键名,而不是加双引号把多个值赋给同一个。红色的部分是隐藏符号。
6.Mac OS X编译
环境
OS X Yosemite 10.10.3
Xcode 6.3.1(6D1002)
boost 1.57/1.58
步骤
和Linux相同。集成到Xcode的步骤和iOS类似。
bjam命令和各参数间及参数与参数间都以空格隔开。debug release表示调试版和发布版皆被生成。
bjam使用的形式为:
bjam --参数1-模式1 --参数2-模式2参数3=模式3 … debug release
参数前要加”--”,参数对应项如“—without-serialization” –without与serialization之间要加”-”,其它的参数需要设置成对应的模式用”=”。
bjam.exe部分参数解释如下:
stage/install:stage指定boost C++库安装在名为stage的子目录下,可又--stagedir指定安装路径;install在系统范围内安装(windows下安装目录为C:\Boost,而在linux中则是/usr/local),目标目录也可以由--prefix指出。 stage表示只生成库(dll和lib),install还会生成包含头文件的include目录。【未经验证】 --toolset:指定C++的编译器[ VS2008对应--toolset=msvc-9.0,vs2010对应--toolset=msvc-10.0 ] --build-type:创建何种方式的库。缺省情况下,该选项设为minimal,即只创建发布版。对于那些想用Visual Studio 或GCC 构建他们项目的调试版的开发者来说,可能是一个问题。因为这些编译器会自动尝试链接调试版的Boost C++ 库,这样就会给出一个错误信息。在这种情况下,应将--build-type 选项设为complete,以同时生成Boost C++ 库的调试版和发布版,当然,所需时间也会更长一些。 link:创建动态[ link=shared ]还是静态[link=static]的库. runtime-link:指定C++运行时库是静态[ runtime-link=static ]链接还是动态[ runtime-link=shared ]链接 threading:单[ threading=single ]/多线程[ threading=multi ]编译。 without/with:选择不编译/编译哪些库。 |
- 顶
- 0
- 踩
- 0