-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
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
Winsock2.h problem and fix #387
Comments
@nkindt, thanks for the feedback. I couldn't reproduce it. Here is what I did. a.cc#include "./httplib.h"
int main(void) {
httplib::Client cli("localhost", 8080);
cli.Get("/hi");
return 0;
}
Do I miss something to reproduce the problem? Thanks. |
If you include Windows.h before httplib.h then it should be repro. If you get those errors then a #define WIN32_LEAN_AND_MEAN before Windows.h should fix it. The fix is to define WIN32_LEAN_AND_MEAN or reorder those 2 includes. Reordering isn't always a solution however especially in huge projects. Edit: Visual Studio 2017 |
Ran into this myself just now - using Visual Studio 2017 Community, Windows 8.1 - and both of nkindt's fixes (either Winsock2.h, or WIN32_LEAN_AND_MEAN + Windows.h) work for me. This is the first time I've tried to compile my previously Unix-based program on Windows, so I can't say whether this problem goes back any particular length of time. The opening of this thread is pretty perfect timing for me though! Thanks :) |
Thanks for the detail. I tried this with VS2019. #define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include "./httplib.h"
int main(void) {
httplib::Client cli("localhost", 8080);
cli.Get("/hi");
return 0;
} I still got the following errors:
Something conflicts with |
By the way, I confirmed that this works: #include "./httplib.h"
#include <windows.h>
int main(void) {
httplib::Client cli("localhost", 8080);
cli.Get("/hi");
return 0;
} |
This works because Winsock2.h is included "before" Windows.h. The problem occurs if it's the other way around. |
That's weird. Both arguments are size_t type. Might be a bug in MSVS. Does it help to add the type explicitly by any chance? - std::min<size_t>(read_len, CPPHTTPLIB_RECV_BUFSIZ) Also, are you compiling with proper C++ language support? |
I've been running into that std::min and max issue today too, not just in httplib (though I wasn't able to fully reproduce it there, so I didn't mention it), but in some of my own code. Adding the type explicitly as you just demonstrated, nkindt, did fix it. Also "#define NOMINMAX" before "#include <Windows.h>" fixes it also - an idea I got from |
Great catch! I missed that NOMINMAX define. That's definitely the culprit. No need for explicit type. |
Thanks for the great investigation. I took the same way as nlohmann/json does, so that users don't need to declare I'll update the README for Windows users later. Thanks for your great contribution! |
You are welcome! Thanks for the great library! |
love you bro. |
Just including the header won't always work on Windows, resulting in tons of errors of the kind (around 1810 errors in my case):
Error C2446 '!=': no conversion from 'long' to 'int (__cdecl *)(void)'
Error C2027 use of undefined type 'fd_set'
Error C2375 'WSAAsyncGetServByName': redefinition; different linkage
...
The fix is to include Winsock2.h first (before Windows.h) or include Windows.h by defining WIN32_LEAN_AND_MEAN beforehand:
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
Might be good to mention this somewhere since otherwise it's not as easy as simply including the header and get going because of this.
The text was updated successfully, but these errors were encountered: