MacOS X 10.6 下 编译APUE代码清单(apue.2e 第二版源码)

来源:互联网 发布:链接数据库接口 编辑:程序博客网 时间:2024/06/06 20:20

1、修改文件Make.defines.macos设置代码目录变量,将WKDIR值改为实际代码目录

$ vim Make.defines.macos


2、修改WKDIR/include/apue.h文件的6-11行如下所示,增加MACOS判断,如果不修改文件,

则定义_XOPEN_SOURCE为600,在macOS中头文件中,部分文件依据_XOPEN_SOURCE是否定义,来定义宏。

#if defined(SOLARIS)

#define _XOPEN_SOURCE   500     /* Single UNIX Specification, Version 2  for Solaris 9 */
#define CMSG_LEN(x)     _CMSG_DATA_ALIGN(sizeof(struct cmsghdr)+(x))
#elif defined(MACOS) /*增加的*/
#elif !defined(BSD)
#define _XOPEN_SOURCE   600     /* Single UNIX Specification, Version 3 */
#endif

3、在WKDIR下,编译即可。

$ make


(注:Mac OS X 10.6.8系统,APUE代码是《UNIX环境高级编程(第二版)》,解压后为apue.2e文件夹)


 

在之前编译时,遇到过CMSG_LEN未定义的问题,根据http://www.apuebook.com/上的FAQs第10条,将CMSG_LEN的定义从系统include文件中找到了CMSG_LEN,在apue.h加了CMSG_LEN的定义,可以编译通过,但是接下来又遇到问题:

loop.c里的error: ‘fd_set’ undeclared。

fd_set是一个struct,将#include <sys/select.h>加进来可以解决问题。

但是紧接着又有major与minor宏未定义的问题,也可以通过查找macOS的/usr/include下grep查找找到头文件解决问题。

 

根据文章上面写的方法,就没有定义_XOPEN_SOURCE了。也就不遵循标准了?

查了一下关于_XOPEN_SOURCE

it tells your compiler to include definitions for some extra functions that are defined in the X/Open and POSIX standards.

This will give you some extra functionality that exists on most recent UNIX/BSD/Linux systems, but probably doesn't exist on other systems such as Windows.

The numbers refer to different versions of the standard.

  • 500 - X/Open 5, incorporating POSIX 1995
  • 600 - X/Open 6, incorporating POSIX 2004
  • 700 - X/Open 7, incorporating POSIX 2008

具体的作用还得有时间再摸索摸索。