《UNIX环境高级编程编译源代码报错apue.h没有那个文件或目录

下载APUE源代码

在异步社区官网中找到《UNIX环境高级编程》页面,在其下方的配套资源中即可下载源码。

写好代码后,使用书上的方法编译出现问题:

cc myls.c

myls.c:1:10: fatal error: apue.h: 没有那个文件或目录
    1 | #include "apue.h"
      |          ^~~~~~~~
compilation terminated.

apue.h是作者自己封装的头文件,包含了某些标准系统头文件,定义了许多常量及函数原型。使用之前需要对其进行编译,生成对应的静态链接库,才能使用。

编译

cd apue.3e

make

  • 错误一:

    首先遇到的是“没有权限”的问题,解决:sudo chmod 777 * -R ,将该目录及子目录的权限设置为最高;


  • 错误二:

    make[1]: Entering directory '/home/pi/Downloads/apue.3e/filedir'
    gcc -ansi -I../include -Wall -DLINUX -D_GNU_SOURCE  access.c -o access  -L../lib -lapue 
    gcc -ansi -I../include -Wall -DLINUX -D_GNU_SOURCE  cdpwd.c -o cdpwd  -L../lib -lapue 
    gcc -ansi -I../include -Wall -DLINUX -D_GNU_SOURCE  changemod.c -o changemod  -L../lib -lapue 
    gcc -ansi -I../include -Wall -DLINUX -D_GNU_SOURCE  devrdev.c -o devrdev  -L../lib -lapue 
    devrdev.c: In function ‘main’:
    devrdev.c:19:25: warning: implicit declaration of function ‘major’ [-Wimplicit-function-declaration]
       printf("dev = %d/%d", major(buf.st_dev),  minor(buf.st_dev));
                             ^~~~~
    devrdev.c:19:45: warning: implicit declaration of function ‘minor’; did you mean ‘mknod’? [-Wimplicit-function-declaration]
       printf("dev = %d/%d", major(buf.st_dev),  minor(buf.st_dev));
                                                 ^~~~~
                                                 mknod
    /usr/bin/ld: /tmp/ccUJTVJZ.o: in function `main':
    devrdev.c:(.text+0x88): undefined reference to `major'
    /usr/bin/ld: devrdev.c:(.text+0x9c): undefined reference to `minor'
    /usr/bin/ld: devrdev.c:(.text+0xfc): undefined reference to `major'
    /usr/bin/ld: devrdev.c:(.text+0x110): undefined reference to `minor'
    collect2: error: ld returned 1 exit status
    make[1]: *** [Makefile:18: devrdev] Error 1
    make[1]: Leaving directory '/home/pi/Downloads/apue.3e/filedir'
    make: *** [Makefile:6: all] Error 1
    

    解决:

    filedir/devrdec.c中,添加头文件:#include <sys/sysmacros.h>


  • 错误三

    gcc -ansi -I../include -Wall -DLINUX -D_GNU_SOURCE  options.c -o options  -L../lib -lapue 
    make[1]: Leaving directory '/home/pi/Downloads/apue.3e/standards'
    making stdio
    make[1]: Entering directory '/home/pi/Downloads/apue.3e/stdio'
    gcc -ansi -I../include -Wall -DLINUX -D_GNU_SOURCE  buf.c -o buf  -L../lib -lapue 
    buf.c: In function ‘is_unbuffered’:
    buf.c:99:13: error: ‘FILE’ {aka ‘struct _IO_FILE’} has no member named ‘_flag’; did you mean ‘_flags’?
      return(fp->_flag & _IONBF);
                 ^~~~~
                 _flags
    buf.c: In function ‘is_linebuffered’:
    buf.c:105:13: error: ‘FILE’ {aka ‘struct _IO_FILE’} has no member named ‘_flag’; did you mean ‘_flags’?
      return(fp->_flag & _IOLBF);
                 ^~~~~
                 _flags
    buf.c: In function ‘is_unbuffered’:
    buf.c:100:1: warning: control reaches end of non-void function [-Wreturn-type]
     }
     ^
    buf.c: In function ‘is_linebuffered’:
    buf.c:106:1: warning: control reaches end of non-void function [-Wreturn-type]
     }
     ^
    make[1]: *** [Makefile:16: buf] Error 1
    make[1]: Leaving directory '/home/pi/Downloads/apue.3e/stdio'
    make: *** [Makefile:6: all] Error 1
    

    解决:

    修改stdio/buf.c ,将fp->_flag改为fp->_flags

    fp->_basefp->_ptr分别改为fp->_IO_read_base fp->_IO_read_ptr ,这两个是我根据FILE结构体的定义猜的。

    image-20211125162329143

编译成功的基础上,我们进行安装apue.h文件及其对应的静态链接库libapue.a

sudo cp ./include/apue.h /usr/include/

sudo cp ./lib/libapue.a /usr/local/lib/

为什么要将libapue.a移到/usr/local/lib中呢?原因是libapue.a是apue.h头文件中包含的所有函数及宏定义的具体实现,是一个静态链接库。

查看ld.conf.d/libc.conf你会发现gcc在搜索链接库的时候默认会去搜索/usr/local/lib/中的文件,所以我们将其放在这里,一劳永逸。

运行

gcc myls.c -o myls -lapue (libapue.a在这里要写成apue

./myls /dev

  • 版权声明: 本博客所有文章除特别声明外,著作权归作者所有。转载请注明出处!
  • Copyrights © 2022-2024 lk
  • 访问人数: | 浏览次数:

请我喝杯咖啡吧~

支付宝
微信