apue 2ed 和 unp 3ed 环境配置
网上虽然有很多配置攻略,但是依然会一头雾水,下面记录我的配置过程。
OS. Ubuntu 10.04 LTS 5
首先下载APUE源代码(http://www.apuebook.com/src.tar.gz)和UNP源代码(http://www.unpbook.com/unpv13e.tar.gz)
先进行配置APUE环境:
1. 使用tar -zxvf命令解压src.tar.gz文件,我的解压位置为/home/yachen/Downloads/apue.2e
2. 修改Make.defines.linux文件,将WKDIR=/home/xxx/download/apue.2e改为你的解压目录
3. 打开std/linux.mk,将里面的2个nawk改为awk
4. 在include/apue.h中添加一行:
#define ARG_MAX 4096
分别打开threadtl/getenv1.c 和threadctl/getenv3.c,首部添加一行:
#include "apue.h"
打开threads/badexit2.c 修改第31行,将 pthread_self() 的返回值转换为 int 类型。
即将该行修改为 printf("thread 2: ID is %d\n", (int)pthread_self());
5.在apue.2e根目录下执行make
6.编译通过,复制include/apue.h到/usr/include下,lib/libapue.a 到/usr/lib/和 /usr/lib64(32位linux不需复制到第二个目录)下。
配置工作结束,编译文件时记得链接-lapue即可。
7. 测试成果,假设/tmp下有一个文件:threadid.c,内容如下(apue线程章节的例子):
#include <apue.h>
#include <pthread.h>
pthread_t ntid;
void
printids(const char *s)
{
pid_t pid;
pthread_t tid;
pid = getpid();
tid = pthread_self();
printf("%s pid %u tid %u (0x%x)\n", s, (unsigned int)pid,
(unsigned int)tid, (unsigned int)tid);
}
void *
thr_fn(void *arg)
{
printids("new thread: ");
return((void *)0);
}
int
main(void)
{
int err;
err = pthread_create(&ntid, NULL, thr_fn, NULL);
if (err != 0)
err_quit("can't create thread: %s\n", strerror(err));
printids("main thread:");
sleep(1);
exit(0);
}
使用如下命令编译:
gcc threadid.c -o threadid -lapue -lpthread
可以运行一下:
yachen@ubuntu:/tmp$ ./threadid
new thread: pid 17490 tid 816015696 (0x30a36950)
main thread: pid 17490 tid 823949040 (0x311c76f0)
下面是UNP环境的配置:
1.使用tar -zxvf命令解压unpv13e.tar.gz文件,我的解压位置为/home/yachen/Downloads/unpv13e
2.打开unpv13e目录,执行./configure
3.由于将unp.h放入/usr/include会可能遇到很多问题,我们新建一个代码目录,比如我建立在/home/yachen/Documents/unpcode文件夹,将unpv13e的config.h和lib文件夹下的unp.h放入unpcode文件夹,打开拷贝来的unp.h,将#include "../config.h"改为#include "config.h"并添加一行#define MAX_LINE 2048。
以后建立unp相关工程直接在此文件夹下建立工程目录,并在main.c中加入#include "../unp.h"即可。
4.重新进入unpv13e目录,进入lib目录,执行make命令,会在unpv13e目录下生成libunp.a,将该文件拷贝入/usr/lib和/usr/lib64(32位系统同APUE)
配置工作结束,编译文件时记得链接-lunp即可。
5. 测试成果
以如下main.c为例
#include "../unp.h"
int main() {
err_sys("wtf");
}
yachen@ubuntu:~/Documents/unp/intro$ gcc main.c -o main -lunp
yachen@ubuntu:~/Documents/unp/intro$ ./main
wtf: Success
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· AI与.NET技术实操系列(六):基于图像分类模型对图像进行分类