【问题标题】:ZeroMQ example in C won't compile; could the official documentation be wrong?C 中的 ZeroMQ 示例无法编译;官方文档可能有误吗?
【发布时间】:2015-08-27 12:11:57
【问题描述】:

ZeroMQ 官方网站 here 提供了一个基本示例(向下滚动到“询问,您将收到”部分):

// Hello World server

#include <zmq.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <assert.h>

int main (void)
{
    // Socket to talk to clients
    void *context = zmq_ctx_new (); // LINE NUMBER 9
    void *responder = zmq_socket (context, ZMQ_REP);
    int rc = zmq_bind (responder, "tcp://*:5555");
    assert (rc == 0);

    while (1) {
        char buffer [10];
        zmq_recv (responder, buffer, 10, 0); // LINE NUMBER 15
        printf ("Received Hello\n");
        zmq_send (responder, "World", 5, 0); // LINE NUMBER 17
        sleep (1); // Do some 'work'
    }
    return 0;
}

我使用的是 Ubuntu 12.04 LTS,并从标准存储库安装了 ZeroMQ。但是当我尝试编译上面的例子时,我得到了以下错误:

./test.c: In function ‘main’:
./test.c:9:21: warning: initialization makes pointer from integer without a cast [enabled by default]
./test.c:15:9: warning: passing argument 2 of ‘zmq_recv’ from incompatible pointer type [enabled by default]
/usr/include/zmq.h:228:16: note: expected ‘struct zmq_msg_t *’ but argument is of type ‘char *’
./test.c:15:9: error: too many arguments to function ‘zmq_recv’
/usr/include/zmq.h:228:16: note: declared here
./test.c:17:9: warning: passing argument 2 of ‘zmq_send’ from incompatible pointer type [enabled by default]
/usr/include/zmq.h:227:16: note: expected ‘struct zmq_msg_t *’ but argument is of type ‘char *’
./test.c:17:9: error: too many arguments to function ‘zmq_send’
/usr/include/zmq.h:227:16: note: declared here

于是我在/usr/include/zmq.h中查找了zmq_recvzmq_send的定义,发现编译器确实说的是实话。以下是签名:

int zmq_bind (void *s, const char *addr);
int zmq_send (void *s, zmq_msg_t *msg, int flags);
int zmq_recv (void *s, zmq_msg_t *msg, int flags);

那么,我认为该站点上的文档不正确(或者可能已过时)是否正确?有没有其他人遇到过这个问题?

【问题讨论】:

  • 所以首先确保您拥有正确的版本 - 默认存储库中的版本可能不适合您正在查看的文档。其次,您是否使用了正确的标志?
  • @EiyrioüvonKauyf,确实如此。我查看了源文件,似乎Ubuntu 12.04 的存储库只有version 2.1.11。本教程假定版本号至少为3.2。我真傻;我只是假设像 Ubuntu 这样的主要发行版会有一个相当最新的版本。

标签: c zeromq


【解决方案1】:

这会奏效。您遇到此问题是因为您没有开发包。我已经在 ubuntu 上验证了这一点。

#sudo apt-get install libzmq3-dev
#gcc  test.c -o Zmserver.out -lzmq

【讨论】:

    猜你喜欢
    • 2017-03-18
    • 2018-03-12
    • 1970-01-01
    • 1970-01-01
    • 2020-03-12
    • 2012-09-10
    • 2012-03-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多