日期:2014-05-16  浏览次数:20702 次

在实验UNIX网络编程书上的程序时碰的问题
总是在使用unp.h里声明的函数时提示没有定义,网上说是没有把lib放进去,于是我想自己一个个的把函数原型放到我自己的文件里,顺便熟悉,但是碰到好多错误
unp01.c: 在函数‘err_doit’中:
unp01.c:27:3: 警告: 隐式声明与内建函数‘snprintf’不兼容 [默认启用]
unp01.c:31:3: 警告: 格式字符串不是一个字面字符串而且没有待格式化的实参 [-Wformat-security]
unp01.c:33:10: 错误: ‘stdout’未声明(在此函数内第一次使用)
unp01.c:33:10: 附注: 每个未声明的标识符在其出现的函数内只报告一次
unp01.c:34:14: 错误: ‘stderr’未声明(在此函数内第一次使用)


#include "unp01.h"
#include <stdlib.h>
#include <stdarg.h>
#include <syslog.h>
#include <string.h>
#include <errno.h>



#define MAXLINE 4096

int daemon_proc;

static void err_doit(int errnoflag, int level, const char *fmt, va_list ap)
{
int errno_save, n;
char buf[MAXLINE];

errno_save = errno; /* value caller might want printed */
#ifdef HAVE_VSNPRINTF
vsnprintf(buf, sizeof(buf), fmt, ap); /* this is safe */
#else
vsprintf(buf, fmt, ap); /* this is not safe */
#endif
n = strlen(buf);
if (errnoflag)
27 snprintf(buf+n, sizeof(buf)-n, ": %s", strerror(errno_save));
strcat(buf, "\n");

if (daemon_proc) {
31 syslog(level, buf);
} else {
fflush(stdout); /* in case stdout and stderr are the same */
fputs(buf, stderr);
fflush(stderr);
}
return;
}

void err_sys(const char *fmt, ...)
{
va_list ap;

va_start(ap, fmt);
err_doit(1, LOG_ERR, fmt, ap);
va_end(ap);
exit(1);
}


int Socket(int family, int type, int protocol)
{
int n;

if ( (n = socket(family, type, protocol)) < 0)
err_sys("socket error");
return(n);
}



------解决方案--------------------
unp01.c:33:10: 错误: ‘stdout’未声明(在此函数内第一次使用)
unp01.c:33:10: 附注: 每个未声明的标识符在其出现的函数内只报告一次
unp01.c:34:14: 错误: ‘stderr’未声明(在此函数内第一次使用)



这些都是什么标准输入输出,出错处理,可以完全不管他了。。。。 
或者你把unp.h文件放到/usr/include 下