13

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.

kar
  • 495
  • 1
  • 8
  • 19
  • 1
    Regarding adding the `#define` before `stdafx.h` you might not be aware of this: https://en.wikipedia.org/wiki/Precompiled_header#stdafx.h . `#include "stdafx.h"` should be first thing. What happens if you place the `#define` right after including stdafx but before all other includes? – Michael Petch Aug 26 '15 at 18:56
  • It build successfully.. Thank you so much for this information – kar Aug 27 '15 at 13:30

3 Answers3

19

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.

Aify
  • 3,543
  • 3
  • 24
  • 43
5

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.

S.Spieker
  • 7,005
  • 8
  • 44
  • 50
Dunc
  • 63
  • 2
  • 5
0
// pch.h
#ifndef PCH_H
#define PCH_H

#define _WINSOCK_DEPRECATED_NO_WARNINGS    // defined here and it worked

#include "framework.h"
#include "xxx.h"
Zrn-dev
  • 99
  • 5