apue2中源码的使用

下载源码后解压至某一目录,比如我的~/new/apue

进入该目录,修改Make.defines.linux,将其中的WKDIR赋为上面解压的目录

进入std子目录,修改linux.mk,把命令nawk改为awk

make

例子中包含的apue.h位于解压目录的include子文件夹,那么相应的原文件要做一些修改.比如fig1.3的程序my_ls.c,我放在~/new/apue/src中:

#include "../include/apue.h"
#include <dirent.h>

int main(int argc, char *argv[])
{
DIR *dp;
struct dirent *dirp;

if (argc != 2)
err_quit("usage: %s directory_name", argv[0]);

if ((dp = opendir(argv[1])) == NULL)
err_sys("can't open %s", argv[1]);
while ((dirp = readdir(dp)) != NULL)
printf("%s\n", dirp->d_name);

closedir(dp);

exit(0);
}


src/中放个Makefile:

obj = $(patsubst %.c,%,$(wildcard *.c))
CC = gcc
CFLAGS = -g -Wall
LIBS = ../lib/libapue.a

all: $(obj)

$(obj): %: %.c
$(CC) $(CFLAGS) $< $(LIBS) -o $@
clean:
-rm *~ *.out

depclean:
-rm *~ *.out $(obj)


至此,ok.

相关推荐