# #define multi-line macros

This topic is 3865 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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

##### Share on other sites
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 ... :'(

##### Share on other sites
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

##### Share on other sites
#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)

##### Share on other sites
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 tryP.S.: I am working with Gcc 4.xP.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.

##### Share on other sites
Doesn't..
#define MY_MACRO cout << "My Macro\n";  \                 cout << "Line two \n";

..work?

##### Share on other sites
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 7My 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");}

##### Share on other sites
Quote:
 Original post by Sabonisbut 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?

1. 1
Rutin
26
2. 2
3. 3
4. 4
5. 5

• 9
• 11
• 10
• 13
• 20
• ### Forum Statistics

• Total Topics
632948
• Total Posts
3009389
• ### Who's Online (See full list)

There are no registered users currently online

×