红联Linux门户
Linux帮助

UNIX网络编程之环境配置

发布时间:2014-12-09 21:43:11来源:linux网站作者:traceorigin

开始学习《Unix网络编程》,输入第一个程序后,遇到各种错误,先将解决方案记录如下。

遇到的第一个错误是:没有找到头文件“unp.h”,该头文件是作者自己写的,并不包含在/usr/include中,这时需要到网上下载unpv13e.tar.gz到某一目录。具体操作:

mkdir /home/yourname/download%创建存放压缩文件的目录

tar -xzvf unpv13e.tar.gz%解压

ls -al%查看该目录下的文件

cdunpv13e%进入unpv13e


cat README%查看README文件,其中有具体的安装信息,按照操作即可


QUICK AND DIRTY

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
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
./daytimetcpcli 127.0.0.1

If all that works, you're all set to start compiling individual programs.


然后,复制unp.h和config.h到/usr/include
cp libunp.a /usr/lib
cp libunp.a /usr/lib64/
cd /lib
cp unp.h /usr/include
cp config.h /usr/include


最后,修改unp.h中的#include "../config.h"为#include "./config.h"。

以上是第一个错误,下面的错误是:
undefined reference to 'err_quit'
undefined reference to 'err_sys'
这也是由于,意思是未定义的声明,也就是说这些函数没有实现,这时候在网上找的自定义错误处理函数myerr.h


#include "apue.h"
#include <errno.h> /* for definition of errno */
#include <stdarg.h> /* ISO C variable aruments */
static void err_doit(int, int, const char *, va_list);
/*
* Nonfatal error related to a system call.
* Print a message and return.
*/
void
err_ret(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
err_doit(1, errno, fmt, ap);
va_end(ap);
}
/*
* Fatal error related to a system call.
* Print a message and terminate.
*/
void
err_sys(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
err_doit(1, errno, fmt, ap);
va_end(ap);
exit(1);
}
/*
* Fatal error unrelated to a system call.
* Error code passed as explict parameter.
* Print a message and terminate.
*/
void
err_exit(int error, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
err_doit(1, error, fmt, ap);
va_end(ap);
exit(1);
}
/*
* Fatal error related to a system call.
* Print a message, dump core, and terminate.
*/
void
err_dump(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
err_doit(1, errno, fmt, ap);
va_end(ap);
abort(); /* dump core and terminate */
exit(1); /* shouldn't get here */
}
/*
* Nonfatal error unrelated to a system call.
* Print a message and return.
*/
void
err_msg(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
err_doit(0, 0, fmt, ap);
va_end(ap);
}
/*
* Fatal error unrelated to a system call.
* Print a message and terminate.
*/
void
err_quit(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
err_doit(0, 0, fmt, ap);
va_end(ap);
exit(1);
}
/*
* Print a message and return to caller.
* Caller specifies "errnoflag".
*/
static void
err_doit(int errnoflag, int error, const char *fmt, va_list ap)
{
char buf[MAXLINE];
vsnprintf(buf, MAXLINE, fmt, ap);
if (errnoflag)
snprintf(buf+strlen(buf), MAXLINE-strlen(buf), ": %s",
strerror(error));
strcat(buf, " ");
fflush(stdout); /* in case stdout and stderr are the same */
fputs(buf, stderr);
fflush(NULL); /* flushes all stdio output streams */
}


将以上代码创建到/usr/include/myerr.h即可。
最后,还缺少"apue.h"文件,到网上下载src.tar.gz到/home/yourname/download下,然后
cd /home/yourname/download
tar -xzvf src.tar.gz
然后找到apue.h文件拷到/usr/include/即可。