Jump to content
  • Advertisement
Sign in to follow this  
Sabonis

#define multi-line macros

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 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.

Share this post


Link to post
Share on other sites
Doesn't..

#define MY_MACRO cout << "My Macro\n"; \
cout << "Line two \n";




..work?

Share this post


Link to post
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 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");
}

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!