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 4 8323
On 2005-11-04, Dom Gilligan <dg****@hotmail.com> wrote: 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.
Dom Gilligan wrote: 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 }
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 }
--
Best regards,
Andrey Tarasevich
On Fri, 04 Nov 2005 17:09:53 +0000, Dom Gilligan wrote: 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...
Thanks guys - those all worked. That's what I call service!
Dom This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Caleb Hattingh |
last post by:
Hi
Here is a script I want to be able to write (explanation appears after):
*** start of script ***
import MyCustomMacroLib # This does the magic I would like help for.
# This is not...
|
by: Ark |
last post by:
Hello, NG,
Please, help on this snippet:
#define CAT(a,b) a##b
#define COMMENT CAT(/,/)
COMMENT This is a comment
Should it compile?
It passes MS C/C++ 13.0 (Visual Studio 2002) and fails...
|
by: reppisch |
last post by:
Hi Ng,
i am looking for a method of expanding a macro while the rest of the
code remains untouched.
I have some code which does macro voodo / ifdef's which i would like to
strip and simplify.
...
|
by: ImOk |
last post by:
I come from the Visual Foxpro world, which is one reason I love PHP.
VFP is a scripting type language with macro substitution abilities
similar to PHP.
Besides the regular expansion I can do...
|
by: srinu.fsl |
last post by:
there's a MACRO call :
MACRO1(cnf)
and its expansion is :
#define MACRO1(cnf) (((cnf) != TRUE)? (CLEANUP(FAIL)):(err =
SUCCESS));
#define CLEANUP(a)
|
by: casul |
last post by:
Hi All,
I was told there were a few macro gurus on this group :)
I'm trying to define a macro that will allow me to write the following
code :
#include MY_MACRO( NAME, SPACE )
|
by: borophyll |
last post by:
Hi all
Can anyone explain to me the algorithm for macro expansion in the C++
preprocessor. I am confused why the following code works like it
does:
#define A(x) #x
#define B(x) A(x)...
|
by: todWulff |
last post by:
Good day folks.
Let me open with the statement that I am not a C++/C programmer. The environment that I am programming in is ARMbasic, an embedded BASIC targeted toward ARM-based...
|
by: Srinivas Mudireddy |
last post by:
Hi,
We have bunch of message levels and depending on whether that level is
turned on, messages of that level are printed. For example, we have
levels like MSG_LOW, MSG_MED etc.
We want to...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: Aftab Ahmad |
last post by:
Hello Experts!
I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
|
by: Aftab Ahmad |
last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below.
Dim IE As Object
Set IE =...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
| |