apue.3e遇到的编译问题(recipe for target 'badexit2' failed)以及ls1.c案例测试

发布时间 2023-07-28 23:21:31作者: 机器猫007

APUE( 慢慢啃这本书)编译问题,网上获得的前辈方法,本地测试可行,仅作记录。

1.系统环境

2.下载解压

tar -zxvf *.tar.gz
cd ./apue.3e
make

报错:

collect2: error: ld returned 1 exit status
Makefile:31: recipe for target 'badexit2' failed
make[1]: *** [badexit2] Error 1
make[1]: Leaving directory '/home/pi/Downloads/apue.3e/threads'
Makefile:6: recipe for target 'all' failed
make: *** [all] Error 1

3.继续编译

sudo apt-get install libbsd-dev
make

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

sudo cp ./include/apue.h /usr/local/include/
sudo cp ./lib/libapue.a /usr/local/lib/
mac中,拷贝头文件 sudo cp ./include/apue.h /usr/local/include/

为什么要将 libapue.a 移到 /usr/local/lib 中呢?
因为 libapue.a 是 apue.h 头文件中包含的所有函数及宏定义的具体实现,是一个静态链接库。
查看 /etc/ld.so.conf.d/libc.conf 你会发现 gcc 在搜索链接库的时候默认会去搜索 /usr/local/lib/ 中的文件,所以我们将其放在这里,一劳永逸。

5.编译测试ls1.c文件

gcc ls1.c -o ls22 -lapue
以下是ls1.c文件

include "apue.h"

include <dirent.h>

int
main(int argc, char *argv[])
{
DIR *dp;
struct dirent *dirp;

if (argc != 2)
err_quit("usage: ls directory_name");

if ((dp = opendir(argv[1])) == NULL)
err_sys("can't open %s", argv[1]);
while ((dirp = readdir(dp)) != NULL)
printf("%s\n", dirp->d_name);

closedir(dp);
exit(0);
}

6.参考文档: http://www.cnblogs.com/52php/p/5681701.html