apue.h不存在,以及apue源码下载编译的解决方法

17 篇文章 0 订阅

下载源码

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 sar@apuebook.com.

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值