Macro expansion in several lines

C

casul

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 )


and end up with the following preprocessed code :


#include NAME.hpp

namespace SPACE {
typedef NAME ImportedNAME;
}


I have a macro that generates the correct text but since macros expand
on one unique line, what I actually end up with is the following :


#include NAME.hpp namespace SPACE { typedef NAME ImportedNAME; }


The problem with this is that the preprocessor will ignore any text
behind the filename in an #include directive, so the "namespace SPACE
...." gets skipped and meerly disappears since a preprocessor directive
has to be defined on its own line.

I can't seem to find any way to have line breaks in a macro expansion.
Is there a way to get a macro to expand on several lines ?


Thanks for your help.


BR,


Olivier.
 
F

Frederick Gotham

casul:
I was told there were a few macro gurus on this group :)


Sorry for the C++ code here. The query was originally posted on
comp.lang.c++, and it was I who suggested that the OP would get a better
resonse here on comp.lang.c.

Thankfully though, the query is nothing at all to do with C++, and only
concerns the C preprocessor (even though there's some C++ code in the post).
 
S

SM Ryan

# 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 :

cpp is not a full macro processor. You will have better luck
if you use a real macro processor such as m4.

# #include MY_MACRO( NAME, SPACE )
#
#
# and end up with the following preprocessed code :
#
#
# #include NAME.hpp
#
# namespace SPACE {
# typedef NAME ImportedNAME;
# }

In m4, you can do something like
define(MY_MACRO,`$1.hpp

namespace $2 {
typedef $1 imported$1;
}
')
and it will expand to what you want. There are also
other less arcane macro languages than m4, but you
might have to hunt harder for them.
 

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

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top