Advertisement

windows.h, winsock.h, winsock2.h ........ uhhh

Started by May 17, 2003 07:00 PM
3 comments, last by foofightr 21 years, 8 months ago
This is really annoying me! (VC++ 6.0, on Windows XP Pro) I have my stand-alone network classes in their own files, and they include winsock2.h because they obviously need to get at winsock functionality. Now, my winmain.cpp includes windows.h (because it needs to know HWND, HINSTANCE, etc, for the WinMain declaration), and then includes the network classes' header files (who in turn include winsock2.h). And now all hell breaks loose, I get tons of "redefinition" errors, some stemming back to winsock.h (version 1.x) even thoguh I don't include that anywhere and windows.h should NOT include it by default. I looked at all the headers in question, and it SEEMS that it should work fine. windows.h has an inclusion guard (_INC_WINDOWS), so does winsock2.h (_WINSOCK2API_), and so does winsock.h (_WINSOCKAPI_). What the heck is going on here?! Their inclusion guards should prevent such redefinition errors... windows includes wincock OR winsock2 (depending on _WIN32_WINNT value). winsock and winsock2 include windows. So it's a circular include chain, but that's what inclusion guards are for. Surely this has happened to someone else, since these are the headers that come with the friggin compiler! How do you solve this mess? [edited by - foofightr on May 17, 2003 8:06:02 PM]
Lame indeed...

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

-- BOOM!



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

-- 0 error(s), 0 warning(s)



Go figure...

[edited by - foofightr on May 17, 2003 8:09:19 PM]
Advertisement
"windows includes wincock OR winsock2 (depending on _WIN32_WINNT value), winsock and winsock2 include windows. So it''s a circular include chain, but that''s what inclusion guards are for."

Are you saying that your program uses winsock and winsock2? If so then you might be getting redefinition errors for specific functions. Like if they both define the select() function.

If you are doing
#ifndef winsock
blah blah
#ifndef winsock2
blh blah

then you would still have them both included which could cause redefinition errors I think. Winsock2 should have everything Winsock had and more so if you need something from winsock2 then why use plain winsock at all.

Keep in mind I''m still a green wet-behind-the-ears noob in this field.

One thing you may want to try. Make a file and in it put ALL of the include files you need. Then in your other files just include the
#ifndef for that single file. Might work but not sure.

Webby
I''m not including winsock.h myself anywhere, but obviously something is going on behind the scenes where it''s getting included.
You peaked my curiosity. My normal network class uses this line:
#include <winsock2.h>

and all works fine. Just for the hell of it, I did this:

#include <winsock2.h>
#include <winsock.h>

it compiled fine! I tried it the other way

#include <winsock.h>
#include <winsock2.h>

and it also compiled fine. I wasn''t expecting it to, but it did, heh. There must be some protection in winsock2.h somewhere to prevent loading of winsock.h. In fact, I just hunted for it, and found these lines right at the top:

#ifndef _WINSOCK2API_
#pragma option push -b -a8 -pc -A- /*P_O_Push_S*/
#define _WINSOCK2API_
#define _WINSOCKAPI_ /* Prevent inclusion of winsock.h in windows.h */

So step one is to make sure that last line appears in your winsock2 header.

As a final test, I did this:
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <winsock.h>
#include <winsock2.h>

I then get a bazillion redefinition errors, just like you describe.

What I suspect is happening to you is this: You are not using the lean and mean definition, so your Windows.h header is loading winsock.h (for whatever reason, it is failing the winsock vs. winsock2 test). Then you load winsock2.h in your network class and you get errors. It turns out that if you define WIN32_LEAN_AND_MEAN before loading the windows.h header, it will make no attempt to load either header for you (I just tested that behavior). Then you can load your winsock2.h header manually.

Give it a try, maybe that''ll help

Ron
Creation is an act of sheer will

This topic is closed to new replies.

Advertisement