跨平台Linux與Windows的一點小結

來源:互聯網
上載者:User

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

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

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

socket

  Linux要包含

[cpp] 
  1. #include <sys/socket.h>   
  2. #include <netinet/in.h>   
  3. #include <netdb.h>   
  4. #include <arpa/inet.h>  

等標頭檔,而windows下則是包含

[cpp] 
  1. #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下為

[cpp] 
  1. flag = fcntl (fd, F_GETFL);  
  2. fcntl (fd, F_SETFL, flag | O_NONBLOCK);  

,Windows下為

[cpp] 
  1. flag = 1;  
  2. 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中沒有,需要自己定義相應的宏,如

[cpp] 
  1. #define S_ISREG(m) (((m) & 0170000) == (0100000))   
  2. #define S_ISDIR(m) (((m) & 0170000) == (0040000))  

  Linux中刪除檔案是unlink,Windows中為DeleteFile。

time

  Linux中,time_t結構是長整形。而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結構的加減宏可以使用,需要手動定義:

[cpp] 
  1. #define MICROSECONDS (1000 * 1000)   
  2.   
  3. #define timeradd(t1, t2, t3) do {                                                          \   
  4.   (t3)->tv_sec = (t1)->tv_sec + (t2)->tv_sec;                                              \  
  5.   (t3)->tv_usec = (t1)->tv_usec + (t2)->tv_usec % MICROSECONDS;                            \  
  6.   if ((t1)->tv_usec + (t2)->tv_usec > MICROSECONDS) (t3)->tv_sec ++;                       \  
  7. } while (0)  
  8.   
  9. #define timersub(t1, t2, t3) do {                                                          \   
  10.   (t3)->tv_sec = (t1)->tv_sec - (t2)->tv_sec;                                              \  
  11.   (t3)->tv_usec = (t1)->tv_usec - (t2)->tv_usec;                                           \  
  12.   if ((t1)->tv_usec - (t2)->tv_usec < 0) (t3)->tv_usec --, (t3)->tv_usec += MICROSECONDS;  \  
  13. } while (0)  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.