winsock deprecated no warnings

2019-04-06 03:02发布

问题:

I am trying to create a UDP multicast socket program using VS2015 (C++ console application).

I got the following error,

Error   C4996   'inet_addr': Use inet_pton() or InetPton() instead or define _WINSOCK_DEPRECATED_NO_WARNINGS to disable deprecated API warnings mulitcast_listener

I tried to add _WINSOCK_DEPRECATED_NO_WARNINGS symbol to my project settings via "Project"->"Properties"->"Configuration properties"->"C/C++"->"Preprocessor"->"Preprocessor definitions" .. But still it says the same.

And then I tried to add symbol above #include "stdafx.h" like

#define _WINSOCK_DEPRECATED_NO_WARNINGS 1

and then No(/sdl-) on "Project"->"Properties"->"Configuration properties"->"C/C++"->General->SDL checks

now I get a error message saying

Warning C4603   '_WINSOCK_DEPRECATED_NO_WARNINGS': macro is not defined or definition is different after precompiled header

Finally I tried to implement

inet_pton(AF_INET, HELLO_GROUP, (PVOID *)(&mreq.imr_multiaddr.s_addr));

instead of

mreq.imr_multiaddr.s_addr = inet_addr(HELLO_GROUP);

I need to understand why the error didn't resolved even after adding the _WINSOCK... macro.

Thanks in advance.

回答1:

As noted in the comments, the solution is to make sure that the line

#define _WINSOCK_DEPRECATED_NO_WARNINGS

is placed after

#include "stdafx.h"

but before the other #include statements.



回答2:

While the previous advice works, it is ignoring the purpose of stdafx.h. The idea is that you place #include statements for header files that don't change frequently inside stdafx.h in order to make use of precompiled headers. Therefore you should ideally place

#define _WINSOCK_DEPCRECATED 

inside stdafx.h, before other #include statements that it affects, in particular before including winsock2.h or other winsock related headers.