How to make G++ preprocessor output a newline in a macro?

僤鯓⒐⒋嵵緔 提交于 2019-12-17 11:21:15

问题


Is there a way in gcc/g++ 4.* to write a macro that expands into several lines?

The following code:

#define A X \ Y

Expands into

X Y

I need a macro expanding into

X
Y

回答1:


Got it!

#define anlb /*
*/ A /*
*/ B

anlb anlb

gcc -E -CC nl.c

/*
*/ A /*
*/ B /*
*/ A /*
*/ B



回答2:


Make the macro generate a special markup, say __CR__, then pipe the result of CPP into a script which translates the macro to a true newline, for example, sed 's/__CR__/\n/g'.

I just found this useful to generate a code pattern to be filled by hand. It is quite easier when the code is readable.




回答3:


You can just put a magic character sequence in the macro, e.g.

#define X(y,z) y nl z

run gcc -E infile | sed g/s/nl/\n/ > whatever

(Maybe not exactly right, but you get the idea, right? Pipe it through sed or tr or run Emacs on the output.)

I do this sometimes to use C macros to generate source code and no I don't want to learn how to do it in Perl or M4 or yet another tool.




回答4:


I'm pretty sure CPP, being designed for C which doesn't care for newlines, and all, can't handle this kind of work. Still you can mark wanted newlines with some special marker string and pass the result through sed or awk to get what you want.




回答5:


From the docs:

in the present implementation, the entire expansion comes out on one line




回答6:


Putting a side the fact that not being able to put newlines in macros create unreadable code, making it harder to debug preprocessor outputs. It is true that C and C++ might not care about newlines, but the C preprocessor does.

I would really like to make a macro ConditionalDefine(x,y) that outputs the following.

    #ifdef x
    #define y
    #endif

The following defines do something close:

    #define hash #
    #define nl
    #define _def_p8(A,B) A ifdef _P8_K60_BOARD_ A define B  A endif
    #define X_def_p8(A,B) _def_p8(A,B)
    #define def_p8(A) X_def_p8(nl hash,A)

expanding the following:

    def_p8(PTPD_DBGA 0)

results in:

    # ifdef _P8_K60_BOARD_ # define PTPD_DBGA 0    # endif

But without being able to put new lines in before the hashes it would not work as intended. It is also annoying the hoops you have to jump through just to get that close.




回答7:


Why does the spacing matter?

The imake program used in (older?) builds of X11 used the C pre-processor to generate makefiles, but imake program used a special technique of indicating line endings with @@ symbols at the ends of lines, and then post-processed the output of the pre-processor to replace the @@ symbols with newlines.

From this design, I conclude that there is no reliable way to obtain newlines from expanded macros in C (or C++). Indeed, for C, there is no need since:

  • C does not care about newlines compared with white space after the C pre-processor is run, and
  • You cannot generate pre-processor directives from macros etc.



回答8:


Is this what you want:

#define A "X \nY"


来源:https://stackoverflow.com/questions/2271078/how-to-make-g-preprocessor-output-a-newline-in-a-macro

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!