Chinaunix首页 | 论坛 | 博客
  • 博客访问: 305464
  • 博文数量: 54
  • 博客积分: 3050
  • 博客等级: 中校
  • 技术积分: 601
  • 用 户 组: 普通用户
  • 注册时间: 2010-01-25 16:53
文章分类
文章存档

2012年(1)

2011年(7)

2010年(46)

我的朋友

分类: LINUX

2010-05-27 22:42:36


在Ubuntu-9.10 的 2.6.31-21 内核下运行 《Unix网络编程-第一卷第三版》(Unix Network Programming Volume 1, Third Edition, 简称 UNP 或 UNPv1e3), 有许多和以前系统或者其他系统以来的定义需要更改才能正常运行。以下是编译时遇到的错误和在当前系统下的可行改正方法。有共同需求的可以一起过来切磋切磋。

————————— Update 1, start ————————————

想运行 P107-Chap 5.4 的例子, 在 UNPv1e3/tcpcliserv 下面运行第一次运行 make 时出现

--> error 1: 
 tcpservpoll01.c:13: error: ‘OPEN_MAX’ undeclared (first use in this function)


解释:
运行 grep -r "OPEN_MAX" ./
可见  ./tcpcliserv/tcpservpoll01.c: include              /* for OPEN_MAX */ 输出.
但是,OPEN_MAX 在 /usr/inlude/limits.h 已经不存在了,根据参考帖里的信息,我也强烈的怀疑现在 /usr/include/linux/fd.h 和 /usr/include/linux/limit.h 里的 NR_OPEN 就是作者所说的 OPEN_MAX。

Solution 1: OPEN_MAX --> NR_OPEN, 连 #include 这句都不用更改
Solution 2: 在 unp.h 里面手动添加 #define OPEN_MAX 1024, 这个 1024 从上面所说 NR_OPEN 的值得到,
至于 #include 句嫌麻烦的可不删.

--> error 2:
tcpservpoll01.c:28: error: ‘POLLRDNORM’ undeclared

参考帖( E文):(Google 关键词:error: ‘POLLRDNORM’ undeclared)
 , 看了一些E 文帖,发现这些说的最丰富吧,加在一起也算完备了.

解释: 必须在任何使用 feature.h 的文件之前有 #define __XOPEN_SOURCE 的语句,而在 /usr/include/feature.h 干脆就有如下的注释语句:
#ifndef _FEATURES_H
#define _FEATURES_H 1

/* These are defined by the user (or the compiler)
to specify the desired environment:

__STRICT_ANSI__ ISO Standard C.
_ISOC99_SOURCE Extensions to ISO C89 from ISO C99.
_POSIX_SOURCE IEEE Std 1003.1.
_POSIX_C_SOURCE If ==1, like _POSIX_SOURCE; if >=2 add IEEE Std 1003.2;
if >=199309L, add IEEE Std 1003.1b-1993;
if >=199506L, add IEEE Std 1003.1c-1995;
if >=200112L, all of IEEE 1003.1-2004
_XOPEN_SOURCE Includes POSIX and XPG things. Set to 500 if
Single Unix conformance is wanted, to 600 for the
upcoming sixth revision.
_XOPEN_SOURCE_EXTENDED XPG things and X/Open Unix extensions.
_LARGEFILE_SOURCE Some more functions for correct standard I/O.
_LARGEFILE64_SOURCE Additional functionality from LFS for large fi
..... 

但是有点扯的太远了,下面才涉及到问题本身:

根本原因 usr/include/sys/poll.h 缺少如下语句:
#ifdef __USE_XOPEN
/* These values are defined in XPG4.2. */
# define POLLRDNORM 0x040 /* Normal data may be read. */
# define POLLRDBAND 0x080 /* Priority data may be read. */
# define POLLWRNORM 0x100 /* Writing now will not block. */
# define POLLWRBAND 0x200 /* Priority data may be written. */
#endif
但事实上,这些在别处倒是真有定义,第三个参考帖里强人给出:

[arnuld@sumit asm-x86_64]$ cat poll.h
#ifndef __x86_64_POLL_H
#define __x86_64_POLL_H

/* These are specified by iBCS2 */
#define POLLIN 0x0001
#define POLLPRI 0x0002
#define POLLOUT 0x0004
#define POLLERR 0x0008
#define POLLHUP 0x0010
#define POLLNVAL 0x0020

/* The rest seem to be more-or-less nonstandard. Check them! */
#define POLLRDNORM 0x0040
#define POLLRDBAND 0x0080
#define POLLWRNORM 0x0100
#define POLLWRBAND 0x0200
#define POLLMSG 0x0400
#define POLLREMOVE 0x1000
#define POLLRDHUP 0x2000

struct pollfd {
int fd;
short events;
short revents;
};

#endif

[arnuld@sumit asm-x86_64]$ pwd
/usr/include/asm-x86_64

可见在 64位的相关文件里面是有定义的,而 32位的Linux 里面没有此相关定义。

Solution :还是乖乖地在 unp.h 里面自己定义这些东西吧,否则,就是编译内核的时候添加 _XOPEN_SOURCE 选项了,至于其他的使用 poll.h 时也会因为 poll.h 里面没有此选项而出现的相同的错误,等到遇到时再说吧。现在就 lazy 一下,直接修改 unp.h

Thu,May/27/2010

———————— Update 1, end ——————————


———————— Update 2. start ——————————


———————— Update 2. end ———————————
阅读(3690) | 评论(3) | 转发(2) |
给主人留下些什么吧!~~

chaolumon2011-05-14 20:15:00

我这儿也编不过,只好在Makefile 里边 干掉 tcpservpoll01 了 ... 哎

chinaunix网友2011-02-21 21:43:26

第一个问题实际上是早期limits.h定义了的,后来取消 因为如果定义了会污染内核的OPEN_MAX。 换句话说系统不会因为定义了OPEN_ MAX而改变。 > I'm trying to use OPEN_MAX in a sample program. But it get not declared also > after including limits.h. This is deliberate and not a bug. For example, /usr/include/bits/local_lim.h says: /* The kernel header pollutes the namespace with the NR_OPEN symbol and defines LINK_MAX although filesystems have different maxima. A similar thing is true for OPEN_MAX: the limit can be changed at runtim

chinaunix网友2010-09-30 11:36:43

呵呵 谢谢~~~~~~~