C中的ZeroMQ示例将无法编译;官方文件可能是错的吗?

时间:2013-08-03 20:26:38

标签: c zeromq

ZeroMQ官方网站提供了一个基本示例 here (向下滚动到“Ask and Ye Shall Receive”部分):

// 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

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

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);

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

1 个答案:

答案 0 :(得分:3)

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

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