473,394 Members | 1,721 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

is it possible to write such a macro?

#define ONCFILE_ERR1(funcname, name) \
{ \
#ifdef DEBUG\
cerr << __FILE__ << ":" << __LINE__ << " duplicated call to " <<
funcname << " " << name << endl; \
#endif \
}
I want to have conditional macros inside a macro. When I compile this
code, the error message is:
ONetCDFFile.cpp:14:2: '#' is not followed by a macro parameter

Any idea how I should proceed with this?

Mar 24 '06 #1
7 23418
another question about macro, how do i force a return at the end of
each macro line?
it seems the end result of multi-line macro always colapse into a
single line...

fe*****@gmail.com wrote:
#define ONCFILE_ERR1(funcname, name) \
{ \
#ifdef DEBUG\
cerr << __FILE__ << ":" << __LINE__ << " duplicated call to " <<
funcname << " " << name << endl; \
#endif \
}
I want to have conditional macros inside a macro. When I compile this
code, the error message is:
ONetCDFFile.cpp:14:2: '#' is not followed by a macro parameter

Any idea how I should proceed with this?


Mar 24 '06 #2
fe*****@gmail.com wrote:
#define ONCFILE_ERR1(funcname, name) \
{ \
#ifdef DEBUG\
cerr << __FILE__ << ":" << __LINE__ << " duplicated call to " <<
funcname << " " << name << endl; \
#endif \
}
I want to have conditional macros inside a macro. When I compile this
code, the error message is:
ONetCDFFile.cpp:14:2: '#' is not followed by a macro parameter

Any idea how I should proceed with this?


Your code is C++, so asking in comp.lang.c++ is
better than here. However I don't think the preprocessor
is different. Think about how \ works with lines being
spliced together. Your #ifdef is in the
middle of a line... However you
could achieve what you want above with

#ifdef DEBUG
#define ONCFILE_ERR1(funcname, name) \
{ some code; }
#else
#define ONCFILE_ERR1(funcname,name)
#endif

-David

Mar 24 '06 #3
fe*****@gmail.com wrote:
#define ONCFILE_ERR1(funcname, name) \
{ \
#ifdef DEBUG\
cerr << __FILE__ << ":" << __LINE__ << " duplicated call to " <<
funcname << " " << name << endl; \
#endif \
}
I want to have conditional macros inside a macro. When I compile this
code, the error message is:
ONetCDFFile.cpp:14:2: '#' is not followed by a macro parameter

Any idea how I should proceed with this?

Turn it inside out...

#ifdef DEBUG
#define ONCFILE_ERR1(funcname, name) \
cerr << __FILE__ << ":" << __LINE__ << " duplicated call to " \
<< funcname << " " << name << endl
#else
#define ONCFILE_ERR1(funcname, name)
#endif

HTH,
--ag

--
Artie Gold -- Austin, Texas
http://goldsays.blogspot.com
"You can't KISS* unless you MISS**"
[*-Keep it simple, stupid. **-Make it simple, stupid.]
Mar 24 '06 #4
Thank you for your reply..sorry about the c++ code although my point is
really just the preprocessing macro. Your suggestions work for this
simple macro, what if I have something more complicated such as

#define macro1
do stuff
#ifdef c1
do something
#endif
do stuff
#ifdef c2
do something else
#endif
.....
#endif /* macro1 */

as you can see, if it's possible to embed "#ifdef" etc inside #define,
things are much easier. Is this possible?

Fei
David Resnick wrote:
fe*****@gmail.com wrote:
#define ONCFILE_ERR1(funcname, name) \
{ \
#ifdef DEBUG\
cerr << __FILE__ << ":" << __LINE__ << " duplicated call to " <<
funcname << " " << name << endl; \
#endif \
}
I want to have conditional macros inside a macro. When I compile this
code, the error message is:
ONetCDFFile.cpp:14:2: '#' is not followed by a macro parameter

Any idea how I should proceed with this?


Your code is C++, so asking in comp.lang.c++ is
better than here. However I don't think the preprocessor
is different. Think about how \ works with lines being
spliced together. Your #ifdef is in the
middle of a line... However you
could achieve what you want above with

#ifdef DEBUG
#define ONCFILE_ERR1(funcname, name) \
{ some code; }
#else
#define ONCFILE_ERR1(funcname,name)
#endif

-David


Mar 24 '06 #5
fe*****@gmail.com wrote:
Thank you for your reply..sorry about the c++ code although my point is
really just the preprocessing macro. Your suggestions work for this
simple macro, what if I have something more complicated such as

#define macro1
do stuff
#ifdef c1
do something
#endif
do stuff
#ifdef c2
do something else
#endif
....
#endif /* macro1 */

as you can see, if it's possible to embed "#ifdef" etc inside #define,
things are much easier. Is this possible?

No.

--ag

--
Artie Gold -- Austin, Texas
http://goldsays.blogspot.com
"You can't KISS* unless you MISS**"
[*-Keep it simple, stupid. **-Make it simple, stupid.]
Mar 24 '06 #6
fe*****@gmail.com schrieb:
#define ONCFILE_ERR1(funcname, name) \
{ \
#ifdef DEBUG\
cerr << __FILE__ << ":" << __LINE__ << " duplicated call to " <<
funcname << " " << name << endl; \
#endif \
}
I want to have conditional macros inside a macro. When I compile this
code, the error message is:
ONetCDFFile.cpp:14:2: '#' is not followed by a macro parameter

Any idea how I should proceed with this?


The workaround already has been stated; back to the issue:
- Preprocessing directives can only span one line (the \ may hide
that but the above is only one source line) and there may be only
one preprocessing directive per line.
- Preprocessing directives start the respective line (but for
whitespace)
- Thus, you cannot generate preprocessing directives using
preprocessing directives; the language does not allow it.

The reason for the error message is another one:
The preprocessor assumes that the # is the "stringize" preprocessor
operator; this operator surrounds the passed argument by double
quotes ("), i.e. makes them into string literals.

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Mar 24 '06 #7
"fe*****@gmail.com" wrote:
fe*****@gmail.com wrote:

#define ONCFILE_ERR1(funcname, name) \
{ \
#ifdef DEBUG\
cerr << __FILE__ << ":" << __LINE__ << " duplicated call to " <<
funcname << " " << name << endl; \
#endif \
}

I want to have conditional macros inside a macro. When I compile
this code, the error message is:
ONetCDFFile.cpp:14:2: '#' is not followed by a macro parameter

Any idea how I should proceed with this?


another question about macro, how do i force a return at the end
of each macro line? it seems the end result of multi-line macro
always colapse into a single line...


Don't toppost. Your answer (or continuation) belongs after (or
intermixed with) the material you quote, after snipping irrelevant
material. I fixed this one.

You can't do that.

Ignoring the C++, and just considering macros (which are common to
C++ and C). By definition a macro (the part after the #define)
ends on the same line. This can be partially ameliorated by the
use of continuation lines (where the last character on the physical
line is a '\'). You CANNOT put preprocessing conditionals inside a
macro, because they have to start a line, and you can't get there
without ending the macro definition.

The usual trick to form multiline macros is the following:

#define MULTILINEMACRO \
do { \
statement1; \
statement2; \
} while (0)

Notice no final semicolon. The limit is the maximum input line the
compiler can handle, which is guaranteed about 500 for C90. Look
it up. This behaves properly for all macros that do not have to
return a value.

In future please ask C++ questions on c.l.c++, and C questions on
c.l.c. Don't toppost on either newsgroup.

--
Read about the Sony stealthware that is a security leak, phones
home, and is generally illegal in most parts of the world. Also
the apparent connivance of the various security software firms.
http://www.schneier.com/blog/archive...drm_rootk.html
Mar 25 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

25
by: Andrew Dalke | last post by:
Here's a proposed Q&A for the FAQ based on a couple recent threads. Appropriate comments appreciated X.Y: Why doesn't Python have macros like in Lisp or Scheme? Before answering that, a...
699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
2
by: Edgar | last post by:
I want to go from viewing a form (record number "x") to the same record ("x") in a report. Of course, when I open the report, it opens to the first record, not record "x". I don't know VBA,...
4
by: Vittal | last post by:
Hello All, Here is a small C program, main() { int a= 100; float b =99.99; TEST(a,%d); TEST(b,%f);
4
by: Max TenEyck Woodbury | last post by:
I need a macro that will take an arbitrarily long list of arguments where each argument needs to be passed to another macro. Is it possible to write such a macro? If so, could you please...
45
by: madhawi | last post by:
Is it better to use a macro or a function?
3
by: deve8ore | last post by:
Hello, I have built a macro to automatically go to a specific directory and a specific folder, however the names of the files I need to automatically locate change names every month. I.e. - I...
2
by: deve8ore | last post by:
Hello, I receive a workbook via 3rd party containing graphs within each sheet. The workbook may contain a different number of sheets per file. I would like to write a macro to go through each...
3
by: thinktwice | last post by:
i want to define a macro like #define STRING2(x) #x #define STRING(x) STRING2(x) #define PROMPT(x) #pragma message(__FILE__ "(" STRING(__LINE__) "):" STRING(x)) <---- compile failed i...
13
by: alkos333 | last post by:
I'm trying figure out the best way to deal with the following situations: This scenario is oversimplified for the sake of the question. Let's say I have 3 tables: Transactions, Accounts and...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.