2

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

// 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_recvzmq_sendin的定义,/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);

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

4

1 回答 1

3

这将起作用。您遇到此问题是因为您没有开发包。我已经在 ubuntu 上验证了这一点。

#sudo apt-get install libzmq3-dev
#gcc  test.c -o Zmserver.out -lzmq
于 2015-08-27T04:11:57.890 回答