about porting

http://www.diybl.com/course/3_program/c/c_js/20100710/426527.html

http://www.cppblog.com/lyricc/archive/2012/01/12/164071.aspx

搞了幾個跨Linux與Windows的項目,碰到很多問題,記下來,以供查考。另外,因爲水平的原因,肯定錯誤在所難免,請讀者一定指正。

  如無其它說明,本文所指Linux均表示2.6內核Linux,GCC編譯器,Windows均表示Windows XP系統,Visual Studio 2005 sp1編譯環境。

  下面大概分幾個方面進行羅列:

socket

  Linux要包含

#include <sys/socket.h> 
#include <netinet/in.h> 
#include <netdb.h> 
#include <arpa/inet.h> 
等頭文件,而windows下則是包含 #include <winsock.h> 

  Linux中socket爲整形,Windows中爲一個SOCKET。

  Linux中關閉socket爲close,Windows中爲closesocket。

  Linux中有變量socklen_t,Windows中直接爲int。

  因爲linux中的socket與普通的fd一樣,所以可以在TCP的socket中,發送與接收數據時,直接使用read和write。而windows只能使用recv和send。

  設置socet選項,比如設置socket爲非阻塞的。Linux下爲

flag = fcntl (fd, F_GETFL); 
fcntl (fd, F_SETFL, flag | O_NONBLOCK); 
,Windows下爲 flag = 1; 
ioctlsocket (fd, FIONBIO, (unsigned long *) &flag); 

  當非阻塞socket的TCP連接正在進行時,Linux的錯誤號爲EINPROGRESS,Windows的錯誤號爲WSAEWOULDBLOCK。

file

  Linux下面,文件換行是"\n",而windows下面是"\r\n"。

  Linux下面,目錄分隔符是"/",而windows下面是"\"。

  Linux與Windows下面,均可以使用stat調用來查詢文件信息。但是,Linux只支持2G大小,而Windows只支持4G大小。爲了支持更大的文件查詢,可以在Linux環境下加_FILE_OFFSET_BITS=64定義,在Windows下面使用_stat64調用,入參爲 struct __stat64。

  Linux中可根據stat的st_mode判斷文件類型,有S_ISREG、S_ISDIR等宏。Windows中沒有,需要自己定義相應的宏,如

#define S_ISREG(m) (((m) & 0170000) == (0100000)) 
#define S_ISDIR(m) (((m) & 0170000) == (0040000)) 

  Linux中刪除文件是unlink,Windows中爲DeleteFile。

time

  Linux中,time_t結構是32位的無符號整形。而windows中,time_t結構是64位的整形。如果要在windows始time_t爲32位無符號整形,可以加宏定義,_USE_32BIT_TIME_T。

  Linux中,sleep的單位爲秒。Windows中,Sleep的單位爲毫秒。即,Linux下sleep (1),在Windows環境下則需要Sleep (1000)。

  Windows中的timecmp宏,不支持大於等於或者小於等於。

  Windows中沒有struct timeval結構的加減宏可以使用,需要手動定義:

#define MICROSECONDS (1000 * 1000) 

#define timeradd(t1, t2, t3) do { \ 
(t3)->tv_sec = (t1)->tv_sec + (t2)->tv_sec; \ 
(t3)->tv_usec = (t1)->tv_usec + (t2)->tv_usec % MICROSECONDS; \ 
if ((t1)->tv_usec + (t2)->tv_usec > MICROSECONDS) (t3)->tv_sec ++; \ 
} while (0) 

#define timersub(t1, t2, t3) do { \ 
(t3)->tv_sec = (t1)->tv_sec - (t2)->tv_sec; \ 
(t3)->tv_usec = (t1)->tv_usec - (t2)->tv_usec; \ 
if ((t1)->tv_usec - (t2)->tv_usec < 0) (t3)->tv_usec --, (t3)->tv_usec += MICROSECONDS; \ 
} while (0) 

process

  Linux下可以直接使用system來調用外部程序。Windows最好使用WinExec,因爲WinExec可以支持是打開還是隱藏程序窗口。用WinExec的第二個入參指明,如SW_SHOW/SW_HIDE。

雜項

  Linux爲srandom和random函數,Windows爲srand和rand函數。

  Linux爲snprintf,Windows爲_snprintf。

  同理,Linux中的strcasecmp,Windows爲_stricmp。

錯誤處理

  Linux下面,通常使用全局變量errno來表示函數執行的錯誤號。Windows下要使用GetLastError ()調用來取得。

Linux環境下僅有的

  這些函數或者宏,Windows中完全沒有,需要用戶手動實現。

  atoll

long long 
atoll (const char *p) 

int minus = 0; 
long long value = 0; 
if (*p == '-') 

minus ++; 
p ++; 

while (*p >= '0' && *p <= '9') 

value *= 10; 
value += *p - '0'; 
p ++; 

return minus ? 0 - value : value; 

gettimeofday

#if defined(_MSC_VER) || defined(_MSC_EXTENSIONS) 
#define EPOCHFILETIME 11644473600000000Ui64 
#else 
#define EPOCHFILETIME 11644473600000000ULL 
#endif 

struct timezone 

int tz_minuteswest; 
int tz_dsttime; 
}; 

int 
gettimeofday (struct timeval *tv, struct timezone *tz) 

FILETIME ft; 
LARGE_INTEGER li; 
__int64 t; 
static int tzflag; 

if (tv) 

GetSystemTimeAsFileTime (&ft); 
li.LowPart = ft.dwLowDateTime; 
li.HighPart = ft.dwHighDateTime; 
t = li.QuadPart; /* In 100-nanosecond intervals */ 
t -= EPOCHFILETIME; /* Offset to the Epoch time */ 
t /= 10; /* In microseconds */ 
tv->tv_sec = (long) (t / 1000000); 
tv->tv_usec = (long) (t % 1000000); 


if (tz) 

if (!tzflag) 

_tzset (); 
tzflag++; 

tz->tz_minuteswest = _timezone / 60; 
tz->tz_dsttime = _daylight; 


return 0; 
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章