【问题标题】:Why does my Thrift (c_glib) example fail to compile with "error: invalid conversion"?为什么我的 Thrift (c_glib) 示例无法编译并出现“错误:无效转换”?
【发布时间】:2015-09-18 11:22:25
【问题描述】:

我正在试用官方 thrift 网站上提供的 thrift c_glib example(仅带 ping 的最小版本),但每当我编译代码(gcc 或 make)时,它都会失败,原因如下:

      thrift-server.c: In function ‘int main()’:
thrift-server.c:186:23: error: invalid conversion from ‘gpointer {aka void*}’ to ‘TutorialCalculatorHandler* {aka _TutorialCalculatorHandler*}’ [-fpermissive]
                   NULL);
                       ^
thrift-server.c:194:23: error: invalid conversion from ‘gpointer {aka void*}’ to ‘CalculatorProcessor* {aka _CalculatorProcessor*}’ [-fpermissive]
                   NULL);
                       ^
thrift-server.c:201:23: error: invalid conversion from ‘gpointer {aka void*}’ to ‘ThriftServerTransport* {aka _ThriftServerTransport*}’ [-fpermissive]
                   NULL);
                       ^
thrift-server.c:208:23: error: invalid conversion from ‘gpointer {aka void*}’ to ‘ThriftTransportFactory* {aka _ThriftTransportFactory*}’ [-fpermissive]
                   NULL);
                       ^
thrift-server.c:214:23: error: invalid conversion from ‘gpointer {aka void*}’ to ‘ThriftProtocolFactory* {aka _ThriftProtocolFactory*}’ [-fpermissive]
                   NULL);
                       ^
thrift-server.c:225:23: error: invalid conversion from ‘gpointer {aka void*}’ to ‘ThriftServer* {aka _ThriftServer*}’ [-fpermissive]
                   NULL);
                       ^
thrift-server.c:176:7: warning: unused variable ‘exit_status’ [-Wunused-variable]
   int exit_status = 0;
       ^
thrift-server.c: At global scope:
thrift-server.c:154:1: warning: ‘void sigint_handler(int)’ defined but not used [-Wunused-function]
 sigint_handler (int signal_number)
 ^
make: *** [server] Error 1

任何指针都会很有帮助。我的 GCC 版本是 4.8.2,我在 ubuntu 14.10 机器上

我的 makefile 看起来像这样:

default: server

server: thrift-server.c
    gcc -g -Wall -Wextra -pedantic -I/usr/include/glib-2.0 -I/usr/lib/i386-linux-gnu/glib-2.0/include -I/usr/include/glib-2.0 -I/usr/lib/i386-linux-gnu/glib-2.0/include -g -O2 -o thrift-server.o -c thrift-server.c  /home/akhld/mobi/localcluster/codes/private/simdprototype/thrift-0.9.2/tutorial/c_glib/.libs/libtutorialgencglib.a -lgobject-2.0 -lglib-2.0 /home/akhld/mobi/localcluster/codes/private/simdprototype/thrift-0.9.2/lib/c_glib/.libs/libthrift_c_glib.so /home/akhld/mobi/localcluster/codes/private/simdprototype/thrift-0.9.2/lib/c_glib/.libs/libthrift_c_glib.so -lssl -lcrypto -lrt -lpthread -Wl,-rpath -Wl,/home/akhld/mobi/localcluster/codes/private/simdprototype/thrift-0.9.2/lib/c_glib/.libs

这就是我的 .thrift 文件的样子:

namespace cpp tutorial
service Calculator {
  void ping()
}

这是 thrift-server.c:

http://pastebin.com/Q4SGAdMu

【问题讨论】:

标签: c gcc makefile thrift glib


【解决方案1】:

尝试使用gcc 而不是g++ 编译您的代码(更改您的makefile 的最后一行)。 C++,但不是 C,需要从 void * 进行显式转换。

如果您出于某种原因必须使用 g++,请尝试在每次调用 g_object_new 时添加显式转换,例如:

handler = (TutorialCalculatorHandler *)
            g_object_new (TYPE_TUTORIAL_CALCULATOR_HANDLER,
                          NULL);

【讨论】:

  • 这些“未定义引用”错误是由于您没有告诉编译器链接到 GObject 库。请参阅 GLib 参考手册的 instructions on compiling GLib applications 了解正确的方法。
  • 是的,解决了。在问题中添加了正确的生成文件。感谢您的宝贵时间。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-10-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-14
  • 1970-01-01
相关资源
最近更新 更多