轉自:https://blog.csdn.net/dj0379/article/details/53577135
linux編程與windows編程的差異之處:
1. 文件與目錄的大小寫以及路徑分隔符的差別
windows下不區分大小寫,路徑分隔符一般使用"/";linux下區分大小寫,路徑分隔符使用"/"。
2. itoa()函數在linux下並不存在
所以使用類似sprintf();之類的函數是個不錯的替代。(為什么說是類似函數,看下面一條啦)
3. _snprintf()與_vsnprintf()的差別
sprintf()不能檢查目標字符串的長度,可能造成眾多安全問題,所以都會推薦使用snprintf()。
int _snprintf( char * buffer, size_t count , const char * format [ , argument] . . . ) ;
例如,要把c2字符串write給c1,比較安全的用法:
char c1[ 256] ; c1[ sizeof ( c1) - 1] = 0; _snprintf( c1, sizeof ( c1) , "%s" , c2) ; if ( c1[ sizeof ( c1) - 1] ! = 0) { cout < < "warning: string is truncated" ) < < endl ; c1[ sizeof ( c1) - 1] = 0; }
如果在write之前主動檢查一下c2的長度再申請c1的空間當然是最好的。
4. 在linux下並不存在std::exception(char*),可以使用logic_error類型代替exception
5. 盡量使用STL與標准C庫,並且要按照標准C++語法來寫程序
比如這種用法list< type1<T0> >::iterator iter;在windows下的編譯器下沒有編譯錯誤;但在linux下,就需要在前面增加typename才能順利編譯通過。
6. 在源文件最后多加一個或幾行的空行,以免蹦出警告:"no newline at end of file"
7. windows中對ini文件進行操作的所有函數在linux下都沒有對應函數
建議自己寫一個操作ini文件的類,在STLChina上有一個使用stl制作這種類的原理介紹和一小段代碼片段,自己擴充一下。
8. SOCKET對象集要使用fd_set,不要使用FD_SET
9. stricmp()函數,在linux下用strcasecmp()函數替換
10. 在linux下不存在INVALID_SOCKET、INVALID_HANDLE_VALUE、SOCKET_ERROR、MAX_PATH、INFINITE、STILL_ACTIVE等宏定義
11. 參數要求為char*的時候別忘了給傳進去的string加一個.c_str()函數
12. linux下不存在目錄操作時要包含的direct.h與io.h頭文件,而要包含相應的unistd.h與fcntl.h頭文件
13. filelength()函數在linux也不存在,可以通過fstat()函數來替代
使用文件與目錄操作函數,不要使用類似_access帶下划線的函數,取而代之有access函數可以通用。
14. #prgram once預編譯指令在linux下並不起作用,請使用以下標准方式來解決頭文件多次包含問題 :
# ifndef _XXXXX_H_
# define _XXXXX_H_
# endif
15. hash_map出現在后期版本的stl庫,為兼顧hasp與tree map,請可以通過以下宏來解決 :
// process hash_map # if defined( _MSC_VER) # if _MSC_VER > = 1300 // hasp_map not standard Container # pragma warning( disable: 4996) # include < hash_map> // we believe _STLP_STRING included means using STLPORT # elif defined( _STLP_STRING) # include < hash_map> # else # define hash_map map # endif # endif
16. 為了區別不同OS,一般可用的幾個宏有:WIN32,__linux__,需要使用與OS相關的內容,請一定用開關來寫
17. 對於界面操作部分代碼,一定要把它與程序邏輯核心代碼用宏分開來,這樣的代碼才好移植,不要交錯
18. 不要假定可執行文件一定有如.exe的擴展名
19. 在父類中用typedef定義的類型,在子類並不能直接使用
20. long類型的變量在32位和64位Windows上都是4個字節,而在64位Linux系統上占8字節。
另附:
linux網絡編程常用頭文件:
sys/types.h:數據類型定義
sys/socket.h:提供socket函數及數據結構
netinet/in.h:定義數據結構sockaddr_in
arpa/inet.h:提供IP地址轉換函數
netdb.h:提供設置及獲取域名的函數
sys/ioctl.h:提供對I/O控制的函數
sys/poll.h:提供socket等待測試機制的函數
其他在網絡程序中常見的頭文件
unistd.h:提供通用的文件、目錄、程序及進程操作的函數
errno.h:提供錯誤號errno的定義,用於錯誤處理
fcntl.h:提供對文件控制的函數
time.h:提供有關時間的函數
crypt.h:提供使用DES加密算法的加密函數
pwd.h:提供對/etc/passwd文件訪問的函數
shadow.h:提供對/etc/shadow文件訪問的函數
pthread.h:提供多線程操作的函數
signal.h:提供對信號操作的函數
sys/wait.h、sys/ipc.h、sys/shm.h:提供進程等待、進程間通訊(IPC)及共享內存的函數
在編寫網絡程序時,可以直接使用下面這段頭文件代碼
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <malloc.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <stdarg.h>
#include <fcntl.h>
#include <fcntl.h>
涉及到用戶權限及密碼驗證問題時加入如下語句:
#include <shadow.h>
#include <crypt.h>
#include <pwd.h>
需要注意的是,應該在編譯時鏈接加密算法庫,即增加編譯選項:
-lcrypt
涉及到文件及時間操作加入如下語句:
#include <sys/time.h>
#include <utime.h>
#include <time.h>
#include <sys/stat.h>
#include <sys/file.h>
涉及到多進程操作時加入如下語句:
#include <sys/wait.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <signal.h>
涉及到多線程操作時加入如下語句:
#include <pthread.h>
#include <sys/poll.h>
需要注意的是,應該在編譯時鏈接線程庫,即增加編譯選項:-lthread