Thrift最新版thrift-0.12.0在Ubuntu 16.04下的build和install及问题解决

Thrift当前最新版是0.12.0,到http://thrift.apache.org/页面右边Download个thrift-0.12.0.tar.gz压缩包,到Linux下解压,或者执行

git clone https://github.com/apache/thrift.git下载源码到本地,按照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完全可用了。

发布了61 篇原创文章 · 获赞 90 · 访问量 11万+

猜你喜欢

转载自blog.csdn.net/XCCCCZ/article/details/91171470
今日推荐