c++簡單使用boost
阿新 • • 發佈:2019-01-07
準備學習下boost,下午剛剛好把環境裝了下,下面通過用thread為例子:
boost簡單的安裝:
wget https://dl.bintray.com/boostorg/release/1.67.0/source/boost_1_67_0.tar.gz
cd boost_1_67_0
sh bootstrap.sh
./b2
cmake安裝:
sudo tar -zxvf cmake-3.11.tar.gz
cd cmake-3.11
sudo ./bootstrap
sudo make
sudo make install
專案tree:
.
├── build
├── CMakeLists.txt
└── main.cpp
CMakeLists.txt:
PROJECT(HELLO)
include_directories(/home/meixu.zmx/boost_1_67_0)
link_directories(/home/meixu.zmx/boost_1_67_0/stage/lib)
add_executable(llll main.cpp)
target_link_libraries(llll libboost_thread.so libboost_system.so)
main.cpp
#include <boost/thread/thread.hpp> #include <iostream> using namespace std; void task1() { for(int i=0;i<10;i++){ std::cout << "This is task1: "<<i << std::endl; } } void task2(){ for(int i=0;i<10;i++){ std::cout << "This is task2: "<<i << std::endl; } } int main(){ cout<<"hellow world"<<endl; boost::thread thread_1 = boost::thread(task1); boost::thread thread_2 = boost::thread(task2); thread_2.join(); thread_1.join(); return 0; }
編譯專案:
cmake .. && make && ./llll
hellow world This is task2: This is task1: 0 This is task2: 1 This is task2: 2 This is task2: 3 This is task2: 4 This is task2: 5 This is task2: 6 This is task2: 7 This is task2: 8 This is task2: 9 0 This is task1: 1 This is task1: 2 This is task1: 3 This is task1: 4 This is task1: 5 This is task1: 6 This is task1: 7 This is task1: 8 This is task1: 9