ZeroMQ在Cygwin下的编译与使用

ZeroMQ 是一个实现消息通信的项目。从网络通信的角度看,它处于会话层之上,应用层之下,有了它,你甚至不需要自己写一行的socket函数调用就能完成复杂的网络通信工作。

ZeroMQ 可以在Window和Unix—like上运行,现在将其在Cygwin下运行。

首先按照官方给出的安装指导

Make sure that libtool, autoconf, automake are installed.
Check whether uuid-dev package, uuid/e2fsprogs RPM or equivalent on your system is installed.
Unpack the .tar.gz source archive.
Run ./configure, followed by make.
To install ZeroMQ system-wide run sudo make install.
On Linux, run sudo ldconfig after installing ZeroMQ.

1.安装 libtool, autoconf, automake等工具(最后发现好像不用也行)

2.安装uuid-dev 和e2fsprogs(bin文件即可)

3.解压下载,进入目录

4.运行 ./configure && make 

出现错误

 

In file included from socket_base.hpp:33:0,
                 from ctx.cpp:30:
poller.hpp:36:13: error: 'poll_t' does not name a type
In file included from ctx.cpp:30:0:
socket_base.hpp:96:29: error: 'poller_t' has not been declared

 

这是通过分析发现这是官方给出的移植并不完善。导致poll_t 未定义。

在文件 "poll.hpp"中的

#if defined ZMQ_HAVE_LINUX || defined ZMQ_HAVE_FREEBSD ||\
    defined ZMQ_HAVE_OPENBSD || defined ZMQ_HAVE_SOLARIS ||\
    defined ZMQ_HAVE_OSX || defined ZMQ_HAVE_QNXNTO ||\
    defined ZMQ_HAVE_HPUX || defined ZMQ_HAVE_AIX ||\
    defined ZMQ_HAVE_NETBSD 

中添加

ZMQ_HAVE_CYGWIN

宏ZMQ_HAVE_CYGWIN在src/platform.hpp中有定义。

/* Have Cygwin */
#define ZMQ_HAVE_CYGWIN 1

再次编译出现

/usr/bin/grep: /usr/lib/libiconv.la: No such file or directory
Makefile:529: recipe for target `libzmq.la' failed

这是由于缺少libiconv库,安装即可。

最好还有一个警告,但是不影响使用。

 CXXLD  libzmq.la
libtool: link: warning: undefined symbols not allowed in i686-pc-cygwin shared libraries

5. make install 安装 

6. 测试

参考一个《Hello 0MQ, Echo Server》
方便起见,将client和server摘录如下

server.c
 1 //-------------------- server.c ---------------------
 2 #include <assert.h>
 3 #include <stdio.h>
 4 #include <string.h>
 5 #include <zmq.h>
 6 
 7 int main()
 8 {
 9     int rc;
10     void *ctx, *s;
11     zmq_msg_t query, resultset;
12     const char *query_string, *resultset_string = "ok";
13 
14     ctx = zmq_init(1);
15     assert(ctx);
16 
17     s = zmq_socket(ctx, ZMQ_REP);
18     assert(s);
19 
20     rc = zmq_bind(s, "tcp://127.0.0.1:5555");
21     assert(rc == 0);
22 
23     while (1)
24     {
25         rc = zmq_msg_init(&query);
26         assert(rc == 0);
27 
28         rc = zmq_recv(s, &query, 0);
29         assert(rc == 0);
30 
31         query_string = (const char *)zmq_msg_data(&query);
32         printf("recv: %s\n", query_string);
33         zmq_msg_close(&query);
34 
35         rc = zmq_msg_init_size(&resultset, strlen(resultset_string)+1);
36         assert(rc == 0);
37         
38         memcpy(zmq_msg_data(&resultset), resultset_string, strlen(resultset_string)+1);
39 
40         rc = zmq_send(s, &resultset, 0);
41         assert(rc == 0);
42         zmq_msg_close(&resultset);
43     }
44 
45     return 0;
46 }
client.c
 1 -------------------- client.c ---------------------
 2 #include <assert.h>
 3 #include <stdio.h>
 4 #include <string.h>
 5 #include <zmq.h>
 6 
 7 int main()
 8 {
 9     int rc;
10     void *ctx, *s;
11     const char *query_string = "hello ZeroMQ.";
12     zmq_msg_t query, resultset;
13 
14     ctx = zmq_init(1);
15     assert(ctx);
16 
17     s = zmq_socket(ctx, ZMQ_REQ);
18     assert(s);
19 
20     rc = zmq_connect(s, "tcp://127.0.0.1:5555");
21     assert(rc == 0);
22 
23     // send
24     rc = zmq_msg_init_size(&query, strlen(query_string)+1);
25     assert(rc == 0);
26     memcpy(zmq_msg_data(&query), query_string, strlen(query_string)+1);
27 
28     rc = zmq_send(s, &query, 0);
29     assert(rc == 0);
30     zmq_msg_close(&query);
31 
32     // recv
33     rc = zmq_msg_init(&resultset);
34     assert(rc == 0);
35 
36     rc = zmq_recv(s, &resultset, 0);
37     assert(rc == 0);
38 
39     printf("ack: %s\n", (const char *)zmq_msg_data(&resultset));
40     zmq_msg_close(&resultset);
41 
42     zmq_close(s);
43     zmq_term(ctx);
44     return 0;
45 }

写一个Makefile

all:
    g++ -o service service.c -L /usr/local/lib -lzmq -luuid
    g++ -o client client.c -L /usr/local/lib -lzmq -luuid

不知道为什么必须显示的给出链接库的路径,还要加上链接库uuid,否则出错。

分别运行服务器端和客户端的程序

$ ./service.exe
recv: hello ZeroMQ.

$ ./client.exe
ack: ok

——————

正常编译和安装。

 

posted @ 2012-05-15 21:26  westfly  阅读(1851)  评论(1编辑  收藏  举报