Today I stumbled upon a rather interesting compiler error:
int main() {
int const unix = 0; // error-line
return unix;
}
Gives the following message with gcc 4.3.2 (yes, ancient…):
error: expected unqualified-id before numeric constant
which is definitely quite confusing.
Fortunately, clang (3.0) is a little more helpful (as usual):
error: expected unqualified-id
int const unix = 0
^
<built-in>:127:14: note: expanded from:
#define unix 1
^
I certainly did not expect unix, which is neither written in upper-case nor begin with underscore to be a macro, especially a built-in one.
I checked the predefined macros in gcc and there are 2 (on my platform) that use “unreserved” symbols:
$ g++ -E -dM - < /dev/null | grep -v _
#define unix 1
#define linux 1
All the others are “well-behaved” macros with leading underscores, using the traditional reserved identifiers, sample:
#define __linux 1
#define __linux__ 1
#define __gnu_linux__ 1
#define __unix__ 1
#define __unix 1
#define __CHAR_BIT__ 8
#define __x86_64 1
#define __amd64 1
#define _LP64 1
(it’s a mess and there does not seem to be any particular order…)
Furthermore, there are lots of “similar” symbols, so I guess there is an issue of backward compatibility…
So, where do the unix and linux macros come from ?
gcc does not fully conform to any C standard by default.
Invoke it with
-ansi,-std=c99, or-std=c1xandunixwon’t be predefined. (-std=c1xwill probably becomebecame-std=c11in afuturemore recent gcc release.)It’s a bit confusing that this is documented in the separate manual for the GNU preprocessor, not in the gcc manual.
Quoting the GNU preprocessor documentation (
info cpp, version 4.5):The current version of the manual is here.