#define multi-line macros

Started by
7 comments, last by Zahlman 16 years, 1 month ago
Hey all, I have a macro defined as such: #define MY_MACRO cout << "My Macro\n"; cout << "Line two \n"; and by virtue of the compiler, it combines that all into a single line. What if I wanted to keep the code on separate lines? I need the code to literally be translated as I have defined above with the code appearing on different lines. How can I do this? I do understand that the compiler doesn't care about it, but I have other code in macros that indeed does care about the separated lines so this is a must. Thanks
"Only two things are infinite, the universe and human stupidity, and I'm not sure about the former." - Albert Einstein (1879-1955) That is so very true...
Advertisement
Basically, you can't, at least with the traditional C or C++ preprocessor. It may be possible to use a different preprocessor that supports what you want, but your code will no longer be portable.
oh noes ... :'(
"Only two things are infinite, the universe and human stupidity, and I'm not sure about the former." - Albert Einstein (1879-1955) That is so very true...
Hehe that would be the easy way out,
but I am dealing with another library whose macros use line numbers as unique identifiers. If I make my own macros with these, they all get mapped to the same number so I get nowhere.
Thanks for the help though
"Only two things are infinite, the universe and human stupidity, and I'm not sure about the former." - Albert Einstein (1879-1955) That is so very true...
#define MACROXYZ\some code\some code\--empty line--




This works for me, however I can say for sure, since my macros usually have parameters, but I see no reason why it shouldn t work .

Just give it a try

P.S.: I am working with Gcc 4.x
P.P.S.: C++ Macros(click)
http://www.8ung.at/basiror/theironcross.html
Quote:Original post by Basiror
#define MACROXYZ\some code\some code\--empty line--




This works for me, however I can say for sure, since my macros usually have parameters, but I see no reason why it shouldn t work .

Just give it a try

P.S.: I am working with Gcc 4.x
P.P.S.: C++ Macros(click)

Add the compiler switch to write the preprocessor otuput to file, which is what the compiler sees, and check the actual code being compiled. The whole macro ends up on the same line. As you say, just give it a try.
Doesn't..
#define MY_MACRO cout << "My Macro\n";  \                 cout << "Line two \n";  

..work?
While that allows your macro to span multiple lines, but from the preprocessors point of view it is the same *line number* because you are escaping the newlines in the source file.

#include <iostream>#define MY_MACRO() std::cout << "My Macro1 " << __LINE__ << "\n";    \                         std::cout << "My Macro2 " << __LINE__ << "\n"int main(){    MY_MACRO();}

Output:
Quote:
My Macro1 7
My Macro2 7


Running with gcc -E to see the pre-processed source (omitting the header file output):
int main(){    std::cout << "My Macro1 " << 7 << "\n"; std::cout << "My Macro2 " << 7 << "\n";    std::system("pause");}
Quote:Original post by Sabonis
but I have other code in macros that indeed does care about the separated lines so this is a must.


Pardon me if I don't believe you. Show it please?

This topic is closed to new replies.

Advertisement