下载源码
APUE源码下载: http://www.apuebook.com/ (选择书的版本,然后Source Code)
我下载的是e3,第三版的书的源代码
编译
一般我们看到一个软件包,第一时间都是先读README文件,然后发现里面说得很简单,直接在软件包目录下执行make就行了
wolfdan@wolfdan-PC $ cat README
Read the file called DISCLAIMER.
On Freebsd, type "gmake".
On other platforms, type "make" (as long as this is gnu make).
For FAQs, updated source code, and the lost chapter, see http://www.apuebook.com.
Please direct questions, suggestions, and bug reports to [email protected].
Steve Rago
January 2013
# 因为我们是linux操作系统,所以我们现在make执行
wolfdan@wolfdan-PC $ make
...[此处省略很多的输出]
make[1]: Entering directory '/home/wolfdan/Documents/Book/APUE/apue.3e/threads'
gcc -ansi -I../include -Wall -DLINUX -D_GNU_SOURCE badexit2.c -o badexit2 -L../lib -lapue -pthread -lrt -lbsd
/usr/bin/ld: 找不到 -lbsd
collect2: error: ld returned 1 exit status
Makefile:31: recipe for target 'badexit2' failed
make[1]: *** [badexit2] Error 1
make[1]: Leaving directory '/home/wolfdan/Documents/Book/APUE/apue.3e/threads'
Makefile:6: recipe for target 'all' failed
make: *** [all] Error 1
# 报错,解决(其实是缺了一个库),所以我们安装库
# 可以使用, sudo apt-get install libbsd-dev , 也可以使用如下命令
wolfdan@wolfdan-PC $ sudo aptitude install libbsd-dev
...[一些输出的省略]
# 再次make
wolfdan@wolfdan-PC $ make
...[一些输出的省略],没有报错了
apue.h全局的一些配置
# 复制我们apue的一些头文件复制到系统路径上面
wolfdan@wolfdan-PC $ sudo cp ./include/apue.h /usr/include/ && sudo cp ./lib/error.c /usr/include && sudo cp ./lib/libapue.a /usr/local/lib/
# 在apue.h最后加一行代码[注意是endif的前一行] include "error.c"
wolfdan@wolfdan-PC $ vim /usr/include/apue.h
wolfdan@wolfdan-PC $ tail /usr/include/apue.h
void log_exit(int, const char *, ...) __attribute__((noreturn));
void TELL_WAIT(void); /* parent/child from {Sec race_conditions} */
void TELL_PARENT(pid_t);
void TELL_CHILD(pid_t);
void WAIT_PARENT(void);
void WAIT_CHILD(void);
#include "error.c"
#endif /* _APUE_H */
注意
在/usr/include/apue.h
最后加一行代码[注意是endif的前一行] include “error.c”
注意上面的路径不是 ./include/apue.h
测试
wolfdan@wolfdan-PC:~/Desktop/code/apue$ mkdir filedir
wolfdan@wolfdan-PC:~/Desktop/code/apue$ cd filedir/
wolfdan@wolfdan-PC:~/Desktop/code/apue/filedir$ vim filetype.c
wolfdan@wolfdan-PC:~/Desktop/code/apue/filedir$ cat filetype.c
#include "apue.h"
int
main(int argc, char *argv[])
{
int i;
struct stat buf;
char *ptr;
for (i = 1; i < argc; i++) {
printf("%s: ", argv[i]);
if (lstat(argv[i], &buf) < 0) {
err_ret("lstat error");
continue;
}
if (S_ISREG(buf.st_mode))
ptr = "regular";
else if (S_ISDIR(buf.st_mode))
ptr = "directory";
else if (S_ISCHR(buf.st_mode))
ptr = "character special";
else if (S_ISBLK(buf.st_mode))
ptr = "block special";
else if (S_ISFIFO(buf.st_mode))
ptr = "fifo";
else if (S_ISLNK(buf.st_mode))
ptr = "symbolic link";
else if (S_ISSOCK(buf.st_mode))
ptr = "socket";
else
ptr = "** unknown mode **";
printf("%s\n", ptr);
}
exit(0);
}
wolfdan@wolfdan-PC:~/Desktop/code/apue/filedir$ gcc filetype.c -o filetype.o
wolfdan@wolfdan-PC:~/Desktop/code/apue/filedir$ ./filetype.o /etc/passwd /etc /dev/log /dev/tty /var/lib/oprofile/opd_pipe /dev/sr0 /dev/cdrom
/etc/passwd: regular
/etc: directory
/dev/log: symbolic link
/dev/tty: character special
/var/lib/oprofile/opd_pipe: lstat error: No such file or directory
/dev/sr0: lstat error: No such file or directory
/dev/cdrom: lstat error: No such file or directory
个人环境说明
Deepin15.11