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

 

posted on 2015-04-30 22:50  yachen  阅读(354)  评论(0编辑  收藏  举报

导航