Problems caused by the inclusion sequence of windows. h and winsock2.h

Source: Internet
Author: User

Environment:

Windows, C ++

Error description:

Socket and thread used in the project, corresponding to include header files # include <winsock2.h> and # include <windows. h>

However, many errors are encountered during compilation, almost all of which are errors such as "type/link redefinition" and "unable to find identifiers", and almost all occur in winsock2.h and ws2def. h;

For example:

Error c2011: "sockaddr_in": "struct" type redefinition
Ws2def. h

Error c2375: "accept": redefinition; different links winsock2.h

Error c3861: "socket": unable to find the identifier server. cpp

Error cause:

Header files are often included in network development.

# Include <winsock2.h>

# Include <windows. h>

If the order is incorrect (Windows. H is prior to winsock2.h), many inexplicable errors may occur.

The reason is: Windows. h contains Winsock. h header file, while a lot of content in winsock2.h will be related to Winsock. the content in H is the same, resulting in type redefinition (winsock2.h is Winsock. later versions of H)

Solution:

If the macro win32_lean_and_mean is not specified, windows. h contains Winsock. h.

Therefore, the solution is as follows:

Define the win32_lean_and_mean macro before including <windows. h>, as shown in
As shown below:
# Define win32_lean_and_mean
# Include <windows. h>

Supplement:

Windows. h contains the following content:

# 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
# Ifndef nocrypt
# Include <wincrypt. h>
# Endif

# Ifndef nogdi
# Include <commdlg. h>
# Ifndef _ Mac
# Include <winspool. h>
# Ifdef inc_ole1
# Include <Ole. h>
# Else
# Include <ole2.h>
# Endif /*! Inc_ole1 */
# Endif /*! Mac */
# Endif /*! Nogdi */
# Endif/* win32_lean_and_mean */

Related Article

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.