b2科目四模拟试题多少题驾考考爆了怎么补救
b2科目四模拟试题多少题 驾考考爆了怎么补救

《APUE》和《UNP》文件的编译和使用(转载)

电脑杂谈  发布时间:2019-09-25 06:03:45  来源:网络整理

对话框阅读顺序_unp和apue阅读顺序_apue unp

source code中的README只提供了编译方式,要是自己敲代码的话,需要稍微修改一下。

1.编译《APUE》

在那里编译都是一样,我们应该的是libapue.a 和apue.h文件。

我使用的目录是/home/dan/download/apue.2e/

修改Make.defines.linux文件中,

WKDIR=/home/dan/download/apue.2e

在我的机器上编译时,提示ARG_MAX未定义,可以这样修改。

在apue.2e/include/apue.h中添加一行:

#define ARG_MAX 4096

打开apue.2e/threadtl/getenv1.c 和apue.2e/threadctl/getenv3.c,添加一行:

#include "apue.h"

这样就可以编译通过了,复制apue.2e/include/apue.h到/usr/include下,

apue.2e/lib/libapue.a 到/usr/lib/和 /usr/lib64下。

2. 使用apue.h文件和libapue.a库。

假定/tmp下有一个文件:threadid.c,内容如下(apue线程章节的实例):

#include

#include

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: ");

unp和apue阅读顺序_apue unp_对话框阅读顺序

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);

}

使用如下命令编译:

cc -o threadid threadid.c -lapue -lpthread

可以运行一下:

dan@dan-laptop:/tmp$ ./threadid

new thread: pid 17490 tid 816015696 (0x30a36950)

main thread: pid 17490 tid 823949040 (0x311c76f0)

3. 编译《UNP》

这个稍微麻烦些。

我们首先造成一个目录,以后自己的代码就敲在这个目录里。

mkdir /home/dan/study/unp

仍然是下载到/home/dan/download/,解压缩,进入目录

cd /home/dan/download/unpv13e/

README文件中说的很具体:

========================================

Execute the following from the src/ directory:

./configure # try to figure out all implementation differences

cd lib # build the basic library that all programs need

make # use "gmake" everywhere on BSD/OS systems

cd ../libfree # continue building the basic library

apue unp_unp和apue阅读顺序_对话框阅读顺序

make

cd ../libroute # only if your system supports 4.4BSD style routing sockets

make # only if your system supports 4.4BSD style routing sockets

cd ../libxti # only if your system supports XTI

make # only if your system supports XTI

cd ../intro # build and test a basic client program

make daytimetcpcli

========================================

这里只编译lib下的文件,这样可以产生libunp.a,复制这个静态库到/usr/lib/和/usr/lib64/

如果提示:

unp.h:139: error: conflicting types for ‘socklen_t’

/usr/include/bits/socket.h:35: error: previous declaration of ‘socklen_t’ was here

需要注解掉当前目录中unp.h的第139行。

复制libunp.a到系统目录:

root@dan-laptop:/home/dan/download/unpv13e/lib# cp ../libunp.a /usr/lib

root@dan-laptop:/home/dan/download/unpv13e/lib# cp ../libunp.a /usr/lib64/

4.使用unp.h和libunp.a

如果直接复制unpv13e/lib/unp.h到/usr/include,那么在别的目录编译书上代码时,很可会受到类似上面的错误:

In file included from daytimetcpsrv1.c:1:

/usr/include/unp.h:227: error: redefinition of ‘struct sockaddr_storage’

In file included from daytimetcpsrv1.c:1:

/usr/include/unp.h:249:30: error: ../lib/addrinfo.h: No such file or directory

/usr/include/unp.h:263: error: redefinition of ‘struct timespec’

/usr/include/unp.h:363: error: conflicting types for ‘gai_strerror’

/usr/include/netdb.h:647: error: previous declaration of ‘gai_strerror’ was here

/usr/include/unp.h:367: error: conflicting types for ‘getnameinfo’

/usr/include/netdb.h:653: error: previous declaration of ‘getnameinfo’ was here

/usr/include/unp.h:371: error: conflicting types for ‘gethostname’

/usr/include/unistd.h:857: error: previous declaration of ‘gethostname’ was here

/usr/include/unp.h:387: error: conflicting types for ‘inet_ntop’

/usr/include/arpa/inet.h:65: error: previous declaration of ‘inet_ntop’ was here

/usr/include/unp.h:395: error: conflicting types for ‘pselect’

unp和apue阅读顺序_apue unp_对话框阅读顺序

/usr/include/sys/select.h:121: error: previous declaration of ‘pselect’ was here

daytimetcpsrv1.c: In function ‘main’:

daytimetcpsrv1.c:9: error: ‘MAX_LINE’ undeclared (first use in this function)

daytimetcpsrv1.c:9: error: (Each undeclared identifier is reported only once

daytimetcpsrv1.c:9: error: for each function it appears in.)

dan@dan-laptop:~/study/unp/4$ rm -f /usr/include/unp.h

解决方法有点傻:

进入我们起初时建立的目录:

cd /home/dan/study/unp

复制config.h和unp.h到此目录:

dan@dan-laptop:~/study/unp$ cp /home/dan/download/unpv13e/config.h .

dan@dan-laptop:~/study/unp$ cp /home/dan/download/unpv13e/lib/unp.h .

修改unp.hunp和apue阅读顺序unp和apue阅读顺序

#include "../config.h"改成 #include "config.h"

添加一行:

#define MAX_LINE 2048

练习书上代码时,在unp目录下创建相应的章节目录,文件中添加一行:

#include "../unp.h"

编译时链接unp库就可以了。

以第四章的daytimetcpsrv1.c为例:

dan@dan-laptop:~/study/unp/4$ pwd

/home/dan/study/unp/4

dan@dan-laptop:~/study/unp/4$ cat daytimetcpsrv1.c

#include "../unp.h"

#include

int main(int argc, char **argv)

{

int listenfd, connfd;

socklen_t len;

struct sockaddr_in servaddr, cliaddr;

char buff[MAX_LINE];

time_t ticks;

unp和apue阅读顺序_对话框阅读顺序_apue unp

listenfd = Socket(AF_INET, SOCK_STREAM, 0);

bzero(&servaddr, sizeof(servaddr));

servaddr.sin_family = AF_INET;

servaddr.sin_addr.s_addr = htonl(INADDR_ANY);

servaddr.sin_port = htons(13);

Bind(listenfd, (SA *)&servaddr, sizeof(servaddr));

Listen(listenfd, LISTENQ);

for (;;) {

len = sizeof(cliaddr);

connfd = Accept(listenfd, (SA *)&cliaddr, &len);

printf("connection from %s, port %d\n",

Inet_ntop(AF_INET, &cliaddr.sin_addr, buff, sizeof(buff)),

ntohs(cliaddr.sin_port));

ticks = time(NULL);

snprintf(buff, sizeof(buff), "%.24s\r\n", ctime(&ticks));

Write(connfd, buff, strlen(buff));

Close(connfd);

}

}

编译:

cc -o daytimetcpsrv1 daytimetcpsrv1.c -lunp

运行一下:

root@dan-laptop:/home/dan/study/unp/4# ./daytimetcpsrv1 &

[1] 22106

root@dan-laptop:/home/dan/study/unp/4#

root@dan-laptop:/home/dan/study/unp/4# ./daytimetcpcli

usage: a.out

root@dan-laptop:/home/dan/study/unp/4# ./daytimetcpcli 127.0.0.1

connection from 127.0.0.1, port 42064

Fri Aug 21 23:03:56 2009

root@dan-laptop:/home/dan/study/unp/4# netstat -nt

Active Internet connections (w/o servers)

Proto Recv-Q Send-Q Local Address Foreign Address State

tcp 0 0 127.0.0.1:13 127.0.0.1:42064 TIME_WAIT


本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-124071-1.html

    相关阅读
      发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

      • 魏楠
        魏楠

        股票会套牢

      • 苏建军
        苏建军

        处理没有问题

      • 鹰眼朱洛基尔诺
        鹰眼朱洛基尔诺

        中国应该马上派战机和导弹严陈以待

      热点图片
      拼命载入中...