views:

272

answers:

2

When I include winsock2.h, I get about 60 redefinition errors. I hunted around a bit a found some advice to include winsock2.h b4 including windows.h. I did that and that cleared up the errors. My problem and question concerns exactly how I should go about doing this. I did not explicitly include windows.h, it was done for me in stdafx.h or stdafx.cpp.

I added the include winsock2.h immediately b4 the include Windows.h in stdafx.h. Is this the right way to go about this or is there a better way?

Judging by a comment in program_name.rc I gather the windows.h include in stdafx.h may have been placed there as a result of some option or configuration parameter but I was unable to find this reference. Is there some way to specify what files are included in stdafx.h?

BTW, WIN32_LEAN_AND_MEAN was defined b4 calling windows.h in stdafx.h.

I am using Visual c++ 6.0 and 'Windows Server 2003 PSDK' The program is straight c++, no mfc, no net, just plain vanilla.

A: 

That should work okay. If you look in winsock2.h, you can see that it includes windows.h if it hasn't already been included.

Mark Wilkins
Judging by the comments in stdafx.h and stdafx.cpp, it is intended that the user add additional includes to stdafx.h. I'm a bit curious about the comment in stdafx.cpp// stdafx.cpp : source file that includes just the standard includes// Solar.pch will be the pre-compiled header// stdafx.obj will contain the pre-compiled type informationWhat is meant by 'type' information? Could someone give an example?
Mike D
By adding includes to stdafx.h, I *think* the precompiled headers are more efficient spacewise. But I know precompiled headers work without stdafx.* if you turn the option in in the project settings. At least Visual Studio always creates a .pch for all of my projects even though I don't have stdafx.* stuff. Someone else might have more info on 'type' information. I think, though, it is still just part of the information that it uses in conjunction with the .pch file for building.
Mark Wilkins
+1  A: 

You can put pretty much whatever you want into stdafx.h. It's certainly fine to add your #include for winsock2.h before the windows.h. I'd move the WIN32_LEAN_AND_MEAN header so that it's defined before you include any other headers:

#define WIN32_LEAN_AND_MEAN
#include <winsock2.h>
#include <windows.h>

stdafx.h is a horrendous name for the precompiled header. I have no idea why Visual Studio still uses this for all the autogenerated projects. It gives the precompiled header an undeserved air of mystery. In my projects I usually set up the precompiled header to use 'precompiled.h' and 'precompiled.cpp'.

Noel Llopis has a great article on precompiled headers - 'The Care and Feeding of Precompiled Headers' if you want a bit more background info on what's going on here.

HulkHolden