11 Mar, 2009, Lobotomy wrote in the 1st comment:
Votes: 0
I've been finding a number of new things out about various extensions GNU has added to GCC which will enable me to create code that I wouldn't be able to otherwise, and one of those things is the __COUNTER__ macro. However, the gcc I'm using doesn't seem to recognize it.

So, I'm curious if anyone knows anything in particular about any particular modes or compiler options that may need to be enabled to gain access to it; perhaps even an upgrade to gcc altogether, although I'd be surprised if an upgrade is necessary. I'm compiling under gcc version 4.2.3, a fairly recent version, and mention of the __COUNTER__ macro (from what little I've found on google) seems to go back as far as 2007/2006, perhaps even earlier than that.

As stated with much of the commentary relating to the macro, my intent is to use it alongside macro token concatenation for the creation of unique variables within various automatic code, so getting this to work is rather important to me right now. Also, if there are other ideas for doing the same or similar in other ways I'd like to hear of it as well. Using __LINE__ instead is only going to be viable so long as I only need to create one unique variable on any particular line; otherwise, that method pretty much falls apart.

Help? :sad:
11 Mar, 2009, David Haley wrote in the 2nd comment:
Votes: 0
What kind of error messages are you seeing?

Also, it's likely that there are other ways of handling what you're trying to do; I've never used automatic variable generation like this, ever, and I've done a fair bit with code generation. It's especially strange to me that you need several variables generated per line. Could you give an example of what you're doing?
11 Mar, 2009, Lobotomy wrote in the 3rd comment:
Votes: 0
make said:
test.c: In function 'main':
test.c:82: error: '__COUNTER__' undeclared (first use in this function)

As for the line in question, it's a simple test to even see if __COUNTER__ works at all.
printf( "%d\n", __COUNTER__ );

11 Mar, 2009, David Haley wrote in the 4th comment:
Votes: 0
Dunno, works for me on 4.3.2.

$ gcc –version
gcc (Debian 4.3.2-1.1) 4.3.2
Copyright (C) 2008 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO

$ cat test.c

#include <stdlib.h>
#include <stdio.h>

int main()
printf("Hello world, %d!\n", __COUNTER__);
printf("Hello world, %d!\n", __COUNTER__);
printf("Hello world, %d!\n", __COUNTER__);
return 0;

$ gcc test.c
$ ./a.out
Hello world, 0!
Hello world, 1!
Hello world, 2!

I tried with gcc-4.2 and it didn't work. It's possible that it's new in gcc-4.3. Discussion of the macro doesn't entail that it was in the production distributions at those times.

In fact, cursory Googling (see result #3…) suggests that it is indeed new to 4.3, and was submitted as a patch in 2007. See this message.
11 Mar, 2009, Davion wrote in the 5th comment:
Votes: 0
[me@beast test]$ more test.c
#include <stdio.h>

int main()
printf( "%d\n", __COUNTER__ );
return 1;
[me@beast test]$ gcc -o test test.c
[me@beast test]$ ./test
[me@beast test]$ gcc -v
Using built-in specs.
Target: x86_64-redhat-linux
gcc version 4.3.2 20081105 (Red Hat 4.3.2-7) (GCC)

Works for me. Might need to upgrade.
11 Mar, 2009, Lobotomy wrote in the 6th comment:
Votes: 0
Well, that explains it then (also I glossed over that particular search result as irrelevant the moment I saw "g++", as I was looking for gcc - my mistake). Thanks again, DavidHaley (and Davion). :smile: