添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement . We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I think winsock2.h has to be included before windows.h.
Ideally upstream would fix this..
Maybe this could even be fixed in windows.h itself. ;)

mysql/mysql.h:

#ifdef _WIN32
#include <windows.h>
#ifdef WIN32_LEAN_AND_MEAN
#include <winsock2.h>
#endif
#define my_socket SOCKET
          

I did a brief search and I don't see it in the docs [1]. In fact, on the page they even do exactly what you've quoted in a sample:

#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <iphlpapi.h>
#include <stdio.h>
#pragma comment(lib, "Ws2_32.lib")
int main() {
  return 0;

Let me know if I missed something.

[1] https://msdn.microsoft.com/en-us/library/windows/desktop/ms737629(v=vs.85).aspx

Right, it appears that mysql.h is written to handle either the old WinSock 1 headers (pulled in through windows.h) or the new WinSock2 headers. Because they also need to pull in windows.h, they can't really delay the choice. I suspect they're just using some network structs that are the same in both headers, so it probably isn't a correctness issue.

In order to use both of them (or, really, mysql + any other networking library), you will almost certainly need to #define WIN32_LEAN_AND_MEAN at the top of your cpp file, before including mysql.h, which will tell MySQL to use WinSock2. However, we can't always define this for you, because it's quite possible that someone would want the alternate behavior (WinSock 1).

Isn't WS2 a superset of WS1?
If not, you have a problem if some code uses WS1 and other code uses WS2.
If it is, including winsock2.h before windows.h in mysql.h would solve the problem wouldn't it?

I'm certainly not confident enough to say that WS2 is a strict superset of WS1 (I'm more confident that they aren't!). What I would guess is more likely is that they both build on a small common core, containing constants and structures that are immutable features of networking (for example, the IANA protocol assigments for IP [1] or endian-manipulation routines). I would guess that in this case, mysql.h is only using that small common core in its headers and therefore can be agnostic and support your application using either of the winsock versions.

As for within a single process, I do not know for certain that it is safe to use both WS1 and WS2, however my default guess would be that it is -- assuming that you have properly managed it and aren't accidentally mixing incompatible structures.

In this case, I think it is best to err on the side of assuming the authors of MySQL have made a sound engineering decision to maintain that dual-mode nature and that they haven't made the situation completely unusable -- at least without much stronger evidence to the contrary than I currently have.

The true fix for this problem, then, is to add the macro WIN32_LEAN_AND_MEAN to your project-wide preprocessor defines. This will ensure that everyone pulls in WinSock 2 and should fix all of the above errors.

[1] http://www.iana.org/assignments/protocol-numbers/protocol-numbers.xhtml