oggmonster
oggmonster

Reputation: 4872

Network/Host Byte Order Functions for multiple platforms

I am working on a project wehre current ntohl, ntohs, htonl and htons are defined as macros in a standard header file that most files include. This causes problems on platforms where these symbols are already defined, for example, winsock2.h declares functions with the same names mentioned above, and causes compile errors, as these declarations get expanded to my macro definition. On Mac OS, I get thousands of compiler warning, as Mac OS defines these macros for you already.

I want to support Windows, Mac OS and Linux, and use the standard OS functions or macros wherever possible, and if they are not decalred, then use my own definition. What is the best way to do this?

I have tried:

#if defined WIN32
  #include <winsock2.h>
#endif

but this causes compile errors as there are lots of function name clashes with my current, large codebase.

Upvotes: 0

Views: 216

Answers (1)

user207421
user207421

Reputation: 311054

This is hardly a mystery:

#ifndef ntohl
#define ntohl(x) ...
#endif

Then you just have to make sure that all language and system #includes occur before your own, which is standard practice anyway.

Upvotes: 1

Related Questions