根据网上各个博客论坛的方法,总算弄成了,在这里分享出来,亲测可行
1.APUE2源代码下载:http://www.apuebook.com
2.解压缩:tar -xzvf src.tar.gz
3.进入目录apue.2e,把文件Make.defines.linux 中的 WKDIR=/home/xxx/apue.2e 修改为 WKDIR=/home/yarkee/apue.2e
4.进入apue.2e目录下的std目录,打开linux.mk,将里面的nawk全部替换为awk
5.回到 /home/yarkee/apue.2e 目录 make
这里我出的错是:
/usr/include/bits/timex.h:31:7: 错误:expected ‘:’, ‘,’, ‘;’, ‘}’ or ‘__attribute__’ before ‘.’ token
make[2]: *** [printd.o] 错误 1
make[2]: 离开目录“/home/alex/apue/apue.2e/ipp”
make[1]: *** [linux] 错误 1
make[1]: 离开目录“/home/alex/apue/apue.2e”
make: *** [all] 错误 2
解决方法:
出现这个问题的原因是在timex.h的第31行出现了status的定义:
int status; /* clock command/status */
而文件apue.2e/ipp/ipp.h中有宏定义:
#define status u.st
这样编译的时候就出问题了,如编译提示:
expected ‘:’, ‘,’, ‘;’, ‘}’ or ‘__attribute__’ before ‘.’ token
解决方法是把ipp.h文件里的status改成其他名称,比如Status,再次编译,错误提示status神马的信息,找到status出错的位置,把它改为Status就行了。
此处我的status出错位置在printd.c的第977行中:
i = ntohs(hp->status);
再次make就好了。
6.然后把 /home/yarkee/apue.2e/inlcude 目录下的 apue.h 文件和位于 /home/yarkee/apue.2e/lib 目录下的 error.c 文件都复制到 /usr/include 目录下。
编辑一下复制过来的 apue.h 文件在最后一行 #endif 前面添加一行 #include “error.c”7.拷贝apue2e_src/apue.2e/include/apue.h和apue2e_src/apue.2e/lib/libapue.a到你的源代码目录。
8.使用gcc -o hello hello.c 来编译你的源代码
这里我出的错是:
undefined reference to `pthread_create'
解决办法:
因为pthread 库不是 Linux 系统默认的库,连接时需要使用静态库 libpthread.a,所以在使用pthread_create()创建线程,以及调用 pthread_atfork()函数建立fork处理程序时,需要链接该库。
加编译参数 -lpthread 即可。
以上均是参考网上各个博客总结起来的。