轉載:https://blog.csdn.net/XCCCCZ/article/details/91171470
參考:https://blog.csdn.net/zhumingyuan111/article/details/70195855
1. Download
- http://thrift.apache.org/頁面右邊Download個thrift-x.xx.0.tar.gz壓縮包,到Linux下解壓.
- git clone https://github.com/apache/thrift.git下載源碼到本地
2. Install
按照 http://thrift.apache.org/docs/install/ 到提示,根據你到操作系統選擇進入對應到頁面看需要安裝哪些所需要到依賴包,比如我時在Ubuntu上編譯和安裝Thrift,根據http://thrift.apache.org/docs/install/debian提示,得執行下面的命令保證所需要的包被安裝:
sudo apt-get install automake bison flex g++ git libboost-all-dev libevent-dev libssl-dev libtool make pkg-config
然後按照http://thrift.apache.org/docs/BuildingFromSource頁面裏到提示執行(執行configure時根據需要增加參數選項):
./bootstrap.sh (可以不執行,我沒有執行)
./configure
make
make install
如果你的環境恰好滿足要求那麼上面的步驟就可順利執行完,但是一般可能都會出現這樣那樣都錯誤,我在Ubuntu Linux 16.04 LTS下執行到make時就遇到了多個錯誤,查了網上有些答案都已過時或者純屬瞎說八道誤導人,只好自己一一琢磨予以解決,分享出來以供其他人蔘考:
(1)
libtool: compile: g++ -std=c++11 -DHAVE_CONFIG_H -I. -I../.. -I../../lib/cpp/src/thrift -I../../lib/c_glib/src/thrift -I/usr/include -I./src -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -I/usr/include/x86_64-linux-gnu/qt5/QtNetwork -I/usr/include/x86_64-linux-gnu/qt5 -I/usr/include/x86_64-linux-gnu/qt5/QtCore -I/usr/include/x86_64-linux-gnu/qt5 -fPIC -Wall -Wextra -pedantic -g -O2 -MT src/thrift/qt/libthriftqt5_la-moc__TQTcpServer.lo -MD -MP -MF src/thrift/qt/.deps/libthriftqt5_la-moc__TQTcpServer.Tpo -c src/thrift/qt/moc__TQTcpServer.cpp -fPIC -DPIC -o src/thrift/qt/.libs/libthriftqt5_la-moc__TQTcpServer.o
src/thrift/qt/moc__TQTcpServer.cpp:13:2: error: #error "This file was generated using the moc from 4.8.7. It"
#error "This file was generated using the moc from 4.8.7. It"
^
src/thrift/qt/moc__TQTcpServer.cpp:14:2: error: #error "cannot be used with the include files from this version of Qt."
#error "cannot be used with the include files from this version of Qt."
^
src/thrift/qt/moc__TQTcpServer.cpp:15:2: error: #error "(The moc has changed too much.)"
#error "(The moc has changed too much.)"
^
src/thrift/qt/moc__TQTcpServer.cpp:63:7: error: ‘QMetaObjectExtraData’ does not name a type
const QMetaObjectExtraData apache::thrift::async::TQTcpServer::staticMetaObject
^
src/thrift/qt/moc__TQTcpServer.cpp:65:2: warning: extra ‘;’ [-Wpedantic]
};
^
src/thrift/qt/moc__TQTcpServer.cpp:69:57: error: ‘staticMetaObjectExtraData’ was not declared in this scope
qt_meta_data_apache__thrift__async__TQTcpServer, &staticMetaObjectExtraDa
^
src/thrift/qt/moc__TQTcpServer.cpp: In member function ‘virtual const QMetaObject* apache::thrift::async::TQTcpServer::metaObject() const’:
src/thrift/qt/moc__TQTcpServer.cpp:78:71: error: conditional expression between distinct pointer types ‘QDynamicMetaObjectData*’ and ‘const QMetaObject*’ lacks a cast
return QObject::d_ptr->metaObject ? QObject::d_ptr->metaObject : &staticMetaO
^
Makefile:1419: recipe for target 'src/thrift/qt/libthriftqt5_la-moc__TQTcpServer.lo' failed
make[4]: *** [src/thrift/qt/libthriftqt5_la-moc__TQTcpServer.lo] Error 1
make[4]: Leaving directory '/home/fychen/thrift-0.12.0/lib/cpp'
Makefile:1656: recipe for target 'all-recursive' failed
make[3]: *** [all-recursive] Error 1
make[3]: Leaving directory '/home/fychen/thrift-0.12.0/lib/cpp'
Makefile:582: recipe for target 'all-recursive' failed
make[2]: *** [all-recursive] Error 1
make[2]: Leaving directory '/home/fychen/thrift-0.12.0/lib'
Makefile:664: recipe for target 'all-recursive' failed
make[1]: *** [all-recursive] Error 1
make[1]: Leaving directory '/home/fychen/thrift-0.12.0'
Makefile:578: recipe for target 'all' failed
make: *** [all] Error 2
按照網上有人說的辦法,把出錯的文件改名再編譯:
mv ./lib/cpp/src/thrift/qt/moc_TQTcpServer.cpp ./lib/cpp/src/thrift/qt/moc_TQTcpServer.cpp.bak
發現根本不行,因爲這個文件時編譯時由qt moc自動生成的(Qt的語法是在標準C++上做了擴展,直接使用C++編譯器編譯是不行的,編譯前需要先用Qt的Meta Object Compiler對使用了Qt擴展語法對語句進行翻譯生成符合C++語法標準的源碼文件),改名了又會生成新的文件,照樣在這裏出錯!https://issues.apache.org/jira/browse/THRIFT-1828說了使用--with-qt=no把支持qt這部分disable掉,這樣編譯能過,但我不想功能少了,於是回頭仔細看錯誤信息,發現這是在生成qt5版的.o文件libthriftqt5_la-moc__TQTcpServer.o時出錯,而且moc_TQTcpServer.cpp裏出錯的地方是頭部:
#elif Q_MOC_OUTPUT_REVISION != 63
#error "This file was generated using the moc from 4.8.7. It"
#error "cannot be used with the include files from this version of Qt."
#error "(The moc has changed too much.)"
#endif
感覺原因是在編譯Qt5版的.o文件時使用了Qt4的環境,於是使用Q_MOC_OUTPUT_REVISION作爲關鍵詞在/usr下搜索,果然發現我係統裏安裝了Qt4和Qt5兩個版本(應該時安裝其他軟件時依賴安裝的,所以之前沒注意到),Qt4的Q_MOC_OUTPUT_REVISION值是63,而Qt5的Q_MOC_OUTPUT_REVISION值是67:
/usr/include/x86_64-linux-gnu/qt5/QtCore/qobjectdefs.h:#ifndef Q_MOC_OUTPUT_REVISION
/usr/include/x86_64-linux-gnu/qt5/QtCore/qobjectdefs.h:#define Q_MOC_OUTPUT_REVISION 67
/usr/include/PySide2/pyside2_global.h:#ifndef Q_MOC_OUTPUT_REVISION
/usr/include/PySide2/pyside2_global.h:#define Q_MOC_OUTPUT_REVISION 67
/usr/include/qt4/Qt/qobjectdefs.h:#ifndef Q_MOC_OUTPUT_REVISION
/usr/include/qt4/Qt/qobjectdefs.h:#define Q_MOC_OUTPUT_REVISION 63
/usr/include/qt4/QtCore/qobjectdefs.h:#ifndef Q_MOC_OUTPUT_REVISION
/usr/include/qt4/QtCore/qobjectdefs.h:#define Q_MOC_OUTPUT_REVISION 63
然後看了一下thrift-0.12.0/lib/cpp/src/thrift/qt/.deps/下確實有Qt4和Qt5兩個版本的TQTcpServer.Plo文件:
libthriftqt_la-moc_TQTcpServer.Plo、libthriftqt5_la-moc__TQTcpServer.Plo
這下確認了原因就是在生成Qt5版的.o文件時使用了系統默認使用的Qt4環境,所以把Qt5版的disable掉不生成就應該可以了,於是去configure文件裏查找發現--with-qt5=yes和--with-qt4=yes,暈倒,原來默認兩個版本都打開了,於是執行
./configure --with-qt5=no
生成的Makefile文件裏的只設置Qt4了:
Makefile
QT5_CFLAGS =
QT5_LIBS =
QT5_MOC =
QT_CFLAGS = -DQT_SHARED -I/usr/include/qt4 -I/usr/include/qt4/QtNetwork -I/usr/include/qt4 -I/usr/include/qt4/QtCore
QT_LIBS = -lQtNetwork -lQtCore
QT_MOC = /usr/bin/moc-qt4
再執行make時這個問題就消失了,
下面又出現了新問題:
(2)
x86_64-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -fPIC -Isrc -I/usr/include/python3.5m -c src/ext/module.cpp -o build/temp.linux-x86_64-3.5/src/ext/module.o
cc1plus: warning: command line option ‘-Wstrict-prototypes’ is valid for C/ObjC but not for C++
src/ext/module.cpp:20:20: fatal error: Python.h: No such file or directory
compilation terminated.
通過-I/usr/include/python3.5m發現這裏使用的時python3.5m,而我的Linux下雖然又python3而/usr/include/python3.5m/下卻是空的,應該時沒有安裝dev文件,於是執行
sudo apt-get install python3.5-dev
後,/usr/include/python3.5m/Python.h文件出現了,再執行make時這個問題消失了,但是總是
(3) 運行進入SBCL環境(執行Lisp語言程序的環境),我不需要它支持Lisp,關掉CL選項應該就可以了,於是再執行:
./configure --with-qt5=no --with-cl=no
make
好了,問題都消失了,編譯成功,再執行(注意加sudo,不然會報permission方面的錯)
sudo make install
把文件安裝到:
/usr/local/include/thrift
/usr/local/lib/
/usr/local/bin/
注意這裏默認是把thrift安裝到了/usr/local下,所以在執行你使用thrift生成並編譯出來的文件前,注意修改ld config或者修改.bashrc添加:
export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
否則,運行程序時會報錯:
./server: error while loading shared libraries: libthrift-0.12.0.so: cannot open shared object file: No such file or directory
這個libthrift-0.12.0.so就是在/usr/local/lib下:/usr/local/lib/libthrift-0.12.0.so
至此,thrift-0.12.0完全可用了。
原文鏈接:https://blog.csdn.net/XCCCCZ/article/details/91171470