boost基礎環境搭建

由於如今手上的老的基類庫常常出現丟包,以及從ServiceAClient 發送消息到 ServiceBServer時出現消息失敗的狀況,以及現有的莫名其妙的內存泄露的問題,以及目前仍是c++0x,準確地說,是c with class的寫法,並且支持windows server。html

因此,如今想擁抱c++11,並解決上述問題。ios

選擇Boost.Asio,同樣是隨便找了個庫,有網友說庫寫的不錯,就拿來用了,內部原理沒看,只知道跨平臺,支持c++11.c++

1.下載boost軟件包
https://www.boost.org/users/download/bootstrap

到下載地址裏下載了1.68.0windows

而後放到某目錄下,tar -zxvf boost1.68.0.tar.gzcentos

2.安裝前配置
現有環境,centos 64bit 6.10
進入boost目錄後,運行./bootstrap.shapp

3.編譯
執行bootstrap.sh後,會在當前目錄下生成b2文件
在這裏執行: ./b2進行編譯ide

4.安裝
sudo ./b2 install測試

不加sudo會報權限錯誤等問題。ui

5.測試
編寫代碼

#include <iostream>
#include <boost/asio.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>

int main()
{
    boost::asio::io_service io;
    boost::asio::deadline_timer t(io, boost::posix_time::seconds(5));
    t.wait();
    std::cout << "Hello, world!" << std::endl;

    return 0;
}

代碼解釋:
Timer.1 - Using a timer synchronously
Source listing for Timer.1
This tutorial program introduces asio by showing how to perform a blocking wait on a timer.

We start by including the necessary header files.

All of the asio classes can be used by simply including the "asio.hpp" header file.

#include <iostream>
#include <boost/asio.hpp>

Since this example uses timers, we need to include the appropriate Boost.Date_Time header file for manipulating times.

#include <boost/date_time/posix_time/posix_time.hpp>

All programs that use asio need to have at least one io_service object. This class provides access to I/O functionality. We declare an object of this type first thing in the main function.

int main()
{
  boost::asio::io_service io;

Next we declare an object of type boost::asio::deadline_timer. The core asio classes that provide I/O functionality (or as in this case timer functionality) always take a reference to an io_service as their first constructor argument. The second argument to the constructor sets the timer to expire 5 seconds from now.

boost::asio::deadline_timer t(io, boost::posix_time::seconds(5));

In this simple example we perform a blocking wait on the timer. That is, the call to deadline_timer::wait() will not return until the timer has expired, 5 seconds after it was created (i.e. not from when the wait starts).

A deadline timer is always in one of two states: "expired" or "not expired". If the deadline_timer::wait() function is called on an expired timer, it will return immediately.

t.wait();

Finally we print the obligatory "Hello, world!" message to show when the timer has expired.

std::cout << "Hello, world!" << std::endl;

  return 0;
}

編譯運行:
g++ -Wall -std=c++14 test_xiaoliuzi.cpp -o xtest_xiaoliuzi -lboost_system -lboost_date_time

須要連接動態連接庫:
若是不連接動態連接庫,編譯會報錯:
/tmp/ccRacuCr.o: In function boost::system::system_category()': test_xiaoliuzi.cpp:(.text._ZN5boost6system15system_categoryEv[_ZN5boost6system15system_categoryEv]+0x1): undefined reference toboost::system::detail::system_category_instance'
/tmp/ccRacuCr.o: In function boost::system::generic_category()': test_xiaoliuzi.cpp:(.text._ZN5boost6system16generic_categoryEv[_ZN5boost6system16generic_categoryEv]+0x1): undefined reference toboost::system::detail::generic_category_instance'
/tmp/ccRacuCr.o: In function boost::asio::detail::posix_event::posix_event()': test_xiaoliuzi.cpp:(.text._ZN5boost4asio6detail11posix_eventC2Ev[_ZN5boost4asio6detail11posix_eventC5Ev]+0x3e): undefined reference topthread_condattr_setclock'
/tmp/ccRacuCr.o: In function boost::asio::detail::posix_thread::~posix_thread()': test_xiaoliuzi.cpp:(.text._ZN5boost4asio6detail12posix_threadD2Ev[_ZN5boost4asio6detail12posix_threadD5Ev]+0x26): undefined reference topthread_detach'
/tmp/ccRacuCr.o: In function boost::asio::detail::posix_thread::join()': test_xiaoliuzi.cpp:(.text._ZN5boost4asio6detail12posix_thread4joinEv[_ZN5boost4asio6detail12posix_thread4joinEv]+0x2b): undefined reference topthread_join'
/tmp/ccRacuCr.o: In function boost::asio::basic_deadline_timer<boost::posix_time::ptime, boost::asio::time_traits<boost::posix_time::ptime> >::basic_deadline_timer(boost::asio::io_context&, boost::posix_time::time_duration const&)': test_xiaoliuzi.cpp:(.text._ZN5boost4asio20basic_deadline_timerINS_10posix_time5ptimeENS0_11time_traitsIS3_EEEC2ERNS0_10io_contextERKNS2_13time_durationE[_ZN5boost4asio20basic_deadline_timerINS_10posix_time5ptimeENS0_11time_traitsIS3_EEEC5ERNS0_10io_contextERKNS2_13time_durationE]+0x33): undefined reference toboost::system::detail::system_category_instance'
/tmp/ccRacuCr.o: In function boost::asio::basic_deadline_timer<boost::posix_time::ptime, boost::asio::time_traits<boost::posix_time::ptime> >::wait()': test_xiaoliuzi.cpp:(.text._ZN5boost4asio20basic_deadline_timerINS_10posix_time5ptimeENS0_11time_traitsIS3_EEE4waitEv[_ZN5boost4asio20basic_deadline_timerINS_10posix_time5ptimeENS0_11time_traitsIS3_EEE4waitEv]+0x18): undefined reference toboost::system::detail::system_category_instance'
/tmp/ccRacuCr.o: In function boost::asio::detail::deadline_timer_service<boost::asio::time_traits<boost::posix_time::ptime> >::destroy(boost::asio::detail::deadline_timer_service<boost::asio::time_traits<boost::posix_time::ptime> >::implementation_type&)': test_xiaoliuzi.cpp:(.text._ZN5boost4asio6detail22deadline_timer_serviceINS0_11time_traitsINS_10posix_time5ptimeEEEE7destroyERNS7_19implementation_typeE[_ZN5boost4asio6detail22deadline_timer_serviceINS0_11time_traitsINS_10posix_time5ptimeEEEE7destroyERNS7_19implementation_typeE]+0x1b): undefined reference toboost::system::detail::system_category_instance'
collect2: error: ld returned 1 exit status

若是隻連接-lboost_system, 沒有連接-lboost_date_time,編譯不會報錯,可是運行會報錯:

./xtest_xiaoliuzi: error while loading shared libraries: libboost_system.so.1.68.0: cannot open shared object file: No such file or directory

用到的輔助命令:
爲了查看當前的開發機器上是否安裝過其餘版本的boost,須要用到:
[xiaoliuzi@ boost_1_68_0]$ sudo /sbin/ldconfig -p | grep boost_thread
libboost_thread.so.1.68.0 (libc6,x86-64) => /usr/local/lib/libboost_thread.so.1.68.0
libboost_thread.so (libc6,x86-64) => /usr/local/lib/libboost_thread.so
編譯或者運行出錯也有可能會是其餘版本同時存在致使的。

Reference: https://www.boost.org/users/download/ https://www.boost.org/doc/libs/1_68_0/more/getting_started/unix-variants.html#install-boost-build https://www.boost.org/doc/libs/1_63_0/doc/html/boost_asio/tutorial.html https://blog.csdn.net/big_bit/article/details/51321251

相關文章
相關標籤/搜索