編寫WinSock程序時(shí),如果不包含WinSock2.h文件很多系統(tǒng)類型無(wú)法識(shí)別??墒侨绻薟inSock2.h文件則報(bào)N多系統(tǒng)類型重定義的錯(cuò)誤。
例如 :
mswsock.h(69) : error C2065: 'SOCKET' : undeclared identifier
winsock2.h(99) : error C2011: 'fd_set' : 'struct' type redefinition
多虧了網(wǎng)上諸多網(wǎng)友的帖子給了我提示,問(wèn)題解決了。跪謝了。
Windows網(wǎng)絡(luò)編程至少需要兩個(gè)頭文件:winsock2.h和windows.h,而在WinSock2.0之前還存在一個(gè)老版本的winsock.h。正是這三個(gè)頭文件的包含順序,導(dǎo)致了上述問(wèn)題的出現(xiàn)。
先看看winsock2.h的內(nèi)容,在文件開(kāi)頭有如下宏定義:
#ifndef _WINSOCK2API_
#define _WINSOCK2API_
#define _WINSOCKAPI_ /* Prevent inclusion of winsock.h in windows.h */
_WINSOCK2API_很容易理解,這是最常見(jiàn)的防止頭文件重復(fù)包含的保護(hù)措施。_WINSOCKAPI_的定義則是為了阻止對(duì)老文件winsock.h的包含,即是說(shuō),如果用戶先包含了winsock2.h就不允許再包含winsock.h了,否則會(huì)導(dǎo)致類型重復(fù)定義。這是怎樣做到的呢?很簡(jiǎn)單,因?yàn)閣insock.h的頭部同樣存在如下的保護(hù)措施:
#ifndef _WINSOCKAPI_
#define _WINSOCKAPI_
再回過(guò)頭來(lái)看winsock2.h,在上述內(nèi)容之后緊跟著如下宏指令:
/*
* Pull in WINDOWS.H if necessary
*/
#ifndef _INC_WINDOWS
#include <windows.h>
#endif /* _INC_WINDOWS */
其作用是如果用戶沒(méi)有包含windows.h(_INC_WINDOWS在windows.h中定義)就自動(dòng)包含它,以定義WinSock2.0所需的類型和常量等。
現(xiàn)在切換到windows.h,查找winsock,我們會(huì)驚奇的發(fā)現(xiàn)以下內(nèi)容:
#ifndef WIN32_LEAN_AND_MEAN
#include <cderr.h>
#include <dde.h>
#include <ddeml.h>
#include <dlgs.h>
#ifndef _MAC
#include <lzexpand.h>
#include <mmsystem.h>
#include <nb30.h>
#include <rpc.h>
#endif
#include <shellapi.h>
#ifndef _MAC
#include <winperf.h>
#if(_WIN32_WINNT >= 0x0400)
#include <winsock2.h>
#include <mswsock.h>
#else
#include <winsock.h>
#endif /* _WIN32_WINNT >= 0x0400 */
#endif
// 這里省略掉一部分內(nèi)容
#endif /* WIN32_LEAN_AND_MEAN */
看到?jīng)]?windows.h會(huì)反向包含winsock2.h或者winsock.h!相互間的包含便是萬(wàn)惡之源!
下面具體分析一下問(wèn)題是怎么發(fā)生的。
錯(cuò)誤情形1:我們?cè)谧约旱墓こ讨邢劝瑆insock2.h再包含windows.h,如果WIN32_LEAN_AND_MEAN未定義且_WIN32_WINNT大于或等于0x400,那么windows.h會(huì)在winsock2.h開(kāi)頭被自動(dòng)引入,而windows.h又會(huì)自動(dòng)引入mswsock.h,此時(shí),mswsock.h里所用的socket類型還尚未定義,因此會(huì)出現(xiàn)類型未定義錯(cuò)誤。
錯(cuò)誤情形2:先包含windows.h再包含winsock2.h,如果WIN32_LEAN_AND_MEAN未定義且_WIN32_WINNT未定義或者其版本號(hào)小于0x400,那么windows.h會(huì)自動(dòng)導(dǎo)入舊有的winsock.h,這樣再當(dāng)winsock2.h被包含時(shí)便會(huì)引起重定義。
這里要說(shuō)明的是,宏WIN32_LEAN_AND_MEAN的作用是減小win32頭文件尺寸以加快編譯速度,一般由AppWizard在stdafx.h中自動(dòng)定義。_WIN32_WINNT的作用是開(kāi)啟高版本操作系統(tǒng)下的特殊函數(shù),比如要使用可等待定時(shí)器(WaitableTimer),就得要求_WIN32_WINNT的值大于或等于0x400。因此,如果你沒(méi)有遇到上述兩個(gè)問(wèn)題,很可能是你沒(méi)有在這些條件下進(jìn)行網(wǎng)絡(luò)編程。
問(wèn)題還沒(méi)有結(jié)束,要知道除了VC自帶windows庫(kù)文件外,MS的Platform SDK也含有這些頭文件。我們很可能發(fā)現(xiàn)在之前能夠好好編譯的程序在改變了windows頭文件包含路徑后又出了問(wèn)題。原因很簡(jiǎn)單,Platform SDK中的windows.h與VC自帶的文件存在差異,其相同位置的代碼如下:
#ifndef WIN32_LEAN_AND_MEAN
#include <cderr.h>
#include <dde.h>
#include <ddeml.h>
#include <dlgs.h>
#ifndef _MAC
#include <lzexpand.h>
#include <mmsystem.h>
#include <nb30.h>
#include <rpc.h>
#endif
#include <shellapi.h>
#ifndef _MAC
#include <winperf.h>
#include <winsock.h> // 這里直接包含winsock.h
#endif
#ifndef NOCRYPT
#include <wincrypt.h>
#include <winefs.h>
#include <winscard.h>
#endif
#ifndef NOGDI
#ifndef _MAC
#include <winspool.h>
#ifdef INC_OLE1
#include <ole.h>
#else
#include <ole2.h>
#endif /* !INC_OLE1 */
#endif /* !MAC */
#include <commdlg.h>
#endif /* !NOGDI */
#endif /* WIN32_LEAN_AND_MEAN */
唉,我們不禁要問(wèn)MS為什么要搞這么多花樣,更讓人氣憤的是,既然代碼不一樣,windows.h里卻沒(méi)有任何一個(gè)宏定義能夠幫助程序辨別當(dāng)前使用的文件是VC自帶的還是PSDK里的。
后來(lái),我寫了一個(gè)頭文件專門處理winsock2.h的包含問(wèn)題,名為winsock2i.h,只需在要使用WinSock2.0的源文件里第一個(gè)包含此文件即可,不過(guò)由于前面提到的問(wèn)題,當(dāng)使用PSDK時(shí),需要手工定義一下USING_WIN_PSDK,源碼如下:
//
// winsock2i.h - Include winsock2.h safely.
//
// Copyleft 02/24/2005 by freefalcon
//
//
// When WIN32_LEAN_AND_MEAN is not defined and _WIN32_WINNT is LESS THAN 0x400,
// if we include winsock2.h AFTER windows.h or winsock.h, we get some compiling
// errors as following:
// winsock2.h(99) : error C2011: 'fd_set' : 'struct' type redefinition
//
// When WIN32_LEAN_AND_MEAN is not defined and _WIN32_WINNT is NOT LESS THAN 0x400,
// if we include winsock2.h BEFORE windows.h, we get some other compiling errors:
// mswsock.h(69) : error C2065: 'SOCKET' : undeclared identifier
//
// So, this file is used to help us to include winsock2.h safely, it should be
// placed before any other header files.
//
#ifndef _WINSOCK2API_
// Prevent inclusion of winsock.h
#ifdef _WINSOCKAPI_
#error Header winsock.h is included unexpectedly.
#endif
// NOTE: If you use Windows Platform SDK, you should enable following definition:
// #define USING_WIN_PSDK
#if !defined(WIN32_LEAN_AND_MEAN) && (_WIN32_WINNT >= 0x0400) && !defined(USING_WIN_PSDK)
#include <windows.h>
#else
#include <winsock2.