在 Windows 中编译 Apache Thrift 服务

Posted

技术标签:

【中文标题】在 Windows 中编译 Apache Thrift 服务【英文标题】:Compiling Apache Thrift Service in Windows 【发布时间】:2011-11-15 23:38:55 【问题描述】:

我正在尝试在我的 Windows 机器上构建一个节俭服务。我正在使用 cygwin 和 Netbeans IDE。我已经下载了 Thrift 并通过 cygwin 构建它,并且能够成功让 Thrift 为我生成服务器代码,如下所示。

#include "Feed.h"
#include <protocol/TBinaryProtocol.h>
#include <server/TSimpleServer.h>
#include <transport/TServerSocket.h>
#include <transport/TBufferTransports.h>

using namespace ::apache::thrift;
using namespace ::apache::thrift::protocol;
using namespace ::apache::thrift::transport;
using namespace ::apache::thrift::server;

using boost::shared_ptr;

using namespace feed;

class FeedHandler : virtual public FeedIf 
 public:
  FeedHandler() 
    // Your initialization goes here
  

  void ping() 
    // Your implementation goes here
    printf("ping\n");
  

  void search_text(search_resultset& _return, const std::string& query, const int32_t offset, const int32_t per_page) 
    // Your implementation goes here
    printf("search_text\n");
  

  void search_prox_web(search_resultset& _return, const double lat, const double lon, const int32_t offset, const int32_t distance) 
    // Your implementation goes here
    printf("search_prox_web\n");
  

  void search_prox_mob(search_resultset& _return, const double lat, const double lon, const int32_t offset, const int32_t distance) 
    // Your implementation goes here
    printf("search_prox_mob\n");
  

  int32_t add_event(const std::string& name) 
    // Your implementation goes here
    printf("add_event\n");
  

  int32_t associate_venue_with_event(const int32_t event_id, const int32_t venue_id, const int32_t usr_loc_id) 
    // Your implementation goes here
    printf("associate_venue_with_event\n");
  

  int32_t save_usr_loc(const std::string& address) 
    // Your implementation goes here
    printf("save_usr_loc\n");
  

;

int main(int argc, char **argv) 
  int port = 9090;
  shared_ptr<FeedHandler> handler(new FeedHandler());
  shared_ptr<TProcessor> processor(new FeedProcessor(handler));
  shared_ptr<TServerTransport> serverTransport(new TServerSocket(port));
  shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory());
  shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());

  TSimpleServer server(processor, serverTransport, transportFactory, protocolFactory);
  server.serve();
  return 0;

然后我构建了库,以便编译和链接我的实现。库构建成功,但是当我尝试编译时出现以下错误:

build/Debug/Cygwin_4.x-Windows/main.o: In function `FeedProcessor': 
/cygdrive/c/Feed Service/Feed.h:930: undefined reference to `vtable for feed::FeedProcessor'
/cygdrive/c/Feed Service/Feed.h:930: undefined reference to `vtable for feed::FeedProcessor'
/cygdrive/c/Feed Service/Feed.h:931: undefined reference to `feed::FeedProcessor::process_ping(int, apache::thrift::protocol::TProtocol*, apache::thrift::protocol::TProtocol*, void*)'
/cygdrive/c/Feed Service/Feed.h:932: undefined reference to `feed::FeedProcessor::process_search_text(int, apache::thrift::protocol::TProtocol*, apache::thrift::protocol::TProtocol*, void*)'
/cygdrive/c/Feed Service/Feed.h:933: undefined reference to `feed::FeedProcessor::process_search_prox_web(int, apache::thrift::protocol::TProtocol*, apache::thrift::protocol::TProtocol*, void*)'
/cygdrive/c/Feed Service/Feed.h:934: undefined reference to `feed::FeedProcessor::process_search_prox_mob(int, apache::thrift::protocol::TProtocol*, apache::thrift::protocol::TProtocol*, void*)'
/cygdrive/c/Feed Service/Feed.h:935: undefined reference to `feed::FeedProcessor::process_add_event(int, apache::thrift::protocol::TProtocol*, apache::thrift::protocol::TProtocol*, void*)'
/cygdrive/c/Feed Service/Feed.h:936: undefined reference to `feed::FeedProcessor::process_associate_venue_with_event(int, apache::thrift::protocol::TProtocol*, apache::thrift::protocol::TProtocol*, void*)'
/cygdrive/c/Feed Service/Feed.h:937: undefined reference to `feed::FeedProcessor::process_save_usr_loc(int, apache::thrift::protocol::TProtocol*, apache::thrift::protocol::TProtocol*, void*)'
collect2: ld returned 1 exit status
make[2]: *** [dist/Debug/Cygwin_4.x-Windows/feed_service.exe] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2

BUILD FAILED (exit value 2, total time: 3s)

我认为这些都是链接器错误,所以我进行了一些谷歌搜索,并找到了一个潜在的解决方案,可以在 makefile 中添加以下行:

THRIFT_O=C:/cygwin/home/thrift/lib/cpp
    LTHRIFT=$(THRIFT_O)/Thrift.o $(THRIFT_O)/TSocket.o $(THRIFT_O)/TSimpleServer.o $(THRIFT_O)/TBufferTransports.o $(THRIFT_O)/TSimpleServer.o $(THRIFT_O)/TBinaryProtocol.o

然后用 $(LTHRIFT) 而不是 -lthrift 链接。但是,我的系统上似乎不存在 TBinaryProtocol.o 文件。我尝试从源代码重新构建库两次,但仍然没有生成文件。

首先,将这些行添加到我的 makefile 中是否是我最初问题的正确解决方案?其次,TBinaryProtocol.o 的缺失是一个大问题,还是它的创建不是有原因的?如果我确实需要它,有没有办法可以单独制作或从某个地方下载?

编辑:所有包含文件都是由 Thrift 编译器自动生成的。我尝试在此处包含 Feed.h,但它太大并且超出了字符数限制,因此我将错误中引用的部分包含在内。

class FeedProcessor : virtual public ::apache::thrift::TProcessor 
 protected:
  boost::shared_ptr<FeedIf> iface_;
  virtual bool process_fn(::apache::thrift::protocol::TProtocol* iprot, ::apache::thrift::protocol::TProtocol* oprot, std::string& fname, int32_t seqid, void* callContext);
 private:
  std::map<std::string, void (FeedProcessor::*)(int32_t, ::apache::thrift::protocol::TProtocol*, ::apache::thrift::protocol::TProtocol*, void*)> processMap_;
  void process_ping(int32_t seqid, ::apache::thrift::protocol::TProtocol* iprot, ::apache::thrift::protocol::TProtocol* oprot, void* callContext);
  void process_search_text(int32_t seqid, ::apache::thrift::protocol::TProtocol* iprot, ::apache::thrift::protocol::TProtocol* oprot, void* callContext);
  void process_search_prox_web(int32_t seqid, ::apache::thrift::protocol::TProtocol* iprot, ::apache::thrift::protocol::TProtocol* oprot, void* callContext);
  void process_search_prox_mob(int32_t seqid, ::apache::thrift::protocol::TProtocol* iprot, ::apache::thrift::protocol::TProtocol* oprot, void* callContext);
  void process_add_event(int32_t seqid, ::apache::thrift::protocol::TProtocol* iprot, ::apache::thrift::protocol::TProtocol* oprot, void* callContext);
  void process_associate_venue_with_event(int32_t seqid, ::apache::thrift::protocol::TProtocol* iprot, ::apache::thrift::protocol::TProtocol* oprot, void* callContext);
  void process_save_usr_loc(int32_t seqid, ::apache::thrift::protocol::TProtocol* iprot, ::apache::thrift::protocol::TProtocol* oprot, void* callContext);
 public:
  FeedProcessor(boost::shared_ptr<FeedIf> iface) :
    iface_(iface) 
    processMap_["ping"] = &FeedProcessor::process_ping;
    processMap_["search_text"] = &FeedProcessor::process_search_text;
    processMap_["search_prox_web"] = &FeedProcessor::process_search_prox_web;
    processMap_["search_prox_mob"] = &FeedProcessor::process_search_prox_mob;
    processMap_["add_event"] = &FeedProcessor::process_add_event;
    processMap_["associate_venue_with_event"] = &FeedProcessor::process_associate_venue_with_event;
    processMap_["save_usr_loc"] = &FeedProcessor::process_save_usr_loc;
  

  virtual bool process(boost::shared_ptr< ::apache::thrift::protocol::TProtocol> piprot, boost::shared_ptr< ::apache::thrift::protocol::TProtocol> poprot, void* callContext);
  virtual ~FeedProcessor() 
;

任何帮助将不胜感激,我非常坚持这一点。

【问题讨论】:

Feed Service/Feed.h 怎么样?链接器错误抱怨它。你可以在这里发布它(Feed.h)吗? Feed.h 是 Thrift 编译器自己自动生成的,但我会在上面贴出来。 我没有使用 thrift,但我使用的是生成 C++ 代码的其他系统。并且显示链接器的消息对于第一次尝试使用它们是相当常见的。一般来说,人们忘记将“生成”文件之一包含到 Makefile 中。但是如果没有 Fred.h 和 Makefile 就不清楚到底发生了什么。 好吧,恭喜你走到这一步……上次我检查时,Windows 根本不支持 Thrift 库。 @user1034749:Feed.h 粘贴在这里:pastebin.com/bUii99FW 当我打开计算机时,我会发布生成文件。克雷克 SB: 谢谢。走到这一步真是让人头疼。 【参考方案1】:

为了完整起见:

(1) 在 Windows 上,无需自己构建 Thrift Compiler EXE,有一个(小)安装程序to be downloaded here。

(2) 如果您仍然想要或需要构建 Thrift 编译器二进制文件,您仍然不需要启动 Cygwin:Apache Thrift 的最新版本提供了一个不错的 Visual Studio solution,它只需要 Win flex-bison 作为依赖项.像魅力一样工作。

【讨论】:

thrift.apache.org/docs/install/windows 根据该说法,这不是真的,请注意以下内容:“大多数语言包必须使用更适合这些语言的构建工具手动构建和安装。” 并且同一个页面也有一个板块Prebuilt Thrift Compiler。那个部分说什么?请注意,Thrift 编译器和语言包是两个完全不同的东西。 问题是关于编译需要编译器和库的服务。这就是我要说的。 这就是为什么我在第一句话中写了“只是为了完整性”。

以上是关于在 Windows 中编译 Apache Thrift 服务的主要内容,如果未能解决你的问题,请参考以下文章

Apache静态编译与动态编译的区别

在 Windows 上编译时出现 Apache thrift 错误

下载windows版本apache网页服务器

使用 Ruby 连接到 Hbase

flask+mod_wsgi+apache在windows上的布署

如何在 apache cgi 应用程序中调用 windows api (FindWindow)?