timeval undefined when using windows.h and WIN32_LEAN_AND_MEAN

tjd picture tjd · Dec 15, 2009 · Viewed 7.8k times · Source

To avoid conflicts with winsock2.h, I want to wrap my include of windows.h with WIN32_LEAN_AND_MEAN (I undef it after windows.h so as not to interfere with applications that include my headers). Doing this causes timeval to be undefined when winsock2.h isn't included. Including time.h doesn't define timeval either.

How can I get timeval defined (a) without having to include winsock2.h, (b) not requiring applications that include my headers to include winsock2.h before my headers, (c) allowing application to include winsock2.h if they need them, and (d) not having to define timeval myself, because it may already be defined by a header the parent application is including?

Answer

Robert Buckingham picture Robert Buckingham · Apr 29, 2011

I don't define either of the LEAN_AND_MEANs, instead I explicitly do the following for each of the 'NOs' prior to including windows.h

//  Exclude Input Method Manager (International stuff...one day)
#if !defined    NOIMM
    #define NOIME
    #define NOIMM
#endif

//  Exclude Metafile API
#if !defined    NOMETAFILE
    #define NOMETAFILE
#endif

This allows me to use the same StdAfx.h. With VS2010 I can include Winsock2 after windows.h without conflicts.

Stdafx is a little longer, but clearly documents what is included and excluded.