Skip to content
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

Closed
nkindt opened this issue Mar 16, 2020 · 12 comments
Closed

Winsock2.h problem and fix #387

nkindt opened this issue Mar 16, 2020 · 12 comments

Comments

@nkindt
Copy link

nkindt commented Mar 16, 2020

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.

@yhirose
Copy link
Owner

yhirose commented Mar 16, 2020

@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;
}
D:\cpp-httplib>cl a.cc /EHsc
Microsoft (R) C/C++ Optimizing Compiler Version 19.23.28106.4 for x86
Copyright (C) Microsoft Corporation.  All rights reserved.

a.cc
Microsoft (R) Incremental Linker Version 14.23.28106.4
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:a.exe
a.obj

D:\cpp-httplib>

Do I miss something to reproduce the problem? Thanks.

@nkindt
Copy link
Author

nkindt commented Mar 16, 2020

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

@dnlopez
Copy link

dnlopez commented Mar 16, 2020

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 :)

@yhirose
Copy link
Owner

yhirose commented Mar 16, 2020

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:

D:\cpp-httplib>cl a.cc  /EHsc
Microsoft (R) C/C++ Optimizing Compiler Version 19.23.28106.4 for x86
Copyright (C) Microsoft Corporation.  All rights reserved.

a.cc
D:\Projects\cpp-httplib\./httplib.h(1788): error C2589: '(': illegal token on right side of '::'
D:\Projects\cpp-httplib\./httplib.h(1788): error C2062: type 'unknown-type' unexpected
D:\Projects\cpp-httplib\./httplib.h(1788): error C2059: syntax error: ')'
D:\Projects\cpp-httplib\./httplib.h(1789): error C3536: 'n': cannot be used before it is initialized
D:\Projects\cpp-httplib\./httplib.h(1808): error C2589: '(': illegal token on right side of '::'
D:\Projects\cpp-httplib\./httplib.h(1808): error C2062: type 'unknown-type' unexpected
D:\Projects\cpp-httplib\./httplib.h(1808): error C2059: syntax error: ')'
D:\Projects\cpp-httplib\./httplib.h(1809): error C3536: 'n': cannot be used before it is initialized
D:\Projects\cpp-httplib\./httplib.h(2872): warning C4003: not enough arguments for function-like macro invocation 'max'
D:\Projects\cpp-httplib\./httplib.h(2872): error C2589: '(': illegal token on right side of '::'
D:\Projects\cpp-httplib\./httplib.h(2872): error C2059: syntax error: ')'
D:\Projects\cpp-httplib\./httplib.h(2872): error C2612: trailing '>' illegal in base/member initializer list
D:\Projects\cpp-httplib\./httplib.h(4008): warning C4003: not enough arguments for function-like macro invocation 'max'
a.cc(9): fatal error C1004: unexpected end-of-file found

D:\cpp-httplib>

Something conflicts with std::min...
Anyway, I'll work on the bug when I have time. Thanks for the great report!

@yhirose
Copy link
Owner

yhirose commented Mar 16, 2020

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;
}

@nkindt
Copy link
Author

nkindt commented Mar 16, 2020

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.

@nkindt
Copy link
Author

nkindt commented Mar 16, 2020

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:

D:\cpp-httplib>cl a.cc  /EHsc
Microsoft (R) C/C++ Optimizing Compiler Version 19.23.28106.4 for x86
Copyright (C) Microsoft Corporation.  All rights reserved.

a.cc
D:\Projects\cpp-httplib\./httplib.h(1788): error C2589: '(': illegal token on right side of '::'
D:\Projects\cpp-httplib\./httplib.h(1788): error C2062: type 'unknown-type' unexpected
D:\Projects\cpp-httplib\./httplib.h(1788): error C2059: syntax error: ')'
D:\Projects\cpp-httplib\./httplib.h(1789): error C3536: 'n': cannot be used before it is initialized
D:\Projects\cpp-httplib\./httplib.h(1808): error C2589: '(': illegal token on right side of '::'
D:\Projects\cpp-httplib\./httplib.h(1808): error C2062: type 'unknown-type' unexpected
D:\Projects\cpp-httplib\./httplib.h(1808): error C2059: syntax error: ')'
D:\Projects\cpp-httplib\./httplib.h(1809): error C3536: 'n': cannot be used before it is initialized
D:\Projects\cpp-httplib\./httplib.h(2872): warning C4003: not enough arguments for function-like macro invocation 'max'
D:\Projects\cpp-httplib\./httplib.h(2872): error C2589: '(': illegal token on right side of '::'
D:\Projects\cpp-httplib\./httplib.h(2872): error C2059: syntax error: ')'
D:\Projects\cpp-httplib\./httplib.h(2872): error C2612: trailing '>' illegal in base/member initializer list
D:\Projects\cpp-httplib\./httplib.h(4008): warning C4003: not enough arguments for function-like macro invocation 'max'
a.cc(9): fatal error C1004: unexpected end-of-file found

D:\cpp-httplib>

Something conflicts with std::min...
Anyway, I'll work on the bug when I have time. Thanks for the great report!

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?

@dnlopez
Copy link

dnlopez commented Mar 16, 2020

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
nlohmann/json#506

@nkindt
Copy link
Author

nkindt commented Mar 16, 2020

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
nlohmann/json#506

Great catch! I missed that NOMINMAX define. That's definitely the culprit. No need for explicit type.

@yhirose
Copy link
Owner

yhirose commented Mar 16, 2020

Thanks for the great investigation. I took the same way as nlohmann/json does, so that users don't need to declare NOMINMAX.
685533b
nlohmann/json@95474e4
https://stackoverflow.com/questions/27442885/syntax-error-with-stdnumeric-limitsmax/27443191#27443191

I'll update the README for Windows users later. Thanks for your great contribution!

@nkindt
Copy link
Author

nkindt commented Mar 16, 2020

You are welcome! Thanks for the great library!

yhirose added a commit that referenced this issue May 2, 2020
@zacario-li
Copy link

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

love you bro.

ExclusiveOrange pushed a commit to ExclusiveOrange/cpp-httplib-exor that referenced this issue May 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants