Ubuntu下编译boost1.53


1.从boost官网下载boost 1.53.0

2.执行 apt-get install build-essential (目的是为了安装好象gcc,g++等一些工具进行编译)
      注:我是在root用户下进行的操作,普通用户执行 sudo apt-get install build-essential 


3.解压boost库到/usr/share/
      將下载好的boost 1.53.0.tar.gz(我用的是这个版本) 复制到/usr/share/ 目录下,然后解压,会自动生成一个boost_1_53_0这个文件夹(或者用命令解压,这里就不提了,可以自己去查解压命令)

4.编译bjam(这个东东是用来编译boost库的)
      编译之前先做如下操作,由于这版本是编译msvc版的bjam,我们把它改成gcc版的bjam......
      进入boost1.53.0解压目录找到bootstrap.bat 用记事本打开,然后修改:
      第13行將call .\build.bat > ..\..\..\bjam.log 改为call .\build.bat gcc > ..\..\..\bjam.log
     第33行將set toolset=msvc改为set toolset=gcc
     保存退出,然后双击 bootstrap.sh在终端中执行,稍等片刻会在boost_1_53_0中生成bjam

     或者直接生成bjam。在terminal中输入

                      ./bootstrap.sh

5.编译boost库: 在终端中进入/usr/share/boost_1_53_0目录
     在终端中输入 cd /usr/share/boost_1_53_10
     然后我在root用户下输入 ./bjam toolset=gcc --layout=tagged --build-type=complete stage 进行完全编译
     (普通用户的话执行: sudo ./bjam toolset=gcc --layout=tagged --build-type=complete stage )
      经过漫长的等 待,命令执行完毕后,将会生成所有版本的库,并存储在:
         /usr/share/boost_1_53_0/stage 下
6.boost 已经编译好了,下面调用boost库
    1.在/usr/include/下生成一个boost库的include文件夹连接:
       ln -s /usr/share/boos t_1_53_0/boost /usr/include/boost
    2.在/usr/lib/ 下生成所有boost编译出的lib库文件的对应连接 切换到stage目录下,执行
       find $PWD/lib/*.* -type f -exec ln -s {} /usr/lib/ \;
       (普通用户执行: sudo find $PWD/lib/*.* -type f -exec ln -s {} /usr/lib/ \; )

添加环境变量(刚改完要重启或者注销一下来更新刚修改过的环境变量)
   两种方法:
   (1)修改/etc/profie文件 末尾添加
           export BOOST_INCLUDE=/usr/local/include/boost-1_52
           export BOOST_LIB=/usr/local/lib
   (2)在/etc/profile.d/ 中新建一个shell文件boost.sh
            #!/bin/sh 
            export BOOST_INCLUDE=/usr/local/include/boost-1_52
            export BOOST_LIB=/usr/local/lib

测试:
test.cpp

#include<boost/lexical_cast.hpp>
#include <iostream>
int main()
{
       using boost::lexical_cast;
       int a =lexical_cast<int>("123");
       double b =lexical_cast<double>("123.12");
       std::cout<<a<<std::endl;
       std::cout<<b<<std::endl;
       return 0;
}

执行完以上操作就算ok了



=================================【转载】============================================
http://www.cppfun.com/2012/03/31/codeblocks-with-boost-under-linux.html

Set up a Code::Blocks global variable for Boost

global variable

global variable

This step only needs to be performed once, after whichthe globalvariable you’ve created will be available for anyproject.

  • Open the Settings menu and select “Global variables…”
  • Click the “New” button next to the Current variable list,specify a name like “boost”, and hit OK
  • In the “base” field of the Builtin fields section, browse forthe base of your Boost location — the path you specified in the–prefix option of the build command(*mandatory)
  • In the “include” field, browse for the header files location —the path can same with the “base” or like “/usr/local/include” or/usr/include.
  • In the “lib” field, browse for the “stage/lib” subfolder ofyour Boost installation — it should be the path in the “base” fieldwith “/stage/lib” tacked on. (This is the folder that containseither multiple lib*.aor *.lib files.)[maybe a path like "/usr/local/lib" or "/usr/lib"].
  • Hit the Close button to save your global variable

Add Boost search directories to your project

search directories - compiler

search directories - compiler

search directories - linker

search directories - linker

  • Right-click your project’s name in the Projects section of theManagement window and select “Build options…”
  • Highlight the root of your project in the tree on the left sideof the Project build options window
  • Select the “Search directories” tab
  • With the “Compiler” subtab selected, click the Add button,enter “$(#boost.include)” (without thequotes), and hit OK
  • With the “Linker” subtab selected, click the Add button, enter“$(#boost.lib)”(without the quotes), and hit OK

Include Boost headers and link with Boost libraries

Your project is now ready to use the Boost libraries. For eachlibrary you want to use, do the following:

  • #include<boost/*.hpp> in yoursource file
  • In your project’s build options, highlight the root of yourproject, select the “Linker settings” tab, and add“boost_*” to your Link libraries[or another way under"Other link options" something like "-lboost_system -lboost_thread-..."]

Add Other libraries into Code::Blocks

The same way like boost, just do as above steps, enjoy.


猜你喜欢

转载自blog.csdn.net/myemailsz/article/details/8770119