boost library usage

本文详细介绍了如何从Boost官方网站下载Boost库,并针对Windows、Ubuntu/Linux及macOSX等不同操作系统进行解压、构建bjam文件、编译库文件的具体步骤。此外,还提供了两种链接库的方法,并解释了静态链接和动态链接的区别。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. download the boost library from the offical website.

2. Unzip the file

    For windows, use 7zip for .7z file or use zip for .zip file.

    For ubuntu/linux/mac os x, use command: tar xjf boost_1_xx_x.tar.bz2 -f boost_1_xx_x  or tar zxf boost_1_xx_x.tar.gz -f boost_1_xx_x

3. Create the bjam/bj2, run the command by the script will create bjam and bj2. both two files are the same to build the libraries.

    For windows, run the "bootstrap.bat".

    For ubuntu/linux/max os x, run the "./bootstrap.sh",

4. Build the library.

    For visual studio, please use the command line tool provided by the vs. For example, for visual studio 2010,

    mkdir msvc

    ./bjam --toolset=msvc-10.0 --build-type=complete --with-filesystem --stagedir=msvc --disable-filesystem2

    For mingw/cybwin on windows, please make sure the compile binaries for mingw, gcc, could be foudn in command line. that means the environment variables contains the path for mingw.

    mkdir mingw

   ./bjam --toolset=gcc --build-type=complete --with-filesystem --stagedir=mingw --disable-filesystem2

   For ubunt/linux/mac os x's gcc, you should use the layout flag to named the libraries with version of boost.

   mkdir gcc

   ./bjam --toolset=gcc --build-type=complete --with-filesystem --stagedir=gcc --disable-filesystem2 --layout=versioned

   For mac os x's darwin

   ./bjam --toolset=darwin --build-type=complete --with-filesystem --stagedir=darwin --disable-filesystem2

Note:

   ./bjam --help

   ./bjam --show-libraries

5. Compile and link

    Create a file to use the file system or other libraries.

    include the boost/filesystem.hpp

    There are two main ways to link to libraries:

  1. You can specify the full path to each library:

    g++ main.cpp -o main -I/home/rogerluo/boost_1_49_0/ /home/user/boost_1_49_0/gcc/lib/libboost_filesystem.so /home/user/boost_1_49_0/gcc/lib/libboost_system.so
    
  2. You can separately specify a directory to search (with -Ldirectory) and a library name to search for (with-llibrary, dropping the filename's leadinglib and trailingsuffix (.a in this case):

    g++ main.cpp -o main -I/home/user/boost_1_49_0/ -L/home/rogerluo/boost_1_49_0/gcc/lib -lboost_system -lboost_filesystem
    

    both these two ways are to dynamically link to libraries, if you want to link statically the libraries,  for linking directly the files,

             g++ main.cpp -o main -I/home/rogerluo/boost_1_49_0/ /home/user/boost_1_49_0/gcc/lib/libboost_filesystem.a /home/user/boost_1_49_0/gcc/lib/libboost_system.a 

     on ubuntu/linix system, (.so) is dynamic library, (.a) is static library.

     For linking the file statically in the second way, declare the -static to make the linker to know using statically linking.

             g++ main.cpp -o main -I/home/user/boost_1_49_0/ -L/home/user/boost_1_49_0/gcc/lib -lboost_system -lboost_filesystem -static

Note:

Using the second way to link libraries,

For visual studio on window, change the linker setting: Additional library directories: $(BOOST_DIR)\msvc\lib\

For mingw on windows(using codeblock), settings->compiler and debuger, choose a compiler and under the "Linkder settings" add "libboost_system-mgw47-1_49" and "libboost_filesystem-mgw47-1_49", if you want to statically link to library, input "-static" into the "Link options".


make sure the LD_LIBRARY_PATH can contain path of boost library, modify the .profile under the your home directory, put this script at the end of the file.

export LD_LIBRARY_PATH=/home/user/boost_1_49_0/gcc/lib:$LD_LIBRARY_PATH




    

   



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值