Boost 編譯及boost::thread靜態鏈接使用

1)源碼下載

由於項目需要,準備移植到Android平臺上,所以下載了1.49版本boost庫,鑑於瀏覽器速度緩慢,

建議獲取到下載鏈接,使用迅雷下載,服務器上有緩存的!!

下載鏈接如下:

http://nbtelecom.dl.sourceforge.net/project/boost/boost/1.49.0/boost_1_49_0.tar.bz2


2)編譯

解壓省略,靜態編譯,無需安裝

#cd boost_1_49_0

#./bootstrap.sh     (後面添加--prefix=dir可以指定安裝的目錄,默認安裝到/usr/local)

#./b2 (後面添加install將庫安裝到系統目錄下)



3)測試代碼:

#include <boost/thread.hpp>

#include <iostream>


void task1() { 

    // do stuff

    std::cout << "This is task1!" << std::endl;

}


void task2() { 

    // do stuff

    std::cout << "This is task2!" << std::endl;

}


int main (int argc, char ** argv) {

    using namespace boost; 

    thread thread_1 = thread(task1);

    thread thread_2 = thread(task2);


    // do other stuff

    thread_2.join();

    thread_1.join();

    return 0;

}



4)編譯


動態鏈接:


使用安裝目錄:

g++ test.cpp -I /usr/local/include/boost/ -L  /usr/local/lib/ -lboost_thread  -o example


使用原有的生成目錄

g++ test.cpp -I /home/boost/boost_1_49_0/boost -L /home/boost/boost_1_49_0/stage/lib/ -lboost_thread -o example


靜態鏈接:


使用安裝目錄:

g++ test.cpp -I /usr/local/include/boost/ /usr/local/lib/libboost_thread.a -lpthread -o example



使用已有的目錄:

g++ test.cpp -I /home/boost/boost_1_49_0/boost  /home/boost/boost_1_49_0/stage/lib/libboost_thread.a -lpthread  -o example



注意作爲靜態編譯必須指定-lpthread,原因在於libboost_thread.so靜態庫在進行靜態鏈接的時候必須指定POSIX線程庫,否則無法找到pthread函數庫的函數實現,就會出現函數調用的未定義異常,簡單截取

其中的錯誤如下:

undefined reference to `pthread_setspecific'

undefined reference to `pthread_key_create'



參考:

http://blog.csdn.net/lixinneo/article/details/7679996


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章