C++ Boost Usage - Master C++ Tutorial

Boost

Ok, I can not using Chinese right now, so....Please let me using English to write this article. The main character of this tutorial is Boost. A library in c++ and supported by the comitte of official C++. However, if you are a newbie in C++, it's really very hard to work very well with Boost, something same with OpenCV.

In this tutorial I will demonstrate how to build a test project using Boost..
If you come into errors like this:

 undefined reference to `boost::archive::detail::basic_oarchive::~basic_oarchive()'
 undefined reference to `boost::serialization::typeid_system::extended_type_info_typeid_0::extended_type_info_typeid_0(char const*)'
/usr/include/boost/serialization/extended_type_info_typeid.hpp:89: undefined reference to `boost::serialization::typeid_system::extended_type_info_typeid_0::type_register(std::type_info const&)'
/usr/include/boost/serialization/extended_type_info_typeid.hpp:90: undefined reference to `boost::serialization::extended_type_info::key_register() const'
/usr/include/boost/serialization/extended_type_info_typeid.hpp:87: undefined reference to `boost::serialization::typeid_system::extended_type_info_typeid_0::~extended_type_info_typeid_0()'
../libscene_segmentation.a(utils_segmentation.cpp.o): In function `boost::serialization::extended_type_info_typeid >::~extended_type_info_typeid()':
/usr/include/boost/serialization/extended_type_info_typeid.hpp:93: undefined reference to `boost::serialization::extended_type_info::key_unregister() const'
/usr/include/boost/serialization/extended_type_info_typeid.hpp:94: undefined reference to `boost::serialization::typeid_system::extended_type_info_typeid_0::type_unregister()'

or something really like God Book:

o._ZTVN5boost13serialization6detail17singleton_wrapperINS0_25extended_type_info_typeidIN5Eigen6Ma

you are just find this tutorial to solve it!

Like's Let it Run

Here maybe the reason why you got this:

  • You are not install boost properly;
  • You are using a low version cmake, cmake3.8 could be more proper;
  • Your CMakeLists.txt not write properly.

Ok, if you are not installed boost, please run this to install it:

cd ~
mkdir Downloads
cd ~/Downloads
git clone --recursive https://github.com/boostorg/boost.git
cd boost
git checkout develop # or whatever branch you want to use
./bootstrap.sh
./b2 headers
./b2
sudo ./b2 install

OK, this is the official way to install boost, it can work without errors because boost almost build with Header-Only, but it still need a while.

Once you installed boost, make sure: /usr/local/include/boost you can find many boost header files. And then, you gonna write your CMakeLists.txt like this:

cmake_minimum_required(VERSION 3.7)
project(boost_learn)

set(CMAKE_CXX_STANDARD 11)


# you have to write the required components of boost, everything is just work fine.
find_package( Boost REQUIRED COMPONENTS system filesystem regex)
include_directories( ${Boost_INCLUDE_DIRS} )

SET(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
SET(BUILD_SHARED_LIBRARIES OFF)



set(SOURCES main.cpp)
add_executable(boost_learn ${SOURCES})

target_link_libraries(boost_learn
        ${Boost_LIBRARIES}
        ${Boost_FILESYSTEM_LIBRARY}
        ${Boost_SYSTEM_LIBRARY})

At this point, you must pay attention that, boost_learn is my project name, you can clone from here.
You must add all boost filesystem system path at target_link_libraries. Once you done, you can build boost without any mistake!!!

你可能感兴趣的:(C++ Boost Usage - Master C++ Tutorial)