安裝 Boost

# ./bootstrap.sh

編輯 project-config.jam 做細部設定。例如指定使用非系統預設的編譯器:

using gcc : : /full/path/to/g++ ;
# ./bjam

Windows

Boost 使用範例

FreeBSD 請到 /usr/local/share/doc/boost/libs 查看範例檔。

Boost.Lambda

Boost.Iostreams

Boost.uBLAS

Boost.Thread

Boost.MPI

系統要先安裝 MPI 函式庫才能安裝 Boost.MPI。。
目前 FreeBSD ports 中的 devel/boost-libs/Makefile 不會安裝 Boost.MPI。
mpi.cpp
#include <boost/mpi/environment.hpp>
#include <boost/mpi/communicator.hpp>
#include <iostream>
namespace mpi = boost::mpi;
 
int main(int argc, char* argv[]) 
{
  mpi::environment env(argc, argv);
  mpi::communicator world;
  std::cout << "I am process " << world.rank() << " of " << world.size()
            << "." << std::endl;
  return 0;
}
# g++45 -I/usr/local/include/ -L/usr/local/lib/ mpi.cpp -lboost_mpi -lboost_serialization
# mpd&
# mpiexec -n 2 ./a.out

Boost.Graph

Boost.Program_options

Boost.Iostreams

Boost.Signals

Boost.Signals 和 Linux 中的 signal 並不相干。Boost.Signals 是用來實現 observer pattern 或是 publisher-subscriber mechanism

Boost.Test

Boost.Test 的使用可參考 Boost Test Library Tutorial

test.cpp
#define BOOST_AUTO_TEST_MAIN
 
#include <boost/test/unit_test.hpp>
 
int add(int i, int j)
{
  return i + j;
}
 
BOOST_AUTO_TEST_SUITE(minimal_test)
 
BOOST_AUTO_TEST_CASE(my_test)
{
  BOOST_CHECK(add(2, 2) == 5);
 
  BOOST_REQUIRE(add(2, 2) == 4);
 
  if(add(2, 2) != 4)
    BOOST_ERROR("oops!");
 
  if(add(2, 2) != 4)
    BOOST_FAIL("oops!");
 
  if(add(2, 2) != 4)
    throw "oops!";
 
  BOOST_CHECK_MESSAGE(add(2, 2) == 4, "add(..) result: " << add(2, 2));
 
  BOOST_CHECK_EQUAL(add(2, 2), 4);
}
 
BOOST_AUTO_TEST_SUITE_END()
g++ -I /usr/local/include/ test.cpp -lboost_test_exec_monitor

Boost.Python

py.cpp
#include <iostream>
#include <string>
#include <boost/python.hpp>
 
using namespace boost::python;
 
int main()
{
  Py_Initialize();
  object main_ns = import("__main__").attr("__dict__");
  std::string str = "for x in range(1,5):\n"
                          "\tprint x";
  exec(str.c_str(), main_ns);
}
g++ -I /usr/local/include/python2.6/ py.cpp -lpython2.6 -lboost_python
hello.cpp
#include <iostream>
#include <string>
 
std::string greet() { return "hello, world"; }
 
#include <boost/python.hpp>
 
using namespace boost::python;
 
BOOST_PYTHON_MODULE(hello) {
 
  def("greet", greet);
}
g++ -fPIC -I/usr/local/include/python2.6 -I/usr/local/include/ -c hello.cpp -o hello.o
g++ -fPIC -shared -Wl,--export-dynamic -L/usr/lib hello.o -lpython2.6 -lboost_python -o hello.so
import hello
print hello.greet();
help(hello)

參考書籍

外部連結

登录