For Programmers: Free Programming Magazines  


Home > Archive > VC Language > November 2005 > winsock2.h and inclusion order









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author winsock2.h and inclusion order
Mark.Stijnman@gmail.com

2005-11-24, 7:59 am

I get a weird problem when I include winsock2.h before my own util.h,
which defines some general (templated) utility functions in it's own
namespace. The compiler (embedded visual C++) gives me a few syntax
errors on my util.h file when I include winsock2.h first. However, when
I include them in the other order (so first util.h, then winsock.h),
everything is fine. I am most puzzled. Is there some issue with
winsock2.h? Actually, it behaves the same way with winsock.h. I can
hardly believe there's a quirk in a header as often used as winsock,
but the error can't really be in my own code, since it's really simple,
clear code, which works fine in other circumstances. Any ideas?

regards Mark

NB: if this is not the correct newsgroup for this question, feel free
to boot me to the correct one.

Ulrich Eckhardt

2005-11-24, 7:01 pm

Mark.Stijnman@gmail.com wrote:
> I get a weird problem when I include winsock2.h before my own util.h,
> which defines some general (templated) utility functions in it's own
> namespace. The compiler (embedded visual C++) gives me a few syntax
> errors on my util.h file when I include winsock2.h first. However, when
> I include them in the other order (so first util.h, then winsock.h),
> everything is fine. I am most puzzled. Is there some issue with
> winsock2.h?


I can only guess: if winsock2.h defines a macro who's name you also use in
your code, this will lead to errors. In particular, it will lead to errors
that seem to have nothing to do with the place where they occur in your
code. Perfectly valid code like
enum transport_t
{ NONE, SOCKET, SERIAL, PIPE };
will fail when there is a
#define SOCKET int
somewhere...

Uli


Igor Tandetnik

2005-11-24, 7:01 pm

<Mark.Stijnman@gmail.com> wrote in message
news:1132840658.028082.48150@g44g2000cwa.googlegroups.com
> I get a weird problem when I include winsock2.h before my own util.h,
> which defines some general (templated) utility functions in it's own
> namespace. The compiler (embedded visual C++) gives me a few syntax
> errors on my util.h file when I include winsock2.h first.


Why don't you quote some of these errors, together with the lines of
code they refer to? I seem to have misplaced my crystal ball.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925


Mark.Stijnman@gmail.com

2005-11-24, 7:01 pm


Ulrich Eckhardt wrote:
> I can only guess: if winsock2.h defines a macro who's name you also use in
> your code, this will lead to errors. In particular, it will lead to errors
> that seem to have nothing to do with the place where they occur in your
> code. Perfectly valid code like
> enum transport_t
> { NONE, SOCKET, SERIAL, PIPE };
> will fail when there is a
> #define SOCKET int
> somewhere...
>
> Uli


It turned out to be something like that... I defined min and max
functions (templated and inline), while in windef.h those were defined
as macros. I had defined min and max in my own namespace, so they
wouldn't clash with any previously defined min and max functions, but I
forgot about macros - they don't listen to namespaces. Another reason
to avoid macros in C++ I suppose...

I solved it by adding lines like these to the start of my util.h and
now it works:
#ifdef min
#undef min
#endif

Thanks for the replies.

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com