Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
612 views
in Technique[技术] by (71.8m points)

c - Cannot include both files (WinSock2, Windows.h)

I'm having a problem including both files. Now, I know I need to either include Winsock2 first, then windows.h, or simply put:

#define WIN32_LEAN_AND_MEAN

but, I'm still having problems

I have a header file that is called XS.h which looks like this

#ifndef XS_H
#define XS_H

#include <winsock2.h>
#include <ws2tcpip.h>
#include <Windows.h>

#endif

and I'm including XS.h in the header Client.h. Client.h include is looks like this :

#ifndef CLIENT_H
#define CLIENT_H

#include "XS.h"

XS.h is my only include in Client.h, yet I still get errors (and as you can see, Winsockis included before windows.h

I'm getting about 78 errors, here are some of them :

Error   90  error C3861: 'WSASetLastError': identifier not found    c:program files (x86)windows kits8.0includeumws2tcpip.h   703
Error   61  error C2375: 'WSAStartup' : redefinition; different linkage c:program files (x86)windows kits8.0includeumwinsock2.h   2296
Error   49  error C2375: 'send' : redefinition; different linkage   c:program files (x86)windows kits8.0includeumwinsock2.h   2026

How can I solve this issue?

Thanks!

Edit: I've tried to use #define _WINSOCKAPI_ as well, though it did not resolve my problems... I have winsock.h first, then windows.h, though it still does the error for me.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Make sure that <windows.h> doesn't include <winsock.h> (which provides many of the same declarations as <winsock2.h>). In the <winsock2.h> file on my system there is this line:

#define _WINSOCKAPI_   /* Prevent inclusion of winsock.h in windows.h */

The _WINSOCKAPI_ include guard may be an internal implementation detail, but as a practical solution I would rely on it, just defining this symbol before including <windows.h>, e.g. in the compiler invocation (which for an IDE means in the IDE project settings).

Alternatively you can try to always include <winsock2.h> before <windows.h>, in order to establish the relevant include guard whatever it is (but this is I think much more fragile than just assuming that the above guard is practically well-defined);

or you can define WIN32_LEAN_AND_MEAN, which prevents <windows.h> from including <winsock.h> but also some other headers (listing from source on my system those are <cderr.h>, <dde.h>, <ddeml.h>, <dlgs.h>, <lzexpand.h>, <mmsystem.h>, <nb30.h>, <rpc.h>, <shellapi.h>, <winperf.h>, <wincrypt.h>, <winefs.h>, <winscard.h>, <winspool.h>, <ole2.h>, and <commdlg.h>). I do not recommend relying on WIN32_LEAN_AND_MEAN optimization for correctness.

I.e., minimum:

#undef UNICODE
#define UNICODE
#undef _WINSOCKAPI_
#define _WINSOCKAPI_
#include <windows.h>
#include <winsock2.h>

auto main()
    -> int
{}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...