作者Caesar08 (Caesar)
看板C_and_CPP
標題[分享] Boost build 心得
時間Tue Feb 16 23:36:24 2016
前言:
會寫這篇文章是因為網路上的資料零零散散的
所以寫一篇文章分享給各位(也給未來的自己)
這篇文章會講到
b2.exe的參數部分
作業系統:windows
編譯器:Visual C++ 14.0(Visual Studio 2015)
正文開始:
首先,先去boost的官網下載最新的lib,
http://www.boost.org/users/download/
目前(2016/2/16)最新版本是1.60.0(所以底下都用boost_1_60_0),下載好記得解壓縮
裡面會看到
bootstrap.bat,他是一個用來產生
b2.exe與
bjam.exe的檔案
(版本1.47.0以前,只有
bjam.exe;版本1.47.0以後,
bjam.exe改名為
b2.exe)
(現在的
bjam.exe是
b2.exe的複製版本,兩個是一樣的,但
bjam.exe為了與以前相容而存在)
執行
bootstrap.bat
在boost裡面,有些library是
header-only(只要是
class template),有些不是(如:thread)
因此只要使用這些非
header-only的檔案,就要把那些.cpp檔也放入編譯
但是每次都這樣編譯實在很花時間,這些檔案都是不會被更改的,那不如就先把他做成.lib(
static link library)或.dll(
dynamic link library)
讓user之後不用編譯這些不會動的library
因此,就要開始介紹如何製作這些.lib或.dll
調整編譯器參數
你需要先知道編譯器版本,像我是Visual C++ 14.0
(如果不知道版本,維基百科有提供協助
https://goo.gl/fhIKnR,或在維基百科上搜尋visual c++)
打開
boost_1_60_0/project-config.jam(註),裡面有一行
using msvc ;
你要改成
using msvc : 14.0 ;
^ ^ ^ 一定要有空格
(如果你電腦只有一個編譯器,實際上可以不用填)
如果你的visual studio不是安裝在預設路徑,那你可能需要指定路徑,因此要改成
using msvc : 14.0 : "D:/your/path/to/cl.exe" ;
路徑中如果有空格(如:D:/my boost),要用"包起來,並且用/來表示路徑(不然就用\\)
(
cl.exe是visual c++的編譯器)
如果你有特別的參數想要給compiler,例如說你希望它只有在編譯c++ code時做optimize,那你就要改成
using msvc : 14.0 : : <cxxflags>"/Od /Ox" ;
^ 因為我用預設路徑,所以可以不填任何值
cxxflags是專門針對的C++做處理,如果想對所有語言做處理,可以使用
<compileflags>
/Od /Ox是
cl.exe的optimize options,別忘記用"包起來
當你的b2.exe的variant使用release時,預設是使用/O2,如果你想用/Ox,你需要先用/O
d把/O2關起來,再使用/Ox
關於更多using的設定可參考
http://goo.gl/RYZRWt
而
cl.exe參數可參考
https://goo.gl/07qCDd,或搜尋visual c++ compiler options
註:不使用
user-config.jam是因為它在boost_1_60_0/tools/build/example裡面,我懶得移動它
準備b2.exe參數
b2.exe就是實際上產生.lib或.dll的檔案,接下來會介紹參數,每一個參數的第一個就是預設值
address-model:32, 64
要產生32或64位元的code
architecture:x86, ia64, sparc, power, mips1, mips2, mips3, mips4, mips32, mips32r2, mips64, parisc, arm, combined, combined-x86-power
因為沒有amd64可以選,所以應該大部分人都會選x86
link:shared, static
shared會產出.dll,static會產出.lib
runtime-link:shared, static
shared會用dynamic的
run-time library(visual c++的/MD)
static會用static的
run-time library(visual c++的/MT)
建議使用shared(default)
target-os:
預設值是你現在使用的os(像我是windows,它就會產出給windows用的library)
如果你想要編譯給別的os用的library,你可以給這些參數aix, bsd, cygwin, darwin, freebsd, hpux, iphone, linux, netbsd, openbsd, osf, qnx, qnxnto, sgi, solaris, unix, unixware, windows
threading:single, multi
建議選擇multi,因為你不使用thread並不代表你用的library不使用thread
toolset:
像我的編譯器是visual c++,所以我會填msvc-14.0(因為我要用14.0的版本)
variant:debug, release, profile
debug會包含
debug-symbols與
runtime-debugging
release會包含
optimization與
inlining
profile會包含
debug-symbols與
optimization與
inlining
debug-symbols:on, off
建議交給
variant設定就好
runtime-debugging:on, off
建議交給
variant設定就好
--build-type:minimal, complete
如果是minimal,在windows上,它只會編譯debug跟release兩種mode(variant)
但如果是complete,它就會編譯你能提供的所有可能,address-model x architecture x ...
建議使用minimal(default)
-jN:平行執行N個指令
N跟你的CPU核心數有關,像我的CPU是8核心,所以N就是8
stage,
install:
stage是裝在./stage,install是裝在C:/Boost
(前者給你用,後者給所有用這台電腦的人用)
到目前為止,最主要的參數應該都講完了,接下來就是如何使用
1. 打開command prompt,
如果你有cygwin或MinGW,請不要用他們的bash shell,b2.exe
會以為是Unix系統
2. cd boost_1_60_0
3.
b2.exe 你的指令
(如:
b2.exe -j8
address-model=64
link=static
threading=multi
toolset=msvc-14.0
variant=release
stage,順序不影響,
architecture、
runtime-link、
--build-type使用default)
(如:
b2.exe -j8
address-model=32,64
link=static
threading=multi
toolset=msvc-14.0
variant=debug,release
--build-type=complete
stage,這邊
address-model有32與64,
variant有debug與release,因此到時候就會產出4個.lib,分別是32 x debug、32 x release、64 x debug、64 x release)
4. 等它執行完畢即可
這樣就大功告成,之後如果你有visual c++的project要使用boost library
你只要在project > XXX Properties... > Configuration Properties > Linker > General > Additional Library Directories增加boost_1_60_0/stage/lib即可
參考:
http://www.boost.org/doc/libs/1_60_0/doc/html/bbv2/reference.html
http://www.boost.org/doc/libs/1_60_0/more/getting_started/windows.html
https://www.ptt.cc/bbs/Windows/M.1242287062.A.27C.html
https://msdn.microsoft.com/en-us/library/8f8h5cxt.aspx
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 111.251.50.24
※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1455637005.A.3C0.html
推 saxontai: 推心得整理。不過我自己是認為官方 Getting Started 02/17 00:23
→ saxontai: 文件已經講得夠清楚了。XD 02/17 00:23
推 damody: 我一直用bjam也沒問題啊??? 02/17 00:48
→ damody: bjam --toolset=msvc-14.0 --build-type=complete archite 02/17 00:50
→ damody: cture=x86 address-model=64 stage 02/17 00:51
→ b98901056: 感恩 02/17 00:56
※ 編輯: Caesar08 (1.161.19.40), 02/17/2016 12:24:09
推 eye5002003: Jamfile裡面寫很簡單,看完都自己寫makefile去編譯了 02/17 20:49
→ eye5002003: 我是自己寫CMake去編譯,方便把微調部分記在專案裡 02/17 20:52