Are Multiline macros in GCC supported - gcc

Are multi-line macros supported(compilable) in gcc version 3.2.4. I am trying to build my source which has multi-line macros on a linux host using above mentioned gcc version.
I get compilation error at the macro, which is multiline.
#define YYCOPY(To, From, Count) \
do \
{ \
YYSIZE_T yyi; \
for (yyi = 0; yyi < (Count); yyi++) \
(To)[yyi] = (From)[yyi]; \
} \
while (0)
If they are not supported, what is the workaround for this, converting the macro to a function or some other compiler option can help?
thank you.
-AD

Backslashes to continue the macro is standard preprocessor. Check for extra spaces or other invisible characters after your backslash.

The ANSI C specification requires compilers to support this -- specifically, the standard says that if a line ends in a backslash immediately before the newline, the preprocessor is to treat that line and the subsequent line as one logical line, as if both the backslash and the newline did not exist. If a preprocessor does not do this, it is not a conforming preprocessor (or more technically, a translator, as the standard calls it).
GCC strives to be as conforming as possible to the ANSI C standard. Yes, it support multiline macros defined with backslashes at the end of lines.
The reason you're getting compiler errors is something else. You're not using the macro properly. Without posting the exact error messages you're receiving and the code which invokes the macro, it's impossible to say what you're doing wrong.

Related

gcc preprocessor: how to escape quotes in an argument

I am trying to use gcc preprocessor for its macro expansion capabilities (I'm not trying to produce code).
I have a macro — MY_MACRO — that needs to get an argument that has double quotes inside
As you can see, the preprocessor produces an error: unterminated argument list
Is there a way to escape the quotes?
#define MY_MACRO(X)
MY_MACRO(prefix"suffix)
For example:
$ gcc -E -P -w a.txt
error: unterminated argument list invoking macro "MY_MACRO"
$
The C preprocessor works on C source code. A lone " is not valid C, so the cpp rejects it. You can cpp for other purposes only if you're willing to stick to the C syntax rules.
m4 is a general-purpose macro-processor and standard installed on anything Unix-like, though nobody likes it. There aren't many alternatives.

How do I use -Wall option on gcc/g++ and turn off the multi line comments warnings?

Like the title, how do I use -Wall option on gcc/g++ and turn off the multi line comments warnings?
The comment look like that:
// Comment starts here \
// and end here (the // at the begging is not necessary)
You could use /* .. */ for the multiline comment.
/* foo bar comment
lala blah
*/
Edit:
I found a solution in another post here: How can I hide "defined but not used" warnings in GCC?
If you add the option -Wno-comment the warning is gone.
gcc -Wall -Wno-comment test.c -o test
Its also explained here: http://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html
best wishes,
Matthias
The GCC warnings are documented here: http://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html
Specifically, you're looking for:
-Wcomment
Warn whenever a comment-start sequence /*' appears in a/*' comment, or whenever a Backslash-Newline appears in a `//' comment. This warning is enabled by -Wall.
So to turn that warning off, you should be able to use -Wno-comment.
The backslash continuation induces the warning; it is not necessary if you start the next line with a comment start.
Remove the backslash continuation and replace the backslashes at the start of the line with // (as written, it is not a comment at all) and you should quell the warnings.
You can also write:
/\
\
* A regular C89 comment with trigraphs for good measure.
*??/
??/
/
and anyone who presented that code to me for review would be sent back to fix it immediately (but pity the poor compiler writer who has to handle such nonsense correctly!).
As to how to fix it, if (as is mentioned in a comment), it is prevalent then you can see whether your version of GCC will support -Wno-comment to suppress warnings about multiline comments. That is probably simplest. Failing that, you have to decide whether you can risk using a heuristic parser to fix the problem:
sed -e 's%\(//.*\)\\$%\1%' -i.bak *.c
(assuming GNU sed for the -i.bak option). This removes the trailing backslash from any such comment. The main place where I could see this causing trouble is in a macro definition such as:
#define MACRO(c,d) { (c) = (d) + 1; } // Comment here \
more comment here
It's an inane macro, but because there is no comment-start on the second line of the comment, the heuristic substitution in the sed script would give you a syntactically invalid program (unless you just so happened to have macros such that 'more comment here' expands to valid code, which is pretty unlikely).
If the heuristic mechanism won't work, then maybe you need a full C comment stripper. If you can't find one on the Internet somewhere, I have such a program (and its test files include comments such as the gruesome one shown above) — see my profile.

cross compile (arm-none-eabi-as) arm assembly error "junk at end of line /" or undefined symbol

Hi while i cross compile an startup.s file
(arm-none-eabi-as file.s)
(*-gcc)
I am getting in each commentary line some errors
- junk at end of line, first unrecognized character is /
when i delete the // some comment lines i get
errors about undefined symbols even i defined them at beginning of the file.
anyone know whats wrong?
If you want to use macros or C comments, then you have to preprocess the source file with the C preprocessor. The C preprocessor removes comments and interprets macros. The GNU assembler should run the C preprocessor automatically if the source file name ends with .S, with an uppercase 'S'.
(arm) Assembler does not support // comments or defines, you have to use .equ and # for comments. If you let gcc parse it you can put C isms like that into your assembler. Personally I avoid such C isms and keep the assembler clean. if you cannot do that or need includes with defines for example let gcc pre-process the file before sending it to gas.

Preprocessor output

How do I view the output produced by the C pre-processor, prior to its conversion into an object file?
I want to see what the MACRO definitions do to my code.
gcc -E file.c
or
g++ -E file.cpp
will do this for you. The -E switch forces the compiler to stop after the preprocessing phase, spitting all it’s got at the moment to standard output.
Note: Surely you must have some #include directives. The included files get preprocessed, too, so you might get lots of output.
For Visual C++ the switch is /E which spits the preprocessor output to screen.
You can also call the C Preprocessor directly.
cpp infile outfile
Check out man cpp for more info.
For GCC,
gcc -E -dM file.c
or
g++ -E -dM file.cpp
should do the job. -dM, as GNU Preprocessor manual puts it, should generate a list of ‘#define’ directives for all the macros defined during the execution of the preprocessor, including predefined macros.
It depends on the compiler you use.
With GCC, you can specify the -E flag on the command-line to let the compiler produce the pre-processor output.
If using CLion by Jetbrains, you can use the action "clangd: Preprocess current TU"
So hit shift shift and start typing clangd...
Best assign it to a shortcut for simpler reuse in preferences->keymap:
Shout out to marcosbento
PS: TU means 'translation unit' (see here LLVM translation unit)
You can check out my script described here:
http://mosermichael.github.io/cstuff/all/projects/2011/09/16/preprocessor.html
It formats the preprocessor output into a (hopefully) readable html document: lines that are different due to preprocessor are marked in the file.

GCC preprocessor removing comments

Is it possible to instruct the GCC preprocessor not to remove comments when processing files?
GCC has the -C option to preserve comments.
`-C'
Do not discard comments. All comments are passed through to the
output file, except for comments in processed directives, which
are deleted along with the directive.
You should be prepared for side effects when using `-C'; it causes
the preprocessor to treat comments as tokens in their own right.
For example, comments appearing at the start of what would be a
directive line have the effect of turning that line into an
ordinary source line, since the first token on the line is no
longer a `#'.
Yes, you can do it with the -C option. E.g.
gcc -C -E myfile.c
man gcc and use / -C (slash, space, dash, capitcal C) to make less (which is probably your pager program) search for the -C option, use n to search again (the description is the 3rd hit). Related options are both above and below.

Resources