Ubuntu 16.04源碼編譯boost庫 編寫CMakeLists.txt | compile boost 1.66.0 from source on ubuntu 16.04

本文首發於我的博客kezunlin.me/post/d5d4a4…,歡迎閱讀!html

compile boost 1.66.0 from source on ubuntu 16.04python

Guide

apt-get

# 1.58 for ubuntu 16.04
    sudo apt-get install libboost-all-dev複製代碼

compile from source

sudo apt-get -y purge libboost-all-dev複製代碼
wget https://dl.bintray.com/boostorg/release/1.66.0/source/boost_1_66_0.tar.gz複製代碼
tar xzvf boost_1_66_0.tar.gz
    cd boost_1_66_0/複製代碼
sudo apt-get update
    sudo apt-get install build-essential g++ python-dev autotools-dev libicu-dev build-essential libbz2-dev複製代碼
./bootstrap.sh --prefix=/usr/local/複製代碼
./b2 --help複製代碼
./b2 -j8 variant=release link=shared threading=multi runtime-link=shared
    sudo ./b2 -j8 install 複製代碼

thread example

thread_demo.cpp

//#include <boost/thread/thread.hpp>
//#include <boost/thread/mutex.hpp>
//#include <boost/bind/bind.hpp>
#include <iostream>

#include <boost/thread.hpp>
#include <boost/bind.hpp>

boost::mutex io_mutex;

void count(int id)
{
    for (int i = 0; i < 10; i++)
    {
        boost::mutex::scoped_lock lock(io_mutex);
        std::cout << id << ":" << i << std::endl;
    }
}

int main(int argc, char* argv[])
{
    boost::thread thrd1(boost::bind(&count, 1));
    boost::thread thrd2(boost::bind(&count, 2));
    thrd1.join();
    thrd2.join();
    return 0;
}複製代碼

CMakeLists.txt

cmake_minimum_required (VERSION 2.6)

project (thread_main)
enable_language(C)
enable_language(CXX)

set(Boost_USE_RELEASE_LIBS ON)
#set(Boost_USE_STATIC_LIBS OFF) # use .a or .so file
set(Boost_USE_MULTITHREAD ON)

# Find Boost package 1.5.8 or 1.6.6
find_package(Boost 1.5.8 REQUIRED COMPONENTS serialization date_time system filesystem thread timer)
include_directories(${Boost_INCLUDE_DIRS})

add_executable (thread_main
    thread_main.cpp
)

target_link_libraries (thread_main 
    ${Boost_LIBRARIES}
)複製代碼

thread pool

see thread pool with boostios

Reference

History

  • 20180131: created.

Copyright

相關文章
相關標籤/搜索