Rocky9编译APUE

127 阅读2分钟

系统信息

[root@rocky9 ~]# uname -a
Linux rocky9 5.14.0-427.13.1.el9_4.x86_64 #1 SMP PREEMPT_DYNAMIC Wed May 1 19:11:28 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux
[root@rocky9 ~]# cat /etc/redhat-release 
Rocky Linux release 9.4 (Blue Onyx)

安装软件包

dnf install epel-release  # 后面安装libbsd-devel需要用到
dnf groupinstall "Development Tools"

下载APUE相关源码

wget http://www.apuebook.com/src.3e.tar.gz

开始编译

tar -xf src.3e.tar.gz
cd apue.3e/
make

报错解决

报错一:

      |                                                           ^~~~~
/usr/bin/ld: /tmp/ccuKbdpb.o: in function `main':
devrdev.c:(.text+0xa5): undefined reference to `minor'
/usr/bin/ld: devrdev.c:(.text+0xbb): undefined reference to `major'
/usr/bin/ld: devrdev.c:(.text+0x106): undefined reference to `minor'
/usr/bin/ld: devrdev.c:(.text+0x11c): undefined reference to `major'
collect2: error: ld returned 1 exit status
make[1]: *** [Makefile:18: devrdev] Error 1
make[1]: Leaving directory '/root/apue.3e/filedir'
make: *** [Makefile:6: all] Error 1

image.png

解决办法,修改devrdev.c文件,添加如下内容

(END)...skipping...
diff --git a/filedir/devrdev.c b/filedir/devrdev.c
index 43d523b..b639beb 100644
--- a/filedir/devrdev.c
+++ b/filedir/devrdev.c
@@ -1,4 +1,5 @@
 #include "apue.h"
+#include <sys/sysmacros.h>
 #ifdef SOLARIS
 #include <sys/mkdev.h>
 #endif

image.png

错误二

gcc -ansi -I../include -Wall -DLINUX -D_GNU_SOURCE  buf.c -o buf  -L../lib -lapue 
buf.c: In function ‘is_unbuffered’:
buf.c:90:15: error: ‘FILE’ has no member named ‘__pad’; did you mean ‘__pad5’?
   90 | #define _flag __pad[4]
      |               ^~~~~
buf.c:98:20: note: in expansion of macro ‘_flag’
   98 |         return(fp->_flag & _IONBF);
      |                    ^~~~~
buf.c: In function ‘is_linebuffered’:
buf.c:90:15: error: ‘FILE’ has no member named ‘__pad’; did you mean ‘__pad5’?
   90 | #define _flag __pad[4]
      |               ^~~~~
buf.c:104:20: note: in expansion of macro ‘_flag’
  104 |         return(fp->_flag & _IOLBF);
      |                    ^~~~~
buf.c: In function ‘buffer_size’:
buf.c:92:15: error: ‘FILE’ has no member named ‘__pad’; did you mean ‘__pad5’?
   92 | #define _base __pad[2]
      |               ^~~~~
buf.c:111:20: note: in expansion of macro ‘_base’
  111 |         return(fp->_base - fp->_ptr);
      |                    ^~~~~
buf.c:91:14: error: ‘FILE’ has no member named ‘__pad’; did you mean ‘__pad5’?
   91 | #define _ptr __pad[1]
      |              ^~~~~
buf.c:111:32: note: in expansion of macro ‘_ptr’
  111 |         return(fp->_base - fp->_ptr);
      |                                ^~~~

image.png

解决办法,修改buf.c,修改如下内容

diff --git a/stdio/buf.c b/stdio/buf.c
index 9555e3d..9d4bdf7 100644
--- a/stdio/buf.c
+++ b/stdio/buf.c
@@ -86,29 +86,23 @@ buffer_size(FILE *fp)
 
 #elif defined(_IONBF)
 
-#ifdef _LP64
-#define _flag __pad[4]
-#define _ptr __pad[1]
-#define _base __pad[2]
-#endif
-
 int
 is_unbuffered(FILE *fp)
 {
-       return(fp->_flag & _IONBF);
+       return(fp->_flags & _IONBF);
 }
 
 int
 is_linebuffered(FILE *fp)
 {
-       return(fp->_flag & _IOLBF);
+       return(fp->_flags & _IOLBF);
 }
 
 int
 buffer_size(FILE *fp)
 {
 #ifdef _LP64
-       return(fp->_base - fp->_ptr);
+       return(fp->_IO_buf_end - fp->_IO_buf_base);
 #else
        return(BUFSIZ); /* just a guess */
 #endif

image.png

错误三: 缺少bsd库

安装相应的库

dnf install libbsd-devel

编译完成后复制头文件和库文件到相应的系统目录中

cp /root/apue.3e/include/apue.h /usr/include/
cp /root/apue.3e/lib/libapue.a /usr/lib/

参考文章