#include <stdio.h>
#include "log4c.h"
int main(int argc, char** argv){
int rc = 0;
log4c_category_t* mycat = NULL;
if (log4c_init()){
printf("log4c_init() failed");
rc = 1;
}else{
mycat = log4c_category_get("log4c.examples.helloworld");
log4c_category_log(mycat, LOG4C_PRIORITY_ERROR, "Hello World!");
/* Explicitly call the log4c cleanup routine */
if ( log4c_fini()){
printf("log4c_fini() failed");
}
}
return 0;
}
我用的编译器是DEV-C++,编译的时候是把用到的log4c的.h文件和.c文件都加到工程里,但是编译的时候报错,不知什么原因。
11 个解决方案
#1
报什么错?
#2
D:\CProject\log4chelloworld\log4c\appender.c In function `log4c_appender_get':
101 D:\CProject\log4chelloworld\log4c\appender.c initializer element is not constant
101 D:\CProject\log4chelloworld\log4c\appender.c (near initialization for `log4c_appender_factory_ops.fac_new')
102 D:\CProject\log4chelloworld\log4c\appender.c initializer element is not constant
102 D:\CProject\log4chelloworld\log4c\appender.c (near initialization for `log4c_appender_factory_ops.fac_delete')
103 D:\CProject\log4chelloworld\log4c\appender.c initializer element is not constant
103 D:\CProject\log4chelloworld\log4c\appender.c (near initialization for `log4c_appender_factory_ops.fac_print')
At top level:
120 D:\CProject\log4chelloworld\log4c\appender.c [Warning] 'log4c_appender_new' defined locally after being referenced with dllimport linkage
137 D:\CProject\log4chelloworld\log4c\appender.c [Warning] 'log4c_appender_delete' defined locally after being referenced with dllimport linkage
211 D:\CProject\log4chelloworld\log4c\appender.c [Warning] 'log4c_appender_set_udata' defined locally after being referenced with dllimport linkage
280 D:\CProject\log4chelloworld\log4c\appender.c [Warning] 'log4c_appender_close' defined locally after being referenced with dllimport linkage
302 D:\CProject\log4chelloworld\log4c\appender.c [Warning] 'log4c_appender_print' defined locally after being referenced with dllimport linkage
D:\CProject\log4chelloworld\Makefile.win [Build Error] [log4c/appender.o] Error 1
我没有将Log4c的源程序进行任何编译,只是把它src目录下的一些源程序直接拿过来用的,不知道这样行不行
101 D:\CProject\log4chelloworld\log4c\appender.c initializer element is not constant
101 D:\CProject\log4chelloworld\log4c\appender.c (near initialization for `log4c_appender_factory_ops.fac_new')
102 D:\CProject\log4chelloworld\log4c\appender.c initializer element is not constant
102 D:\CProject\log4chelloworld\log4c\appender.c (near initialization for `log4c_appender_factory_ops.fac_delete')
103 D:\CProject\log4chelloworld\log4c\appender.c initializer element is not constant
103 D:\CProject\log4chelloworld\log4c\appender.c (near initialization for `log4c_appender_factory_ops.fac_print')
At top level:
120 D:\CProject\log4chelloworld\log4c\appender.c [Warning] 'log4c_appender_new' defined locally after being referenced with dllimport linkage
137 D:\CProject\log4chelloworld\log4c\appender.c [Warning] 'log4c_appender_delete' defined locally after being referenced with dllimport linkage
211 D:\CProject\log4chelloworld\log4c\appender.c [Warning] 'log4c_appender_set_udata' defined locally after being referenced with dllimport linkage
280 D:\CProject\log4chelloworld\log4c\appender.c [Warning] 'log4c_appender_close' defined locally after being referenced with dllimport linkage
302 D:\CProject\log4chelloworld\log4c\appender.c [Warning] 'log4c_appender_print' defined locally after being referenced with dllimport linkage
D:\CProject\log4chelloworld\Makefile.win [Build Error] [log4c/appender.o] Error 1
我没有将Log4c的源程序进行任何编译,只是把它src目录下的一些源程序直接拿过来用的,不知道这样行不行
#3
不可以的。
你需要先编译,生成动态或静态库后,再链接使用。
#4
注意看源码包下类似:README或INSTALL或BUILD之类的文档。
#5
可是好像需要在linux环境里面编译,我用cygwin编译后好像又对cygwin有依赖,可以直接在windows环境下编译吗?没搞过C啊,现在临时研究起来头疼啊!
#6
怎么不用logforcplus?
#7
我只想找个可以输出C日志文件的工具就可以了,使用越简单越好,之前看到一个将LOG4C简化成只有一个头文件的,一直也没有实验成功。
#8
MyLog.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef WIN32
#include <windows.h>
#include <io.h>
#else
#include <unistd.h>
#include <sys/time.h>
#include <pthread.h>
#define CRITICAL_SECTION pthread_mutex_t
#define _vsnprintf vsnprintf
#endif
//Log{
#define MAXLOGSIZE 100000000
#define ARRSIZE(x) (sizeof(x)/sizeof(x[0]))
#include <time.h>
#include <stdarg.h>
char logfilename1[]="MyLog1.log";
char logfilename2[]="MyLog2.log";
char logstr[16000];
char datestr[16];
char timestr[16];
char ms10[3];
CRITICAL_SECTION cs_log;
FILE *flog;
int centisec() {
#ifdef WIN32
return ((GetTickCount()%1000L)/10)%100;
#else
struct timeval tv;
if (!gettimeofday(&tv,NULL)) {
return ((tv.tv_usec%1000000L)/10000)%100;
} else {
return 0;
}
#endif
}
#ifdef WIN32
void Lock(CRITICAL_SECTION *l) {
EnterCriticalSection(l);
}
void Unlock(CRITICAL_SECTION *l) {
LeaveCriticalSection(l);
}
#else
void Lock(CRITICAL_SECTION *l) {
pthread_mutex_lock(l);
}
void Unlock(CRITICAL_SECTION *l) {
pthread_mutex_unlock(l);
}
#endif
void LogV(const char *pszFmt,va_list argp) {
struct tm *now;
time_t aclock;
if (NULL==pszFmt||0==pszFmt[0]) return;
if (-1==_vsnprintf(logstr,ARRSIZE(logstr),pszFmt,argp)) logstr[ARRSIZE(logstr)-1]=0;
time(&aclock);
now=localtime(&aclock);
sprintf(datestr,"%04d-%02d-%02d",now->tm_year+1900,now->tm_mon+1,now->tm_mday);
sprintf(timestr,"%02d:%02d:%02d",now->tm_hour ,now->tm_min ,now->tm_sec );
sprintf(ms10,"%02d",centisec());
printf("%s %s.%s %s",datestr,timestr,ms10,logstr);
flog=fopen(logfilename1,"a");
if (NULL!=flog) {
fprintf(flog,"%s %s.%s %s",datestr,timestr,ms10,logstr);
if (ftell(flog)>MAXLOGSIZE) {
fclose(flog);
if (rename(logfilename1,logfilename2)) {
remove(logfilename2);
rename(logfilename1,logfilename2);
}
flog=fopen(logfilename1,"a");
if (NULL==flog) return;
}
fclose(flog);
}
}
void Log(const char *pszFmt,...) {
va_list argp;
Lock(&cs_log);
va_start(argp,pszFmt);
LogV(pszFmt,argp);
va_end(argp);
Unlock(&cs_log);
}
//Log}
int main(int argc,char * argv[]) {
int i;
#ifdef WIN32
InitializeCriticalSection(&cs_log);
#else
pthread_mutex_init(&cs_log,NULL);
#endif
for (i=0;i<10000;i++) {
Log("This is a Log %04d from FILE:%s LINE:%d\n",i, __FILE__, __LINE__);
}
#ifdef WIN32
DeleteCriticalSection(&cs_log);
#else
pthread_mutex_destroy(&cs_log);
#endif
return 0;
}
#9
楼上的大哥是自己写文件实现日志的功能吗?
#10
我在网上下了log4cplus的dll,不知如何使用
#11
你好 有LOG4C的例子吗
谢谢
可以给我现成的例子吗
谢谢
可以给我现成的例子吗
#1
报什么错?
#2
D:\CProject\log4chelloworld\log4c\appender.c In function `log4c_appender_get':
101 D:\CProject\log4chelloworld\log4c\appender.c initializer element is not constant
101 D:\CProject\log4chelloworld\log4c\appender.c (near initialization for `log4c_appender_factory_ops.fac_new')
102 D:\CProject\log4chelloworld\log4c\appender.c initializer element is not constant
102 D:\CProject\log4chelloworld\log4c\appender.c (near initialization for `log4c_appender_factory_ops.fac_delete')
103 D:\CProject\log4chelloworld\log4c\appender.c initializer element is not constant
103 D:\CProject\log4chelloworld\log4c\appender.c (near initialization for `log4c_appender_factory_ops.fac_print')
At top level:
120 D:\CProject\log4chelloworld\log4c\appender.c [Warning] 'log4c_appender_new' defined locally after being referenced with dllimport linkage
137 D:\CProject\log4chelloworld\log4c\appender.c [Warning] 'log4c_appender_delete' defined locally after being referenced with dllimport linkage
211 D:\CProject\log4chelloworld\log4c\appender.c [Warning] 'log4c_appender_set_udata' defined locally after being referenced with dllimport linkage
280 D:\CProject\log4chelloworld\log4c\appender.c [Warning] 'log4c_appender_close' defined locally after being referenced with dllimport linkage
302 D:\CProject\log4chelloworld\log4c\appender.c [Warning] 'log4c_appender_print' defined locally after being referenced with dllimport linkage
D:\CProject\log4chelloworld\Makefile.win [Build Error] [log4c/appender.o] Error 1
我没有将Log4c的源程序进行任何编译,只是把它src目录下的一些源程序直接拿过来用的,不知道这样行不行
101 D:\CProject\log4chelloworld\log4c\appender.c initializer element is not constant
101 D:\CProject\log4chelloworld\log4c\appender.c (near initialization for `log4c_appender_factory_ops.fac_new')
102 D:\CProject\log4chelloworld\log4c\appender.c initializer element is not constant
102 D:\CProject\log4chelloworld\log4c\appender.c (near initialization for `log4c_appender_factory_ops.fac_delete')
103 D:\CProject\log4chelloworld\log4c\appender.c initializer element is not constant
103 D:\CProject\log4chelloworld\log4c\appender.c (near initialization for `log4c_appender_factory_ops.fac_print')
At top level:
120 D:\CProject\log4chelloworld\log4c\appender.c [Warning] 'log4c_appender_new' defined locally after being referenced with dllimport linkage
137 D:\CProject\log4chelloworld\log4c\appender.c [Warning] 'log4c_appender_delete' defined locally after being referenced with dllimport linkage
211 D:\CProject\log4chelloworld\log4c\appender.c [Warning] 'log4c_appender_set_udata' defined locally after being referenced with dllimport linkage
280 D:\CProject\log4chelloworld\log4c\appender.c [Warning] 'log4c_appender_close' defined locally after being referenced with dllimport linkage
302 D:\CProject\log4chelloworld\log4c\appender.c [Warning] 'log4c_appender_print' defined locally after being referenced with dllimport linkage
D:\CProject\log4chelloworld\Makefile.win [Build Error] [log4c/appender.o] Error 1
我没有将Log4c的源程序进行任何编译,只是把它src目录下的一些源程序直接拿过来用的,不知道这样行不行
#3
不可以的。
你需要先编译,生成动态或静态库后,再链接使用。
#4
注意看源码包下类似:README或INSTALL或BUILD之类的文档。
#5
可是好像需要在linux环境里面编译,我用cygwin编译后好像又对cygwin有依赖,可以直接在windows环境下编译吗?没搞过C啊,现在临时研究起来头疼啊!
#6
怎么不用logforcplus?
#7
我只想找个可以输出C日志文件的工具就可以了,使用越简单越好,之前看到一个将LOG4C简化成只有一个头文件的,一直也没有实验成功。
#8
MyLog.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef WIN32
#include <windows.h>
#include <io.h>
#else
#include <unistd.h>
#include <sys/time.h>
#include <pthread.h>
#define CRITICAL_SECTION pthread_mutex_t
#define _vsnprintf vsnprintf
#endif
//Log{
#define MAXLOGSIZE 100000000
#define ARRSIZE(x) (sizeof(x)/sizeof(x[0]))
#include <time.h>
#include <stdarg.h>
char logfilename1[]="MyLog1.log";
char logfilename2[]="MyLog2.log";
char logstr[16000];
char datestr[16];
char timestr[16];
char ms10[3];
CRITICAL_SECTION cs_log;
FILE *flog;
int centisec() {
#ifdef WIN32
return ((GetTickCount()%1000L)/10)%100;
#else
struct timeval tv;
if (!gettimeofday(&tv,NULL)) {
return ((tv.tv_usec%1000000L)/10000)%100;
} else {
return 0;
}
#endif
}
#ifdef WIN32
void Lock(CRITICAL_SECTION *l) {
EnterCriticalSection(l);
}
void Unlock(CRITICAL_SECTION *l) {
LeaveCriticalSection(l);
}
#else
void Lock(CRITICAL_SECTION *l) {
pthread_mutex_lock(l);
}
void Unlock(CRITICAL_SECTION *l) {
pthread_mutex_unlock(l);
}
#endif
void LogV(const char *pszFmt,va_list argp) {
struct tm *now;
time_t aclock;
if (NULL==pszFmt||0==pszFmt[0]) return;
if (-1==_vsnprintf(logstr,ARRSIZE(logstr),pszFmt,argp)) logstr[ARRSIZE(logstr)-1]=0;
time(&aclock);
now=localtime(&aclock);
sprintf(datestr,"%04d-%02d-%02d",now->tm_year+1900,now->tm_mon+1,now->tm_mday);
sprintf(timestr,"%02d:%02d:%02d",now->tm_hour ,now->tm_min ,now->tm_sec );
sprintf(ms10,"%02d",centisec());
printf("%s %s.%s %s",datestr,timestr,ms10,logstr);
flog=fopen(logfilename1,"a");
if (NULL!=flog) {
fprintf(flog,"%s %s.%s %s",datestr,timestr,ms10,logstr);
if (ftell(flog)>MAXLOGSIZE) {
fclose(flog);
if (rename(logfilename1,logfilename2)) {
remove(logfilename2);
rename(logfilename1,logfilename2);
}
flog=fopen(logfilename1,"a");
if (NULL==flog) return;
}
fclose(flog);
}
}
void Log(const char *pszFmt,...) {
va_list argp;
Lock(&cs_log);
va_start(argp,pszFmt);
LogV(pszFmt,argp);
va_end(argp);
Unlock(&cs_log);
}
//Log}
int main(int argc,char * argv[]) {
int i;
#ifdef WIN32
InitializeCriticalSection(&cs_log);
#else
pthread_mutex_init(&cs_log,NULL);
#endif
for (i=0;i<10000;i++) {
Log("This is a Log %04d from FILE:%s LINE:%d\n",i, __FILE__, __LINE__);
}
#ifdef WIN32
DeleteCriticalSection(&cs_log);
#else
pthread_mutex_destroy(&cs_log);
#endif
return 0;
}
#9
楼上的大哥是自己写文件实现日志的功能吗?
#10
我在网上下了log4cplus的dll,不知如何使用
#11
你好 有LOG4C的例子吗
谢谢
可以给我现成的例子吗
谢谢
可以给我现成的例子吗