Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a C #error macro display multiple line message?

I tried to use the #error directive with GCC compiler like this:

#error "The charging pins aren't differing! One pin cannot be used for multiple purposes!"

This says, I should use double quotes, so the argument will be a single string constant and I can use an apostrophe inside of it. However, I want this string to be appeared in the source code in mutiple lines, like:

#error "The charging pins aren't differing! 
    One pin cannot be used for multiple purposes!"

Then, I got some error messages:

warning: missing terminating " character
#error "The charging pins aren't differing! One pin

error: missing terminating " character
cannot be used for multiple purposes!"

If I insert a blackslash at the end of the first line, the diagnostic message conatains all the whitespaceb between the beginning of the second line and the first word (One). If both lines are strings the diagnostic message shows the inner double quotes.

So the question: How can I achieve this output? (or a similar wihtout double quotes, but the apostrophe included)

#error "The charging pins aren't differing! One pin cannot be used for multiple purposes!"
like image 977
prodx Avatar asked Sep 03 '25 16:09

prodx


1 Answers

Unfortunately, you can't have it all.

  • Either you have to get rid of the apostrophe so that the message only contains what's regarded as valid pre-processing tokens.
  • Or you can write it as a string on a single line.
  • Or you can write it two string literals and break the line with \. You can't do this in the middle of a string literal, because then it wouldn't be a valid pre-processing token. This will make the output look weird though, like : error: "hello" "world".
  • Relying on pre-processor concatenation of two string literals won't work, since the error directive only looks until it spots a newline character in the source. And the error directive translates everything you type into strings anyway.

The relevant translation phases are (from C17 5.1.1.2) executed in this order:

2) Each instance of a backslash character () immediately followed by a new-line character is deleted, splicing physical source lines to form logical source lines.

3) The source file is decomposed into preprocessing tokens and sequences of white-space characters (including comments).

4) Preprocessing directives are executed, ...

6) Adjacent string literal tokens are concatenated.

#error is executed in step 4, earlier than string literal concatenation in step 6.

I personally think the best solution is to skip the apostrophe:

#error The charging pins are not differing! \
       One pin cannot be used for multiple purposes!

Slight fix of English and you get the best compromise between readable source and a readable error message.

like image 126
Lundin Avatar answered Sep 05 '25 04:09

Lundin