Are there certain keywords that should not be “#defined” by me?

  • A+
Category:Languages

I am developing a platform layer for my application in C/C++.

I would like to

#define WINDOWS // on Windows machines

#define ANDROID // on Android phones

Is it a bad idea to define very common keywords like "WINDOWS" or "ANDROID" due to conflict with other libraries and would it make sense to prefix these keywords with something:

#define MYLIB_WINDOWS // Not used by any other 3rdparty libraries

#define MYLIB_ANDROID

 


I would like to

Beware of exposing macros with 'common' names in your library's header files.

Beware also of commonly named macros in private code - particularly if it #includes (either directly or indirectly) other library's headers.

You cannot rely on the hope that all library maintainers are as well-behaved as you.

and would it make sense to prefix these keywords with something:

In general, yes.

A great example of this is the BOOST suite of libraries. BOOST takes great care to ensure that all macros exported by its header files have the prefix BOOST_. It is no coincidence that the prefix matches the library's namespace name of boost::.

In summary, if your library is implemented in a namespace (and it should be, otherwise you're guilty of polluting the global namespace), please do use a macro prefix that matches it.

Example:

namespace mylib { namespace innerthing { }}  #define MYLIB_ON 1 #define MYLIB_OFF 0 #define MYLIB_SETTING MYLIB_ON #define MYLIB_INNERTHING_SETTING MYLIB_OFF 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: