boost 编译,每次都找,麻烦。

本文详细介绍了Boost库的编译步骤及配置方法,包括不同编译器下的编译指令、参数说明,以及如何使用STLPort。适用于希望深入了解Boost库构建过程的开发者。

1、运行VS2003工具里的 “Visual Studio .NET 2003 命令提示"
2、在命令行模式下,cd 进入需要编译的Boost C++ Libraries 下面具体类库的Build目录
3、运行 nmake vc71.mak

例子:
cd D:/MyProject/ShareCode/boost_1_35_0/libs/regex/build
nmake vc71.mak
and install

boost库的编译其实很简单,关键是要自己尝试一下。

 

从网上的一些文章中摘录出其中最关键的步骤,再加上自己的实践,总结出如下文字,在此存档。

 

===================================================

boost编译步骤:

1.代码下载 - 官方网站:http://www.boost.org

在官方网站下载代码的压缩包,解压到本地目录。
比如boost_1_38_0.zip,解压到 "e:/boost_1_38_0"。   

2. 准备工作

下载boost 编译工具 boost-jam-3.1.17-1-ntx86.zip。
下载地址google一下吧。http://sourceforge.net/projects/boost

3. 编译

  1):打开“开始”菜单,选择“运行”,输入cmd,进入控制台窗口,进入boost安装源路径e:/boost_1_38_0。

 2):cmd中输入:

  bjam install --toolset=borland --prefix="E:/boost_1_38_0" debug --with-date_time --with-regex
 
    -- 是两个连续的减号,=前后均不能夹带空格,除路径之外,参数都是小写字母。
   --toolset 表示采用borland编译。如果用VC编译,则改为 --toolset=msvc
   debug表示编译“调试版”。
    --with 指定要编译的模块
  3):回车后,出现大量奇怪文字,最后能看到以下内容,就表示成功完成编译:
  ...failed updating 2 targets.
  ...skipped 31 targets...
  ...updated 341 targets...

 4). release版本

  bjam install --toolset=borland --prefix="E:/boost_1_38_0" release --with-date_time --with-regex

  改变的是“debug”更换为“release”。回车后开始release的boost库编译。

 
 5).有时不知道编译器到底需要什么版本的lib,可以完整编译boost:

  bjam install --toolset=borland --prefix="E:/boost_1_38_0" debug(或release)--build-type=complete

    这样会编译出所有debug和release的lib库。

 6). 库的使用
  编译完成后,E:/boost_1_38_0 目录下,可以看到两个子目录:"include"和"lib",前者保存着所有采用纯头文件形式提供的boost代码文件;后者则是所有编译出的库文件。.dll, .lib

 7). 结束
  打开C++Builder2007.
    Project-Default Options-C++Builder设置。在Include Path添加前面的"include/boost-1_38"所在的目录,在Library Path添加“lib”所在的目录。

  C++Options-Paths and Directories.同上设置。

      VC的设置众所周知,就不说了。


另附 bjam 参数   

--build-dir=<builddir>     编译的临时文件会放在builddir里(编译完就可删除)
--stagedir=<stagedir>     存放编译后库文件的路径,默认是stage
--build-type=complete     编译所有版本(确切地说是相当于:variant=release, threading=multi;link=shared|static;runtime-link=shared)
variant=debug|release     编译什么版本(Debug or Release?)
link=static|shared             使用静态库还是动态库。
threading=single|multi      单线程还是多线程库。
runtime-link=static|shared     决定是静态还是动态链接C/C++标准库。
--with-<library>     只编译指定的库,如输入--with-regex就只编译regex库了。
--show-libraries     显示需要编译的库名称
===============================================================

1、下载boost包
从boost网站下载boost_1_36_0.tar.gz

2、解压
假设解压到D:/,那么Boost根目录就是D:/boost_1_36_0

3、构建bjam
在“Boost根目录/tools/jam”下,运行build_dist.bat(Linux:build_dist.sh)
将stage/bin.bin.ntx86/bjam.exe(Linux:stage/bin.linuxx86_64/bjam)拷贝到Boost根目录,创建快捷方式,符号链接也可以

4、构建boost库
bjam -toolset=msvc --build-type=complete install
(Linux:./bjam -toolset=gcc --build-type=complete install)

其他配置
1、安装路径:--prefix=<PREFIX>
默认:C:/Boost(Linux:/usr/local)

2、库文件安装路径:--libdir=<DIR>
默认:<EPREFIX>/lib

3、头文件安装路径:--includedir=<HDRDIR>
默认:<PREFIX>/include

4、构建类型:--build-type=<type>
debug release shared static mutil

5、中间文件存放路径:--build-dir=DIR
默认:Boost根目录

6、指定编译器:--toolset=toolset

msvc Microsoft Visual Studio
gcc Linux

可以指定版本,如
msvc-7.1 Microsoft Visual Studio 2003
msvc-8.0 Microsoft Visual Studio 2005

7、只构建指定库:--with-<library>

8、不构建指定库:--without-<library>
=========================test==============================
bjam install --toolset=msvc --prefix="E:/__Library__/boost_1_39_0" debug mutil --with-date_time --with-regex
bjam --with-system --with-thread --with-date_time --with-regex --with-serialization stage
///////////////////////////////////////////////////////
bjam install --toolset=msvc-7.1 --prefix="E:/__Library__/boost_1_39_0" --build-type="debug <threading>multi <link>shared <link>static <runtime-link>shared" --with-system

bjam install --toolset=msvc-7.1 --prefix="E:/__Library__/boost_1_39_0" debug --build-type=complete --with-system --with-thread --with-date_time --with-regex --with-serialization stage
bjam install --toolset=msvc-7.1 --prefix="E:/__Library__/boost_1_39_0" release --build-type=complete --with-system --with-thread --with-date_time --with-regex --with-serialization stage
LinuX
make lib gcc43

./bjam install --toolset=gcc --prefix="/home/hjz/boost_1_39_0" debug --build-type=complete --with-system --with-thread --with-date_time --with-regex --with-serialization stage


make *.cpp.
c++ -I /home/hjz/boost_1_39_0 example.cpp -o example ~/boost_1_39_0/lib/libboost_regex-gcc43-mt-d-1_39.a

asio
c++ -I /home/hjz/boost_1_39_0 LinuX_HttpClient.cpp -o /
lhc ~/boost_1_39_0/lib/libboost_regex-gcc43-mt-d-1_39.a /
~/boost_1_39_0/lib/libboost_system-gcc43-mt-d-1_39.a /
~/boost_1_39_0/lib/libboost_thread-gcc43-mt-d-1_39.a /
~/boost_1_39_0/lib/libboost_date_time-gcc43-mt-d-1_39.a /
~/boost_1_39_0/lib/libboost_serialization-gcc43-mt-d-1_39.a /
~/boost_1_39_0/lib/libboost_wserialization-gcc43-mt-d-1_39.a


D:/__Library__/boost_1_42_0>bjam --toolset=intel-win32 --prefix="d:/__Library__/boost_1_42_0" debug --build-type=complete --with-thread stage

 

D:/boost_1_42_0>bjam "-sTools=vc-7_1-stlport" "-sSTLPORT_PATH=D:/STLport-5.2.1/STLport-5.2.1/stlport" "-sSTLPORT_VERSION=5.2.1" --prefix="E:/__Library__/boost_1_39_0" debug --build-type=complete --with-thread stage

 

 

1.  到 http://www.stlport.com/ 下载 ,最新的版本是STLport-5.1.3,并解压到
    C:/STLport-5.1.3
2.  解压后, 包含几个目录和文件, 如 doc, src, build 等:
3.  修改"C:/Program Files2/Microsoft Visual Studio .NET 2003/Common7
    /Tools/vsvars32.bat"文件,
    在@set INCLUDE=的后面添加“C:/STLport-5.1.3/stlport;”
    在@set LIB=的后面添加“D:/STLport-5.1.3/lib;”
4.  打开控制台窗口 cmd , 执行:
    cd C:/STLport-5.1.3/stlport
    "C:/Program Files2/Microsoft Visual Studio .NET 2003/Common7/Tools/vsvars32.bat"
    这样,就可以在该控制台窗口里运行 nmake.exe了。
5.  不要关闭控制台窗口,运行如下命令:
    cd  D:/STLport-5.1.3/build/lib            // 切换到make目录
    configure -c msvc71         // 指定编译器.  --help 可以看到其它选项
    nmake /fmsvc.mak      
    nmake /fmsvc.mak install
6.  打开vs2003,选择菜单“工具”-》“选项”,弹出对话框,选择左边列表框的
    “项目”-》“VC目录”:包含文件添加“D:/STLport-5.0-0125/stlport”,
    库文件添加“D:/STLport-5.0-0125/lib”,分别将它们移到最上一条。
    这样,就不必在以后的每个项目中显示指定包含路径和库路径.
7. 测试是否使用了STLPort.
    用 vs2003 新建一个项目. 使用 <vector> .
    确保 项目属性/C/C++代码生成/运行时库 = 单线程 或 单线程调试.
    然后 build 该项目, 会看到大量link错误: 类似
     无法解析的外部符号 "public: bool __thiscall stlpmtx_std::......
    若将 项目属性 改为多线程后, build 成功.
    这说明 STLPort 已经起作用.
    这同时说明, STLPort 库生成的时候只针对多线程. 因此使用 STLPort
    的项目必须使用多线程.
    当然, STLPort 库也可以生成针对单线程的. 在编译STLPort库前,
    查看 configure --help 可以找到答案.

 

using stlport : 5.2.1 : D://STLport-5.2.1//stlport : D://STLport-5.2.1//lib ;

bjam stdlib=stlport --toolset=msvc-7.1 --prefix="D:/boost_1_42_0" debug --build-type=complete --with-thread stage

 

FeatureAllowed valuesNotes
variantdebug,release 
linkshared,staticDetermines if Boost.Build creates shared or static libraries
threadingsingle,multiCause the produced binaries to be thread-safe. This requires proper support in the source code itself.
address-model32,64Explicitly request either 32-bit or 64-bit code generation. This typically requires that your compiler is appropriately configured. Please refer to the section called “C++ Compilers” and your compiler documentation in case of problems.
toolset(Depends on configuration)The C++ compiler to use. See the section called “C++ Compilers” for a detailed list.
include(Arbitrary string)Additional include paths for C and C++ compilers.
define(Arbitrary string)Additional macro definitions for C and C++ compilers. The string should be either SYMBOL or SYMBOL=VALUE
cxxflags(Arbitrary string)Custom options to pass to the C++ compiler.
cflags(Arbitrary string)Custom options to pass to the C compiler.
linkflags(Arbitrary string)Custom options to pass to the C++ linker.
runtime-linkshared,staticDetermines if shared or static version of C and C++ runtimes should be used.

<think>嗯,用户问的是哪些文件需要包含预编译头文件,还有自己定义的头文件是否需要包含。这个问题我得先回忆一下预编译头文件的作用。预编译头文件主要是为了提高编译速度,把常用的头文件预先编译好,这样在后续编译中可以直接复用,减少重复编译的时间。 首先,哪些文件需要包含预编译头呢?一般来说,预编译头文件(比如stdafx.h或pch.h)应该被项目中的源文件包含。特别是那些需要频繁引用大量头文件的源文件,比如主程序文件或者常用的库文件。不过,不同的编译器可能有不同的要求,比如MSVC通常要求每个源文件都包含预编译头,而GCC或Clang可能更灵活一些。 接下来,用户问自己定义的头文件是否需要包含。这里可能需要分情况讨论。如果自定义的头文件内容比较稳定,不经常改动,并且被多个源文件包含,那么可以考虑放到预编译头里,这样可以加速编译。但如果头文件经常修改,或者只是被少数源文件使用,可能不适合放进预编译头,因为每次修改都会导致重新生成预编译头,反而影响效率。 另外,需要注意包含顺序的问题。预编译头文件通常需要放在源文件的最前面,确保编译器先处理它。如果自定义头文件已经被包含在预编译头里,那么源文件就不需要再次包含,否则可能导致重复包含的问题。 可能用户的实际场景是他们在项目中遇到了编译速度慢的问题,想通过预编译头优化,但不确定如何处理自己的头文件。需要提醒他们正确配置编译器的设置,比如在MSVC中指定预编译头的名称,以及确保包含顺序正确。同时,也要权衡是否将自定义头文件加入预编译头,避免不必要的麻烦。 最后,总结一下要点:预编译头适合包含稳定且广泛使用的头文件,自定义的根据情况决定,注意编译器的要求和配置,以及包含顺序的重要性。这样用户应该能更清楚地知道如何处理他们的具体情况了。</think>预编译头文件(如 `stdafx.h``/``pch.h`)的使用规则和范围主要取决于编译器和项目配置,以下是关键要点: #### 需要包含预编译头文件的文件 1. **所有参与预编译的源文件** - 在 MSVC 等编译器中,`.cpp` 源文件通常**必须**在第一行显式包含预编译头文件(如 `#include "pch.h"`),否则会报错。 - 示例: ```cpp // main.cpp 的第一行必须包含预编译头 #include "pch.h" #include <iostream> // 其他代码... ``` 2. **高频使用的稳定头文件** - 若自定义头文件(如 `MyUtils.h`)**内容稳定**且被大量 `.cpp` 文件引用,可以将其加入预编译头(`pch.h`),避免重复解析。 #### 不需要包含的情况 1. **自定义头文件本身** - 若你的 `.h` 文件未被包含在预编译头中,则**不需要**(也不应该)在 `.h` 文件里包含 `pch.h`,否则会破坏头文件的独立性。 - 正确做法:在 `.cpp` 文件中先包含 `pch.h`,再包含自定义头文件。 2. **频繁修改的头文件** - 若头文件内容经常变动(如调试阶段),加入预编译头会导致频繁重新生成 `.pch` 文件,反而降低效率。 #### 编译器差异 | 编译器 | 预编译头文件要求 | 典型配置 | |--------|-----------------------------------|--------------------------| | MSVC | `.cpp` 必须显式包含 `pch.h` | `/Yu` (使用)、`/Yc` (生成) | | GCC/Clang | 可选,通过 `-include` 指定 | `-include pch.h` | #### 最佳实践 1. **预编译头内容**:仅包含**稳定且广泛使用**的库(如 STL、第三方库),自定义头文件谨慎添加。 2. **依赖顺序**:确保 `.cpp` 文件**第一行**是预编译头,之后才是其他头文件。 3. **项目配置**:在 IDE 中正确设置预编译头的生成和使用(如 MSVC 的 `pch.h` 和 `pch.cpp`)。 #### 示例场景 ```cpp // pch.h(预编译头) #include <vector> #include <string> // 稳定第三方库 #include "boost/algorithm/string.hpp" // MyClass.cpp #include "pch.h" // 必须第一行 #include "MyClass.h" // 自定义头文件 // 其他代码... ``` > 总结:自定义头文件是否需要加入预编译头,取决于它的**使用频率**和**稳定性**。大多数情况下,保持预编译头最小化,优先加速系统/第三方库的编译
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值