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.

Re: winsock2.h and inclusion order by Ulrich

Ulrich
Thu Nov 24 08:12:16 CST 2005

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



Re: winsock2.h and inclusion order by Igor

Igor
Thu Nov 24 08:17:20 CST 2005

<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



Re: winsock2.h and inclusion order by Mark

Mark
Thu Nov 24 08:52:47 CST 2005


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.