I recently stumbled across an article referencing macros defined by gcc. That list is pretty daunting! If you compare it to GCC Common Predefined Macros (the official source), you realize quite fast that a lot of those macros exist for the benefit of libc and libstdc++ library authors—not compiler end users such as myself.
For whatever reason, it takes quite a bit of Google-Fu (or luck) to get GCC's official page to show up on the first page of search results. Whenever I search for it, if I don't remember the page title exactly, I have to dig through about 20 other sites before finding it. In any case, both of those lists are frigging huge, so here's a bit of a smaller list that I usually have up my sleeves:
- Use this instead of __CHAR_BIT__. Required by the C and C++ language standards (IIRC) and defined in <limits.h> or <climits>. Number of bits in a byte. I'm not old enough to have worked on any machines that didn't have 8-bit bytes, but they certainly existed at one point (and it's the reason why my university networking class used the term octets instead of bytes).
- __GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__
- GCC-specific versioning info, considered as a tuple. For example, if I'm using GCC 4.3.1, those values will respectively be (4, 3, 1). Simple, and useful for code that needs to be compatible across multiple versions of the compiler, or requires language features that are newer (such as using GCC's new builtin atomic intrinsics).
If you want to simply identify any GCC-family compiler (including the Intel C/C++ compilers), simply look for __GNUC__'s existence.
- size_t, ptrdiff_t
- Use these instead of __SIZE_TYPE__, __PTRDIFF_TYPE__. Required by the C++ language standard and defined in <stddef.h> or <cstddef>. Occasionally, I'll write code that should follow STL semantics, but it doesn't bring in any other parts of the STL. I always forget that <cstddef> is the header to bring in for the machine-agnostic size types.
- Still not quite as good as nullptr, GCC does have some magic that provides better type-checking than just using the literal 0 in C++. However, I've recently found that in GCC 4.3.0 that it introduces ambiguouity:
std::vector<T*>::push_back(NULL)confuses the compiler, and I have to instead write
T *nullptr = NULL; std::vector<T*>::push_back(nullptr). Once that breaks, I'll just delete the first line.
- Defined by GCC if the -ansi flag is set. You probably don't need to depend upon it, but if you really want to be anal (or a jerk, depending on your point of view), you could error out if __STRICT_ANSI__ was not set, thereby forcing users to crank up the standardization in their compilers. In my experience, this in combination with Visual Studio's DisableLanguageExtensions setting makes it much easier to write code that works across both GCC/Linux and MSVC/Win32.
- __TIMESTAMP__, __BASE_FILE__
- Ever need to get automated build metrics out of a compilation phase? __TIMESTAMP__ records the system time when the compile took place, and __BASE_FILE__ records the name of the source file being compiled (as opposed to __FILE__, which lists the current file). You could craft up a common project header file that somehome embedded these values into the object file (say by using a static global in an anonymous namespace that writes those strings to an empty file descriptor). Not useful in 99% of the cases, but it's a good way to add forensic traceability data to a build.
- __FILE__, __LINE__, __func__, __PRETTY_FUNCTION__
- Diagnostics for "where am I?" Note that the first three are standard and provided by any compiler, __PRETTY_FUNCTION__ is a GCC-ism and contains the "decorated name", which includes all of the arguments and template instantiations. The only good reason I've heard to use __PRETTY_FUNCTION__ is the template instantiation inclusion (e.g. "this problem happens with
std::vector<MyCrappyType>, and not with just any vector type).
- This macro is also provided by the Visual Studio compiler. It simply expands to an increasing integer value, and the only use I've found for it is to provide unique ids in auto-generated code (say, for the TUT C++ testing framework).
- A necessity for writing headers compatible with both C and C++. 'Nuff said.
- linux, unix, i386
- Since I don't really write code that's cross-UNIXish-platform, I've never really had to mess with GCC on other platforms (BSD/Solaris/AIX/Win32), nor have I had to use other non-GCC-family compilers on Linux. I imagine these would come in handy for those cases, but I can't speak to their exact uses.
Since I just noticed the original article I referred to has a snide comment about development environments, I'll include this just for kicks: Predefined Macros in VC++ 2005.