国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Linux > 正文

幾個Windows到Linux的代碼移植問題

2024-08-27 23:57:43
字體:
來源:轉載
供稿:網友

1、在Linux實現Win32 API之GetTickCount函數

為了將Windows中的GetTickCount API函數移植到Linux,可以使用如下的代碼:

  1. long GetTickCount() 
  2. tms tm; 
  3. return times(&tm); 

2、Windows和Linux系統關于itoa的移植問題

大家知道,在將Windows的STL代碼移植到Linux系統時,由于Linux系統中STL沒有實現默認的itoa函數,因此itoa在Linux中無法正常工作。要是在GCC命令行禁用STL的話,那么代碼里就無法使用STL,從而丟失可移植性。這里給出一個簡單可行的解決方法,以便你碰到這種情況時順利進行從Windows到Linux的移植:

  1. #if defined(__linux__) 
  2. #define _itoa   itoa 
  3. char* itoa(int value, char*  str, int radix) 
  4. int  rem = 0; 
  5. int  pos = 0; 
  6. char ch  = ''!'' ; 
  7. do 
  8. rem    = value % radix ; 
  9. value /= radix; 
  10. if ( 16 == radix ) 
  11. if( rem >= 10 && rem <= 15 ) 
  12. switch( rem ) 
  13. case 10: 
  14. ch = ''a'' ; 
  15. break
  16. case 11: 
  17. ch =''b'' ; 
  18. break
  19. case 12: 
  20. ch = ''c'' ; 
  21. break
  22. case 13: 
  23. ch =''d'' ; 
  24. break
  25. case 14: 
  26. ch = ''e'' ; 
  27. break
  28. case 15: 
  29. ch =''f'' ; 
  30. break
  31. if''!'' == ch ) 
  32. str[pos++] = (char) ( rem + 0x30 ); 
  33. else 
  34. str[pos++] = ch ; 
  35. }while( value != 0 ); 
  36. str[pos] = '''' ; 
  37. return strrev(str); 
  38. #endif 

3、Windows到Linux關于__strrev的移植問題

因為在Linux系統中沒有__strrev函數,那么將Windows代碼移植到Linux系統時會有問題,本文下面描述一個技巧,在Linux中提供一個替代__strrev函數的方法。這里提供兩個單獨的實現:一個是普通的char* C函數使用的__strrev標準實現,另一個是針對STL的實現。兩者的輸入和輸出仍然都是char*。

  1. // 
  2. // strrev 標準版 
  3. // 
  4. #if !defined(__linux__) 
  5. #define __strrev strrev 
  6. #endif 
  7. char* strrev(char* szT) 
  8. if ( !szT )                 // 處理傳入的空串. 
  9. return ""
  10. int i = strlen(szT); 
  11. int t = !(i%2)? 1 : 0;      // 檢查串長度. 
  12. for(int j = i-1 , k = 0 ; j > (i/2 -t) ; j-- ) 
  13. char ch  = szT[j]; 
  14. szT[j]   = szT[k]; 
  15. szT[k++] = ch; 
  16. return szT; 
  17. // 
  18. // strrev 針對 STL 的版本. 
  19. // 
  20. char* strrev(char* szT) 
  21. string s(szT); 
  22. reverse(s.begin(), s.end()); 
  23. strncpy(szT, s.c_str(), s.size()); 
  24. szT[s.size()+1] = ''''
  25. return szT; 

4、實現Sleep函數從Windows到Linux的移植

假設你有一些在Windows環境編寫的代碼,你想讓它們在Linux環境下運行,條件是要保持對原有API署名的調用。比如在Windows中有Sleep,而在Linux中對應的函數是usleep,那么如何保持原有的函數名稱調用呢?下面給出一段代碼例子:

  1. void Sleep(unsigned int useconds ) 
  2. // 1 毫秒(milisecond) = 1000 微秒 (microsecond). 
  3. // Windows 的 Sleep 使用毫秒(miliseconds) 
  4. // Linux 的 usleep 使用微秒(microsecond) 
  5. // 由于原來的代碼是在 Windows 中使用的,所以參數要有一個毫秒到微秒的轉換。 
  6. usleep( useconds * 1000 ); 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 嘉义市| 太保市| 邯郸县| 九台市| 长沙市| 荃湾区| 墨竹工卡县| 宿松县| 开阳县| 剑川县| 邻水| 夏邑县| 莱州市| 饶河县| 克拉玛依市| 玉树县| 吴川市| 四平市| 达孜县| 合水县| 巴林左旗| 汶川县| 辉县市| 珠海市| 琼海市| 贞丰县| 广东省| 玉门市| 翁牛特旗| 伊吾县| 永康市| 扎鲁特旗| 新龙县| 开鲁县| 什邡市| 静海县| 建始县| 理塘县| 洛宁县| 大田县| 连城县|