c++線程庫ZThread在Linux上的安裝

最近比較清閒, 所以看了看c++線程庫ZThread

下載地址
http://zthread.sourceforge.net/download.html

我的平臺是: Linux 222 2.6.18-53.el5xen #1 SMP Wed Oct 10 16:48:44 EDT 2007 x86_64 x86_64 x86_64 GNU/Linux

解壓, 進入解壓目錄
安裝目錄爲: /usr/local/zthread
如果目錄不存在,則建一個: mkdir -p /usr/local/zthread
./configure --prefix=/usr/local/zthread
make
編譯出錯: 出錯提示需要加"-fpermissive"

-------------------------------------------------------
參考網頁: http://bbs.chinaunix.net/viewthread.php?tid=928981
這個是編譯器編譯器版本的問題.印象裏Zthread的作者已經停止開發一段時間了.

g++3.4以後版本的名字查找方法和以前的版本不同,問題這裏有解釋.http://gcc.gnu.org/onlinedocs/gcc/Name-lookup.html.

實際上解決方法編譯結果已經說了.
1. 一種是使用-fpermissive開關來允許老的語法,
用export CXXFLAGS=-fpermissive,然後在configure

2. 另一種方法可以在調用函數時加this.
Guard.h:  this->isDisabled()

3. 還可以把isDisabled()在那個類頭部分重新聲明一下.
using BaseClass<T>::isDisabled();

---------------------------------------------------------

我使用的是第一種方法, 安裝成功後, 修改/usr/local/zthread下的文件include/zthread/Guard.h: 第494行, 就是上面
提到的第2步, 如果不做, 以後調用Guard.h還是會出錯的.

然後 make install, 安裝完成



#######################################
寫一個簡單的測試程序main.cpp,
[code]
#include <iostream>
#include <fstream>
#include <string>
#include <zthread/Thread.h>

using namespace std;
using namespace ZThread;


class ARunnable : public Runnable{
    void run(){
        string str="hello";
        cout << str<<endl;
    }
};

int main(void)
{
    try{
        Thread t(new ARunnable(), true);
        t.wait();
    }catch(Synchronization_Exception& e){
        cerr<<e.what()<<endl;
    }
    return 0;   
}
[/code]

 注意要加入正確的ZThread的include路徑和庫路徑,
我的Makefile文件如下: 可參考
[Makefile]
### ZThread   include路徑
ZTHREAD_INCLUDE = -I"/usr/local/zthread/include"
### ZThread   lib路徑, 包含庫文件 -lZThread
ZTHREAD_LIB            = -L "/usr/local/zthread/lib" -lZThread

APPLPATH     =     .
INCLUDE        =     $(ZTHREAD_INCLUDE)  -I$(APPLPATH)

MAIN_C     =        $(APPLPATH)/main.cpp

CC        =    g++
CFLAG        =     -Wall -g ## -rdynamic

###  生成的可執行文件名
AIM = test

all : $(AIM)

$(AIM) :  main.o
    $(CC)  $(CFLAG) -o  $(AIM)  main.o   /
    $(ZTHREAD_LIB) 


main.o : $(MAIN_C)
    $(CC) $(INCLUDE)  $(CFLAG) -c $(MAIN_C)


### Valgrind: 測試內存泄露情況的,網上有介紹
valgrind:
    valgrind --tool=memcheck --leak-check=full  --show-reachable=yes  /
     -v -q   ./$(AIM)   

clean:
    /rm -rf  *.o   $(AIM)  valgrindLog.txt
[/Makefile]

 編譯通過, 運行錯誤:
./test: error while loading shared libraries: libZThread-2.3.so.2: cannot open shared object file: No such file or directory
把 libZThread-2.3.so.2  cp 到/usr/lib目錄,然後,然後自然就一切OK了

此錯誤參考http://www.cnblogs.com/WuErPIng/archive/2005/03/05/113553.html




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