当前位置: 技术问答>linux和unix
为什么我的这段代码编译不过去?那位大哥帮帮忙!谢谢!
来源: 互联网 发布时间:2014-12-25
本文导语: /* *liberr.h */ #ifndef LIBERR_H #define LIBERR_H #include #define MAXLINELEN 4096 void err_ret(const char *fmt, ...); void err_quit(const char *fmt, ...); void log_ret(char *logfile, const char *fmt, ...); void log_quit(char *logfile, const char *fmt, ...)...
/*
*liberr.h
*/
#ifndef LIBERR_H
#define LIBERR_H
#include
#define MAXLINELEN 4096
void err_ret(const char *fmt, ...);
void err_quit(const char *fmt, ...);
void log_ret(char *logfile, const char *fmt, ...);
void log_quit(char *logfile, const char *fmt, ...);
void err_prn(const *fmt, va_list ap, char *logfile);
#endif LIBERR_H
/*
*liberr.c
*/
#include
#include
#include
#include
#include "liberr.h"
void err_ret(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
err_prn(fmt, ap, NULL);
va_end(ap);
return;
}
void err_quit(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
err_prn(fmt, ap, NULL);
va_end(ap);
exit(EXIT_FAILURE);
}
void log_ret(char *logfile, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
err_prn(fmt, ap, NULL);
va_end(ap);
return;
}
void log_quit(char *logfile, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
err_prn(fmt, ap, NULL);
va_end(ap);
exit(EXIT_FAILURE);
}
extern void err_prn(const char *fmt, va_list ap, char *logfile)
{
int save_err;
char buf[MAXLINELEN];
FILE *plf;
save_err = errno;
vsprintf(buf, fmt, ap);
sprintf(buf + strlen(buf), ": %s", strerror(save_err));
strcat(buf, "n");
fflush(stdout);
if(logfile != NULL)
if((plf = fopen(logfile, "a")) != NULL)
{
fputs(buf, plf);
fclose(plf);
}
else
fputs("failed to open log filen", stderr);
else
fputs(buf, stderr);
fflush(NULL);
return;
}
[wb@webmail wb]$ gcc -c liberr.c -o liberr.o
错误信息如下:
In file included from liberr.c:8:
liberr.h:16:8: warning: extra tokens at end of #endif directive
liberr.c: In function `err_ret':
liberr.c:15: warning: passing arg 1 of `err_prn' from incompatible pointer type
liberr.c: In function `err_quit':
liberr.c:25: warning: passing arg 1 of `err_prn' from incompatible pointer type
liberr.c:27: `EXIT_FAILURE' undeclared (first use in this function)
liberr.c:27: (Each undeclared identifier is reported only once
liberr.c:27: for each function it appears in.)
liberr.c: In function `log_ret':
liberr.c:35: warning: passing arg 1 of `err_prn' from incompatible pointer type
liberr.c: In function `log_quit':
liberr.c:45: warning: passing arg 1 of `err_prn' from incompatible pointer type
liberr.c:47: `EXIT_FAILURE' undeclared (first use in this function)
liberr.c: At top level:
liberr.c:51: conflicting types for `err_prn'
liberr.h:14: previous declaration of `err_prn'
[wb@webmail wb]$
*liberr.h
*/
#ifndef LIBERR_H
#define LIBERR_H
#include
#define MAXLINELEN 4096
void err_ret(const char *fmt, ...);
void err_quit(const char *fmt, ...);
void log_ret(char *logfile, const char *fmt, ...);
void log_quit(char *logfile, const char *fmt, ...);
void err_prn(const *fmt, va_list ap, char *logfile);
#endif LIBERR_H
/*
*liberr.c
*/
#include
#include
#include
#include
#include "liberr.h"
void err_ret(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
err_prn(fmt, ap, NULL);
va_end(ap);
return;
}
void err_quit(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
err_prn(fmt, ap, NULL);
va_end(ap);
exit(EXIT_FAILURE);
}
void log_ret(char *logfile, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
err_prn(fmt, ap, NULL);
va_end(ap);
return;
}
void log_quit(char *logfile, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
err_prn(fmt, ap, NULL);
va_end(ap);
exit(EXIT_FAILURE);
}
extern void err_prn(const char *fmt, va_list ap, char *logfile)
{
int save_err;
char buf[MAXLINELEN];
FILE *plf;
save_err = errno;
vsprintf(buf, fmt, ap);
sprintf(buf + strlen(buf), ": %s", strerror(save_err));
strcat(buf, "n");
fflush(stdout);
if(logfile != NULL)
if((plf = fopen(logfile, "a")) != NULL)
{
fputs(buf, plf);
fclose(plf);
}
else
fputs("failed to open log filen", stderr);
else
fputs(buf, stderr);
fflush(NULL);
return;
}
[wb@webmail wb]$ gcc -c liberr.c -o liberr.o
错误信息如下:
In file included from liberr.c:8:
liberr.h:16:8: warning: extra tokens at end of #endif directive
liberr.c: In function `err_ret':
liberr.c:15: warning: passing arg 1 of `err_prn' from incompatible pointer type
liberr.c: In function `err_quit':
liberr.c:25: warning: passing arg 1 of `err_prn' from incompatible pointer type
liberr.c:27: `EXIT_FAILURE' undeclared (first use in this function)
liberr.c:27: (Each undeclared identifier is reported only once
liberr.c:27: for each function it appears in.)
liberr.c: In function `log_ret':
liberr.c:35: warning: passing arg 1 of `err_prn' from incompatible pointer type
liberr.c: In function `log_quit':
liberr.c:45: warning: passing arg 1 of `err_prn' from incompatible pointer type
liberr.c:47: `EXIT_FAILURE' undeclared (first use in this function)
liberr.c: At top level:
liberr.c:51: conflicting types for `err_prn'
liberr.h:14: previous declaration of `err_prn'
[wb@webmail wb]$
|
你的liberr.h有错误,正确的如下:
*******************
#ifndef LIBERR_H
#define LIBERR_H
#include
#define MAXLINELEN 4096
#define EXIT_FAILURE -1 //这里预定义EXIT_FAILURE
void err_ret(const char *fmt, ...);
void err_quit(const char *fmt, ...);
void log_ret(char *logfile, const char *fmt, ...);
void log_quit(char *logfile, const char *fmt, ...);
void err_prn(const char *fmt, va_list ap, char *logfile);
//这里的第一个参数的类型为const char * ,不是const *
#endif
*******************
#ifndef LIBERR_H
#define LIBERR_H
#include
#define MAXLINELEN 4096
#define EXIT_FAILURE -1 //这里预定义EXIT_FAILURE
void err_ret(const char *fmt, ...);
void err_quit(const char *fmt, ...);
void log_ret(char *logfile, const char *fmt, ...);
void log_quit(char *logfile, const char *fmt, ...);
void err_prn(const char *fmt, va_list ap, char *logfile);
//这里的第一个参数的类型为const char * ,不是const *
#endif
|
EXIT_FAILURE没有定义
|
缺文件,全传上来