window下c程序移植到linux g 下,Windows平台下的C项目怎么向Linux下移植

C/C++ code#include

#include

#include

#ifdef WIN32

#include

#include

#else

#include

#include

#include

#define CRITICAL_SECTION pthread_mutex_t

#define _vsnprintf vsnprintf

#endif

//Log{

#define MAXLOGSIZE 100000000

#define ARRSIZE(x) (sizeof(x)/sizeof(x[0]))

#include

#include

#include

char logfilename1[]="MyLog1.log";

char logfilename2[]="MyLog2.log";

char logstr[16000];

char datestr[16];

char timestr[16];

char mss[4];

CRITICAL_SECTION cs_log;

FILE *flog;

#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;

struct timeb tb;

if (NULL==pszFmt||0==pszFmt[0]) return;

if (-1==_vsnprintf(logstr,ARRSIZE(logstr),pszFmt,argp)) logstr[ARRSIZE(logstr)-1]=0;

ftime(&tb);

now=localtime(&tb.time);

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(mss,"%03d",tb.millitm);

printf("%s %s.%s %s",datestr,timestr,mss,logstr);

flog=fopen(logfilename1,"a");

if (NULL!=flog) {

fprintf(flog,"%s %s.%s %s",datestr,timestr,mss,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;

}

------解决方案--------------------

将socket稍微封装一下就可以了,socket基本形似,STL基本不需要修改的。

------解决方案--------------------探讨

感谢楼上的各位

我现在已经做了一部分区别平台的定义,例如在WIN32下使用Sleep(1000)来等待1秒,Linux下使用sleep(1)等等

但是我还是不是很清楚,我怎样判断WIN32下的1个函数,是否可以在Linux下work呢,以及如果要在Linux下work,需要引用哪些头文件?

比如说,,,

------解决方案--------------------

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值