關於WINSOCK.H與winsock2.h中的重定義解決辦法分析

來源:互聯網
上載者:User
windows.h與winsock2.h的包含順序 大凡在Windows平台下用C++做網路開發很多時候都會同時包含這兩個標頭檔,如若順序不當(windows.h先於winsock2.h)就會出現很多莫名其妙的錯誤。諸如:

警告    4    warning C4005: “AF_IPX”: 宏重定義    c:/program files/microsoft sdks/windows/v6.0a/include/ws2def.h    91
警告    5    warning C4005: “AF_MAX”: 宏重定義    c:/program files/microsoft sdks/windows/v6.0a/include/ws2def.h    127
警告    6    warning C4005: “SO_DONTLINGER”: 宏重定義    c:/program files/microsoft sdks/windows/v6.0a/include/ws2def.h    163
錯誤    7    error C2011: “sockaddr”: “struct”類型重定義    c:/program files/microsoft sdks/windows/v6.0a/include/ws2def.h    206
錯誤    8    error C2143: 語法錯誤 : 缺少“}”(在“常量”的前面)    c:/program files/microsoft sdks/windows/v6.0a/include/ws2def.h    384
錯誤    9    error C2143: 語法錯誤 : 缺少“;”(在“常量”的前面)    c:/program files/microsoft sdks/windows/v6.0a/include/ws2def.h    384
錯誤    10    error C2059: 語法錯誤 : “常量”    c:/program files/microsoft sdks/windows/v6.0a/include/ws2def.h    384
錯誤    11    error C2143: 語法錯誤 : 缺少“;”(在“}”的前面)    c:/program files/microsoft sdks/windows/v6.0a/include/ws2def.h    437
錯誤    12    error C4430: 缺少類型說明符 - 假定為 int。注意: C++ 不支援預設 int    c:/program files/microsoft sdks/windows/v6.0a/include/ws2def.h    437
錯誤    13    error C4430: 缺少類型說明符 - 假定為 int。注意: C++ 不支援預設 int    c:/program files/microsoft sdks/windows/v6.0a/include/ws2def.h    437
警告    14    warning C4005: “IN_CLASSA”: 宏重定義    c:/program files/microsoft sdks/windows/v6.0a/include/ws2def.h    518
警告    15    warning C4005: “IN_CLASSB”: 宏重定義    c:/program files/microsoft sdks/windows/v6.0a/include/ws2def.h    524
警告    16    warning C4005: “IN_CLASSC”: 宏重定義    c:/program files/microsoft sdks/windows/v6.0a/include/ws2def.h    530
……
錯誤    133    fatal error C1003: 錯誤計數超過 100;正在停止編譯    f:/yang fan/courses/works/c++/mylamebt/btendpoint/btendpoint.h    267

初看到如此一堆的錯誤委實不爽,但是只要將二者的包含順序調換一下問題就會解決,原因參見下面那個連結。另外,上述問題不僅影響直接包含二者的檔案,還影響間接包含的情形。比如,a.h包含了windows.h,b.h包含了winsock2.h,如果在c.h當中要引用a.h和b.h,那麼正確的順序應當是b.h先於a.h。當然,實踐當中有時很難找到究竟是哪兩個檔案順序不對了,終極的解決辦法是,在當前工程(就是編譯不過的這個工程)所有include語句最前面加上#include <winsock2.h>和#include<windows.h>,世界清靜了。

http://blog.chinaunix.net/u2/64540/showart_689402.html

為防止連結失效,敬錄如下:

關於WINSOCK.H與winsock2.h中的重定義解決辦法分析

問題描述:在 VC 6.0中使用socket相關的函數時沒有什麼問題,可是到了.net下就有以下類似的錯誤,

[C++ Error] winsock2.h(109): E2238 Multiple declaration for 'fd_set'
[C++ Error] winsock.h(54): E2344 Earlier declaration of 'fd_set'
[C++ Error] winsock2.h(112): E2146 Need an identifier to declare
[C++ Warning] winsock2.h(144): W8017 Redefinition of 'FD_SET' is not identical
[C++ Error] winsock2.h(153): E2238 Multiple declaration for 'timeval'
[C++ Error] winsock.h(97): E2344 Earlier declaration of 'timeval'
[C++ Error] winsock2.h(209): E2238 Multiple declaration for 'hostent'
[C++ Error] winsock.h(153): E2344 Earlier declaration of 'hostent'
[C++ Error] winsock2.h(222): E2238 Multiple declaration for 'netent'
[C++ Error] winsock.h(166): E2344 Earlier declaration of 'netent'
[C++ Error] winsock2.h(229): E2238 Multiple declaration for 'servent'
[C++ Error] winsock.h(173): E2344 Earlier declaration of 'servent'
[C++ Error] winsock2.h(241): E2238 Multiple declaration for 'protoent'
[C++ Error] winsock.h(185): E2344 Earlier declaration of 'protoent'
[C++ Error] winsock2.h(327): E2238 Multiple declaration for 'in_addr'
[C++ Error] winsock.h(269): E2344 Earlier declaration of 'in_addr'
[C++ Error] winsock2.h(385): E2238 Multiple declaration for 'sockaddr_in'
[C++ Error] winsock.h(319): E2344 Earlier declaration of 'sockaddr_in'
[C++ Error] winsock2.h(395): E2238 Multiple declaration for 'WSAData'
[C++ Error] winsock.h(329): E2344 Earlier declaration of 'WSAData'
[C++ Error] winsock2.h(411): E2146 Need an identifier to declare
[C++ Warning] winsock2.h(455): W8017 Redefinition of 'SO_DONTLINGER' is not identical
[C++ Warning] winsock2.h(512): W8017 Redefinition of 'AF_IPX' is not identical
[C++ Warning] winsock2.h(540): W8017 Redefinition of 'AF_MAX' is not identical
[C++ Error] winsock2.h(546): E2238 Multiple declaration for 'sockaddr'
[C++ Error] winsock.h(492): E2344 Earlier declaration of 'sockaddr'
[C++ Error] winsock2.h(586): E2238 Multiple declaration for 'sockproto'
[C++ Error] winsock.h(501): E2344 Earlier declaration of 'sockproto'
[C++ Error] winsock2.h(625): E2238 Multiple declaration for 'linger'
[C++ Error] winsock2.h(625): E2228 Too many error or warning messages

Solution:

This problem arises because windows.h (at least, that version of it) includes not winsock2.h but winsock.h; sadly when Microsoft wrote winsock2.h they chose neither to change windows.h to include winsock2.h, which replaces winsock.h, nor to include windows.h from winsock2.h and then add the definitions for the new Winsock 2 API methods & structures (this might seem reasonable since Winsock 2 does, strictly speaking, replace Winsock 1, but since the API must be fully backwards-compatible the distinction is somewhat meaningless and there's no real benefit to making winsock2.h standalone).

The fix is thankfully simple: always "#include <winsock2.h>" before windows.h.

However, you must remember that if windows.h has been included by (for example) a higher-level header file that is subsequently including your header file, it's too late - so you must make sure that the higher-level header files respect this convention also.

It is however rarely necessary to modify the header files of libraries or other code modules you are using just because you include their header files, and their header files include windows.h - you can just include winsock2.h before you include the library's header files.

在包含jrtplib有時候我也遇到這個問題,解決方案與之相同。一句話,在#include<windows.h>之前 #include <winsock2.h> 問題就可以解決。

問題描述]
   在包含了<windows.h>以及<winsock2.h>的工程中,編譯有時會出現如
下錯誤:
     error C2011: 'fd_set' : 'struct' type redefinition
     error C2011: 'timeval' : 'struct' type redefinition
                     ....
     error C2375: 'accept' : redefinition; different linkage
[原因分析]
   主要原因是因為<windows.h>中包含了<winsock.h>標頭檔,由於其版
本的不同,導致出現上述的錯誤。<windows.h>中相關代碼如下:
               #ifndef WIN32_LEAN_AND_MEAN
               #include <cderr.h>
               #include <dde.h>
               #include <ddeml.h>
               ........
                #ifndef _MAC
               #include <winperf.h>
               #include <winsock.h>
               #endif
                .......
               #include <commdlg.h>
               #endif
               #endif
[解決方案]
    由以上代碼可以看出如果在沒有定義WIN32_LEAN_AND_MEAN宏的大前
提下windows.h有可能包含winsock.h 標頭檔,因此我們得出一個很簡單
的解決方案就是在包含<windows.h>之前定義WIN32_LEAN_AND_MEAN宏,如
下所示:
#define WIN32_LEAN_AND_MEAN
#include <windows.h>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.