Macro expansion of '#__LINE__'?

D

Dom Gilligan

Is there any way to get the preprocessor to produce the current line
number in double quotes? At first sight, gcc seems to replace __LINE__
last (which would make sense), and so won't replace it at all if it's
preceded by '#'.

Background: I want to produce a string giving the current file and
line number in an array of structures, as follows:

---------------

#define OP(z) OP2(__FILE__, __LINE__, z)
#define OP2(x,y,z) { x ", line " #y ": ", z }
struct opinfo {
const char *preamble;
const char *cstr;
};

....

opinfo ops[] = {
OP("wibble1"),
OP("wibble2")
};

---------------

My intention is that this should produce an array of two opinfos which
look like:

opinfo ops[] = {
{ "src.cc, line 10: ", "wibble1" },
{ "src.cc, line 11: ", "wibble2" }
};

However, what it actually produces is:

opinfo ops[] = {
{ "src.cc, line __LINE__: ", "wibble1" },
{ "src.cc, line __LINE__: ", "wibble2" }
};

Any ideas?

Thanks -

Dom
 
J

Jordan Abel

Is there any way to get the preprocessor to produce the current line
number in double quotes? At first sight, gcc seems to replace __LINE__
last (which would make sense), and so won't replace it at all if it's
preceded by '#'.

#define STRING2(x) #x
#define STRING(x) STRING2(x)
#define STR_LINE STRING(__LINE__)

the stringize operator is weird, but this seems to work.
 
A

Andrey Tarasevich

Dom said:
Is there any way to get the preprocessor to produce the current line
number in double quotes? At first sight, gcc seems to replace __LINE__
last (which would make sense), and so won't replace it at all if it's
preceded by '#'.

Background: I want to produce a string giving the current file and
line number in an array of structures, as follows:

Arguments of '#' are not considered for further macro replacement. That's why
your __LINE__ is not replaced with the line number. You need an extra macro
level to give __LINE__ an opportunity to get replaced

#define OP(z) OP2(__FILE__, __LINE__, z)
#define OP2(x,y,z) OP3(x, y, z)
#define OP3(x,y,z) { x ", line " #y ": ", z }
 
V

Victor Bazarov

Is there any way to get the preprocessor to produce the current line
number in double quotes? At first sight, gcc seems to replace __LINE__
last (which would make sense), and so won't replace it at all if it's
preceded by '#'.

Background: I want to produce a string giving the current file and
line number in an array of structures, as follows:

---------------

#define OP(z) OP2(__FILE__, __LINE__, z)
#define OP2(x,y,z) { x ", line " #y ": ", z }
struct opinfo {
const char *preamble;
const char *cstr;
};

...

opinfo ops[] = {
OP("wibble1"),
OP("wibble2")
};

---------------

My intention is that this should produce an array of two opinfos which
look like:

opinfo ops[] = {
{ "src.cc, line 10: ", "wibble1" },
{ "src.cc, line 11: ", "wibble2" }
};

However, what it actually produces is:

opinfo ops[] = {
{ "src.cc, line __LINE__: ", "wibble1" },
{ "src.cc, line __LINE__: ", "wibble2" }
};

Any ideas?

You need to stringize indirectly:

#define OP(z) OP2(__FILE__, __LINE__, z)
#define OP2(x,y,z) { x ", line " STR(y) ": ", z }
#define STR(a) # a

Victor

P.S. Sorry for the extensive quoting...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top