thrift安装记录

thrift homepage : http://incubator.apache.org/thrift/

 

http://hi.baidu.com/hy0kl/blog/item/950fcd248ece383e8644f94d.html


要安装thrift,得先安装boost库

 

一、安装boost库

1.sudo apt-get install libboost-dev libboost-dbg libboost-doc bcp libboost-* 
若此命令执行失败,可以在System->Administration->Synaptic Package Manager中安装,最好把以libboost-开头的都安装了,免得以后出错。

 

安装好后

boost库在ubuntu的
/usr/lib/libboost_下面

 

2.安装好后就可以写一个小程序测试了

vim test.cpp

 

#include <boost/lexical_cast.hpp>
#include <iostream>
 
int main()
{
   using boost::lexical_cast;
   int a = lexical_cast<int>("123456");
   double b = lexical_cast<double>("123.456");
   std::cout << a << std::endl;
   std::cout << b << std::endl;
   return 0;
}

 

编译:
g++ -o test test.cpp
运行结果:
./test
123456
123.456

 

二、安装配置thrift
1.cd /home/panbook/workspace/boost_project

(ps:this directory is a little deep,you can use a shallow directory like cd /home/panbook/workspace)

wget http://apache.etoak.com/incubator/thrift/0.2.0-incubating/thrift-0.2.0-incubating.tar.gz

2.panbook@panbook-desktop:~/workspace/boost_project$ tar -zvxf thrift-0.2.0-incubating.tar.gz 

3. sudo apt-get install autoconf
sudo apt-get install autotools-dev(usually this program has installed)
sudo apt-get install flex
sudo apt-get install libtool
sudo apt-get install byacc

 

4.

cd thrift-0.2.0/
./bootstrap.sh
./configure
make

sudo make install (must use root )

 

5 测试

 

panbook@panbook-desktop:~/workspace/boost_project/thrift$ thrift
Usage: thrift [options] file
Options:
  -version    Print the compiler version
  -o dir      Set the output directory for gen-* packages
               (default: current directory)
  -I dir      Add a directory to the list of directories
                searched for include directives
  -nowarn     Suppress all compiler warnings (BAD!)
  -strict     Strict compiler warnings on
  -v[erbose]  Verbose mode
  -r[ecurse]  Also generate included files
  -debug      Parse debug trace to stdout
  --gen STR   Generate code with a dynamically-registered generator.
                STR has the form language[:key1=val1[,key2,[key3=val3]]].
                Keys and values are options passed to the generator.
                Many options will not require values.

Available generators (and options):
  cocoa (Cocoa):
    log_unexpected:  Log every time an unexpected field ID or type is encountered.
  cpp (C++):
    dense:           Generate type specifications for the dense protocol.
    include_prefix:  Use full include paths in generated files.
  csharp (C#):
  erl (Erlang):
  hs (Haskell):
  html (HTML):
  java (Java):
    beans:           Generate bean-style output files.
    nocamel:         Do not use CamelCase field accessors with beans.
    hashcode:        Generate quality hashCode methods.
  ocaml (OCaml):
  perl (Perl):
  php (PHP):
    inlined:         Generate PHP inlined files
    server:          Generate PHP server stubs
    autoload:        Generate PHP with autoload
    oop:             Generate PHP with object oriented subclasses
    rest:            Generate PHP REST processors
  py (Python):
    new_style:       Generate new-style classes.
    twisted:         Generate Twisted-friendly RPC services.
  rb (Ruby):
  st (Smalltalk):
  xsd (XSD):

恭喜你,你的thrift已经安装成功咯~

 

三、thrift使用入门

使用入门

使用源代码里的Tutorial做例子。它包含了两个thrift文件。

shared.thrift

namespace cpp shared

namespace java shared

namespace perl shared struct

SharedStruct {

1: i32 key

2: string value

}

service SharedService {

SharedStruct getStruct(1: i32 key)

}


tutorial.thrift

include "shared.thrift"

namespace cpp tutorial

namespace java tutorial

namespace php tutorial

namespace perl tutorial

namespace smalltalk.category Thrift.Tutorial

typedef i32 MyInteger

const i32 INT32CONSTANT = 9853

const map<string,string> MAPCONSTANT = {'hello':'world','goodnight':'moon'}

enum Operation { ADD = 1,SUBTRACT = 2,MULTIPLY = 3,DIVIDE = 4 }

struct Work {

1: i32 num1 = 0,

2: i32 num2,

3: Operation op,

4: optional string comment,

}

service Calculator extends shared.SharedService {

void ping(),

i32 add(1:i32 num1,2:i32 num2),

i32 calculate(1:i32 logid,2:Work w)

throws (1:InvalidOperation ouch),

oneway void zip()

}


声称c++代码:
thrift thrift -r --gen cpp tutorial.thrift
声称的代码会在gen-cpp目录中,进入gen-cpp发现有下面几个文件:
Calculator.cpp                  SharedService_server.skeleton.cpp
Calculator.h                    shared_types.cpp
Calculator_server.skeleton.cpp  shared_types.h
shared_constants.cpp            tutorial_constants.cpp
shared_constants.h              tutorial_constants.h
SharedService.cpp               tutorial_types.cpp
SharedService.h                 tutorial_types.h
shared.thrift定义的SharedStruct生成在shared_types.h和shared_types.cpp中,service SharedService生成在SharedService.h和SharedService.cpp中,shared.thrift相关的还有shared_constants.h和shared_constants.cpp。同样,tutorial.thrift定义的对象和服务生成的源文件也类似。这里我们可以看出thrift生成代码的规则。

下面我们看看如何利用生成的代码。

Server端:

实现接口

class CalculatorHandler : public CalculatorIf {
 public :
  CalculatorHandler( ) { }

  void ping( ) {
    printf ( "ping()/n" ) ;
  }

  int32_t add( const int32_t n1, const int32_t n2) {
    printf ( "add(%d,%d)/n" , n1, n2) ;
    return n1 + n2;
  }

  int32_t calculate( const int32_t logid, const Work & work) {
    printf ( "calculate(%d,{%d,%d,%d})/n" , logid, work. op, work. num1, work. num2) ;
    int32_t val;

    switch ( work. op) {
    case ADD:
      val = work. num1 + work. num2;
      break ;
    case SUBTRACT:
      val = work. num1 - work. num2;
      break ;
    case MULTIPLY:
      val = work. num1 * work. num2;
      break ;
    case DIVIDE:
      if ( work. num2 = = 0) {
        InvalidOperation io;
        io. what = work. op;
        io. why = "Cannot divide by 0" ;
        throw io;
      }
      val = work. num1 / work. num2;
      break ;
    default :
      InvalidOperation io;
      io. what = work. op;
      io. why = "Invalid Operation" ;
      throw io;
    }

    SharedStruct ss;
    ss. key = logid;
    char buffer[ 12] ;
    snprintf( buffer, sizeof ( buffer) , "%d" , val) ;
    ss. value = buffer;

    log [ logid] = ss;

    return val;
  }

  void getStruct( SharedStruct & ret, const int32_t logid) {
    printf ( "getStruct(%d)/n" , logid) ;
    ret = log [ logid] ;
  }

  void zip( ) {
    printf ( "zip()/n" ) ;
  }

protected :
  map < int32_t , SharedStruct> log ;

} ;


main里面创建服务

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

  shared_ptr< TProtocolFactory> protocolFactory( new TBinaryProtocolFactory( ) ) ;
  shared_ptr< CalculatorHandler> handler( new CalculatorHandler( ) ) ;
  shared_ptr< TProcessor> processor( new CalculatorProcessor( handler) ) ;
  shared_ptr< TServerTransport> serverTransport( new TServerSocket( 9090) ) ;
  shared_ptr< TTransportFactory> transportFactory( new TBufferedTransportFactory( ) ) ;

  TSimpleServer server( processor,
                       serverTransport,
                       transportFactory,
                       protocolFactory) ;
  printf ( "Starting the server.../n" ) ;
  server. serve( ) ;
  printf ( "done./n" ) ;
  return 0;
}


client端:

int main( int argc, char * * argv) {
  shared_ptr< TTransport> socket ( new TSocket( "localhost" , 9090) ) ;
  shared_ptr< TTransport> transport( new TBufferedTransport( socket ) ) ;
  shared_ptr< TProtocol> protocol( new TBinaryProtocol( transport) ) ;
  CalculatorClient client( protocol) ;

  try {
    transport- > open ( ) ;

    client. ping( ) ;
    printf ( "ping()/n" ) ;

    int32_t sum = client. add( 1, 1) ;
    printf ( "1+1=%d/n" , sum) ;

    Work work;
    work. op = DIVIDE;
    work. num1 = 1;
    work. num2 = 0;

    try {
      int32_t quotient = client. calculate( 1, work) ;
      printf ( "Whoa? We can divide by zero!/n" ) ;
    } catch ( InvalidOperation & io) {
      printf ( "InvalidOperation: %s/n" , io. why. c_str( ) ) ;
    }

    work. op = SUBTRACT;
    work. num1 = 15;
    work. num2 = 10;
    int32_t diff = client. calculate( 1, work) ;
    printf ( "15-10=%d/n" , diff) ;

    
// Note that C++ uses return by reference for complex types to avoid

    
// costly copy construction

    SharedStruct ss;
    client. getStruct( ss, 1) ;
    printf ( "Check log: %s/n" , ss. value. c_str( ) ) ;

    transport- > close ( ) ;
  } catch ( TException & tx) {
    printf ( "ERROR: %s/n" , tx. what ( ) ) ;
  }

}

 

 

安装过程中出现的错误:
1.configure: creating ./config.status
config.status: error: cannot find input file: `Makefile.in'
解决办法:sudo apt-get install autotools-dev

2.../../ylwrap: line 109: yacc:找不到命令
解决办法:sudo apt-get install byacc

3.required file `./ltmain.sh' not found
解决办法: sudo apt-get install libtool

4.
warning: underquoted definition of AM_PATH_LIBMCRYPT
run info '(automake)Extending aclocal'
or see http://sources.redhat.com/automake/automake.html#Extending-aclocal
解决办法:sudo apt-get autoremove libmcrypt


5. /bin/sh ../../libtool --tag=CXX   --mode=compile g++ -DHAVE_CONFIG_H -I. -I../..  -I/usr/include -I./src  -Wall -g -O2 -MT Thrift.lo -MD -MP -MF .deps/Thrift.Tpo -c -o Thrift.lo `test -f 'src/Thrift.cpp' || echo './'`src/Thrift.cpp
../../libtool: line 841: X--tag=CXX: command not found
../../libtool: line 874: libtool: ignoring unknown tag : command not found
../../libtool: line 841: X--mode=compile: command not found
../../libtool: line 1008: *** Warning: inferring the mode of operation is deprecated.: command not found
../../libtool: line 1009: *** Future versions of Libtool will require --mode=MODE be specified.: command not found
../../libtool: line 1152: Xg++: command not found
../../libtool: line 1152: X-DHAVE_CONFIG_H: command not found
../../libtool: line 1152: X-I.: command not found
../../libtool: line 1152: X-I../..: No such file or directory
../../libtool: line 1152: X-I/usr/include: No such file or directory
../../libtool: line 1152: X-I./src: No such file or directory
../../libtool: line 1152: X-Wall: command not found
../../libtool: line 1152: X-g: command not found
../../libtool: line 1152: X-O2: command not found
../../libtool: line 1152: X-MT: command not found
../../libtool: line 1152: XThrift.lo: command not found
../../libtool: line 1152: X-MD: command not found
../../libtool: line 1152: X-MP: command not found
../../libtool: line 1152: X-MF: command not found
../../libtool: line 1152: X.deps/Thrift.Tpo: No such file or directory
../../libtool: line 1152: X-c: command not found
../../libtool: line 1205: XThrift.lo: command not found
../../libtool: line 1210: libtool: compile: cannot determine name of library object from `': command not found
make[3]: *** [Thrift.lo] Error 1
解决办法:libtool的问题,目前只在服务器上有这个问题
1).卸载原服务器的libtool
2).下载最新的libtool安装,wget
http://ftp.gnu.org/gnu/libtool/libtool-2.2.6b.tar.gz . 3). ./configure ;make;make install 4). 重新thrift安装步骤的第四步即可 6../CppServer: error while loading shared libraries: libthrift.so.0: cannot open shared object file: No such file or directory 解决办法: sudo ln -s /usr/local/lib/libthrift.so.0   /usr/lib/libthrift.so.0 7.PHP Notice: Use of undefined constant E_NONE - assumed 'E_NONE' in / home/wr_webroot/thrift/tutorial/php/PhpClient.php on line 38 解决办法: Just replace E_NONE with 0 (int),that will fix it

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


我正在用TitaniumDeveloper编写一个应用程序,它允许我使用Javascript,PHP,Ruby和Python.它为API提供了一些可能需要的标准功能,但缺少的是全局事件.现在我想将全局热键分配给我的应用程序并且几乎没有任何问题.现在我只针对MAC,但无法找到任何Python或Ruby的解决方案.我找到了Coc
我的问题是当我尝试从UIWebView中调用我的AngularJS应用程序中存在的javascript函数时,该函数无法识别.当我在典型的html结构中调用该函数时,该函数被识别为预期的.示例如下:Objective-C的:-(void)viewDidLoad{[superviewDidLoad];//CODEGOESHERE_webView.d
我想获取在我的Mac上运行的所有前台应用程序的应用程序图标.我已经使用ProcessManagerAPI迭代所有应用程序.我已经确定在processMode中设置了没有modeBackgroundOnly标志的任何进程(从GetProcessInformation()中检索)是一个“前台”应用程序,并显示在任务切换器窗口中.我只需要
我是一名PHP开发人员,我使用MVC模式和面向对象的代码.我真的想为iPhone编写应用程序,但要做到这一点我需要了解Cocoa,但要做到这一点我需要了解Objective-C2.0,但要做到这一点我需要知道C,为此我需要了解编译语言(与解释相关).我应该从哪里开始?我真的需要从简单的旧“C”开始,正
OSX中的SetTimer在Windows中是否有任何等效功能?我正在使用C.所以我正在为一些软件编写一个插件,我需要定期调用一个函数.在Windows上,我只是将函数的地址传递给SetTimer(),它将以给定的间隔调用.在OSX上有一个简单的方法吗?它应该尽可能简约.我并没有在网上找到任何不花哨的东西
我不确定引擎盖下到底发生了什么,但这是我的设置,示例代码和问题:建立:>雪豹(10.6.8)>Python2.7.2(由EPD7.1-2提供)>iPython0.11(由EPD7.1-2提供)>matplotlib(由EPD7.1-2提供)示例代码:importnumpyasnpimportpylabasplx=np.random.normal(size=(1000,))pl.plot
我正在使用FoundationFramework在Objective-C(在xCode中)编写命令行工具.我必须使用Objective-C,因为我需要取消归档以前由NSKeyedArchiver归档的对象.我的问题是,我想知道我现在是否可以在我的Linux网络服务器上使用这个编译过的应用程序.我不确定是否会出现运行时问题,或者可
使用cocoapods,我们首先了解一下rvm、gem、ruby。rvm和brew一样,但是rvm是专门管理ruby的版本控制的。rvmlistknown罗列出ruby版本rvminstall版本号   可以指定更新ruby版本而gem是包管理gemsource-l查看ruby源gemsource-rhttps://xxxxxxxx移除ruby源gemsou
我有一个包含WebView的Cocoa应用程序.由于应用程序已安装客户群,我的目标是10.4SDK.(即我不能要求Leopard.)我有两个文件:index.html和data.js.在运行时,为了响应用户输入,我通常会使用应用程序中的当前数据填充data.js文件.(data.js文件由body.html上的index.html文件用于填充
如何禁用NSMenuItem?我点击后尝试禁用NSMenuItem.操作(注销)正确处理单击.我尝试通过以下两种方式将Enabled属性更改为false:partialvoidLogout(AppKit.NSMenuItemsender){sender.Enabled=false;}和partialvoidLogout(AppKit.NSMenuItemsender){LogoutI
我在想,创建一个基本上只是一个带Web视图的界面的Cocoa应用程序是否可行?做这样的事情会有一些严重的限制吗?如果它“可行”,那是否也意味着你可以为Windows应用程序做同样的事情?解决方法:当然可以创建一个只是一个Cocoa窗口的应用程序,里面有一个Web视图.这是否可以被称为“可可应
原文链接:http://www.cnblogs.com/simonshi2012/archive/2012/10/08/2715464.htmlFrom:http://www.idev101.com/code/Cocoa/Notifications.htmlNotificationsareanincrediblyusefulwaytosendmessages(anddata)betweenobjectsthatotherwi
如果不手动编写GNUmake文件,是否存在可以理解Xcode项目的任何工具,并且可以直接针对GNUstep构建它们,从而生成Linux可执行文件,从而简化(略微)保持项目在Cocoa/Mac和GNUstep/Linux下运行所需的工作?基本上,是否有适用于Linux的xcodebuild样式应用程序?几个星期前我看了pbtomake
我正在将页面加载到WebView中.该页面有这个小测试Javascript:<scripttype="text/javascript">functiontest(parametr){$('#testspan').html(parametr);}varbdu=(function(){return{secondtest:function(parametr){$('#testspan&#039
我正在尝试使用NSAppleScript从Objective-C执行一些AppleScript…但是,我正在尝试的代码是Yosemite中用于自动化的新JavaScript.它在运行时似乎没有做任何事情,但是,正常的AppleScript工作正常.[NSAppactivateIgnoringOtherApps:YES];NSAppleScript*scriptObject=[[NSApple
链接:https://pan.baidu.com/s/14_im7AmZ2Kz3qzrqIjLlAg           vjut相关文章Python与Tkinter编程ProgrammingPython(python编程)python基础教程(第二版)深入浅出PythonPython源码剖析Python核心编程(第3版)图书信息作者:Kochan,StephenG.出
我正在实现SWTJava应用程序的OSX版本的视图,并希望在我的SWT树中使用NSOutlineView提供的“源列表”选项.我通过将此代码添加到#createHandle()方法来破解我自己的Tree.class版本来实现这一点:longNSTableViewSelectionHighlightStyleSourceList=1;longhi=OS.sel_regist
我的Cocoa应用程序需要使用easy_install在用户系统上安装Python命令行工具.理想情况下,我想将一个bash文件与我的应用程序捆绑在一起然后运行.但据我所知这是不可能的,因为软件包安装在Python的“site-packages”目录中.有没有办法创建这些文件的“包”?如果没有,我应该如何运行ea
摘要: 文章工具 收藏 投票评分 发表评论 复制链接 Swing 是设计桌面应用程序的一个功能非常强大工具包,但Swing因为曾经的不足常常遭到后人的诟病.常常听到旁人议论纷纷,”Swing 运行太慢了!”,”Swing 界面太丑嘞”,甚至就是说”Swing 简直食之无味”. 从Swing被提出到现在,已是十年光景,Swing早已不是昔日一无是处的Swing了. Chris Adamson 和我写
苹果的开发:   我对于Linux/Unix的开发也是一窍不通,只知道可以用Java.不过接触了苹果过后,确实发现,世界上确实还有那么一帮人,只用苹果,不用PC的.对于苹果的开发,我也一点都不清楚,以下是师兄们整理出来的网站. http://www.chezmark.com/osx/    共享软件精选 http://www.macosxapps.com/    分类明了,更新及时的一个重要Mac