歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> UNIX網絡編程之環境配置

UNIX網絡編程之環境配置

日期:2017/2/28 16:05:17   编辑:Linux教程

開始學習《Unix網絡編程》,輸入第一個程序後,遇到各種錯誤,先將解決方案記錄如下。

遇到的第一個錯誤是:沒有找到頭文件“unp.h”,該頭文件是作者自己寫的,並不包含在/usr/include中,這時需要到網上下載unpv13e.tar.gz到某一目錄。具體操作:

mkdir /home/yourname/download %創建存放壓縮文件的目錄


tar -xzvf unpv13e.tar.gz %解壓


ls -al %查看該目錄下的文件


cd unpv13e %進入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/即可

Copyright © Linux教程網 All Rights Reserved