Analysis on redefinition solutions in Winsock. h and winsock2.h

Source: Internet
Author: User
Windows. H and winsock2.h contain sequence. Generally, when using C ++ for network development on the Windows platform, these two header files are included at the same time. If the sequence is improper (windows. h prior to winsock2.h. For example:

Warning 4 warning c4005: "af_ipx": Macro redefinition C:/program files/Microsoft sdks/Windows/v6.0a/include/ws2def. h 91
Warning 5 Warning c4005: "af_max": Macro redefinition C:/program files/Microsoft sdks/Windows/v6.0a/include/ws2def. h 127
Warning 6 warning c4005: "so_dontlinger": Macro redefinition C:/program files/Microsoft sdks/Windows/v6.0a/include/ws2def. h 163
Error 7 Error c2011: "sockaddr": "struct" type redefinition C:/program files/Microsoft sdks/Windows/v6.0a/include/ws2def. h 206
Error 8 Error c2143: syntax error: Missing "}" (before "constant") C:/program files/Microsoft sdks/Windows/v6.0a/include/ws2def. h 384
Error 9 error c2143: syntax error: ";" missing (before "constant") C:/program files/Microsoft sdks/Windows/v6.0a/include/ws2def. h 384
Error 10 error c2059: syntax error: "constant" C:/program files/Microsoft sdks/Windows/v6.0a/include/ws2def. h 384
Error 11 error c2143: syntax error: ";" missing (before "}") C:/program files/Microsoft sdks/Windows/v6.0a/include/ws2def. h 437
Error 12 error c4430: The type specifier is missing-it is assumed to be Int. Note: C ++ does not support the default int C:/program files/Microsoft sdks/Windows/v6.0a/include/ws2def. h 437
Error 13 Error c4430: The type specifier is missing-it is assumed to be Int. Note: C ++ does not support the default int C:/program files/Microsoft sdks/Windows/v6.0a/include/ws2def. h 437
Warning 14 warning c4005: "in_classa": Macro redefinition C:/program files/Microsoft sdks/Windows/v6.0a/include/ws2def. h 518
Warning 15 warning c4005: "in_classb": Macro redefinition C:/program files/Microsoft sdks/Windows/v6.0a/include/ws2def. h 524
Warning 16 warning c4005: "in_classc": Macro redefinition C:/program files/Microsoft sdks/Windows/v6.0a/include/ws2def. h 530
......
Error 133 fatal error c1003: error count exceeds 100; stopping compilation F:/Yang Fan/courses/Works/C ++/mylamebt/btendpoint. h 267

At first we saw a bunch of errors, but we only needed to change the order of the two to solve the problem. For the reason, see the link below. In addition, these problems not only affect files that directly contain the two, but also affect indirect inclusion. For example,. h contains windows. h, B. h contains winsock2.h. h to reference. H and B. h, then the correct order should be B. h prior to. h. Of course, in practice, it is sometimes difficult to find out which two files are in wrong order. The ultimate solution is that in the current project (that is, the project that cannot be compiled) # include <winsock2.h> and # include <windows. h> the world is quiet.

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

To prevent link failure, we would like to record the following:

Analysis on redefinition solutions in Winsock. h and winsock2.h

Problem description: there is no problem when using socket-related functions in VC 6.0, but there are similar errors in. 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 'sockadd'
[C ++ Error] Winsock. H (492): e2344 earlier declaration of 'sockadd'
[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 internal error or warning messages

Solution:

This problem arises becauseWindows. h(At least, that version of it) provided des notWinsock2.hButWinsock. h; Sadly when Microsoft wroteWinsock2.hThey chose neither to changeWindows. hTo includeWinsock2.h, Which replacesWinsock. h, Nor to includeWindows. hFromWinsock2.hAnd 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 makingWinsock2.hStandalone ).

The fixIs thankfully simple: Always"# Include <winsock2.h>"Before Windows. h.

However, you must remember that ifWindows. hHas been encoded ded 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 becauseYouIncludeTheirHeader files, andTheirHeader files includeWindows. h-You can just includeWinsock2.hBefore you include the Library's header files.

Sometimes I encounter this problem when jrtplib is included, and the solution is the same. In a word, the # include <winsock2.h> problem can be solved before # include <windows. h>.

Problem description]
In projects that contain <windows. h> and <winsock2.h>
The following error occurs:
Error c2011: 'fd _ set': 'struct 'type redefinition
Error c2011: 'timeval': 'struct 'type redefinition
....
Error c2375: 'accept': redefinition; different linkage
[Cause Analysis]
The main reason is that <windows. h> contains the <Winsock. h> header file.
The preceding error occurs because of the differences in the current version. <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
[Solution]
The code above shows that if the windows 32 _ lean_and_mean macro is not defined
Windows. h may contain the Winsock. h header file.
The solution is to define the win32_lean_and_mean macro before including <windows. h>, as shown in figure
As shown below:
# Define win32_lean_and_mean
# Include <windows. h>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.