1

I have following macro:

#define DEFINE_EXPORT_MODULE(__module__) /##*Exported by __module__*/

If I use it like this: DEFINE_EXPORT_MODULE(foo) it expands properly into /*Exported by foo*/ but I still get the E0169 error: "Expected a declaration."

What is wrong?

Edit: Here is how I use it in my code.

#define DEFINE_EXPORT_MODULE(__module__) /##*Exported by __module__*/

DEFINE_EXPORT_MODULE("foo.dll") //this produces the error.
void Function(void);

James
  • 49
  • 1
  • 5
  • Can we get a [mre]? – NathanOliver Dec 17 '19 at 16:27
  • 1
    Don’t use names starting with double underscores for your own names, these are all reserved for compiler use. – Cubic Dec 17 '19 at 16:30
  • What compiler are you using? g++ throws an error with this. – dbush Dec 17 '19 at 16:33
  • 1
    Which compiler are you using? I didn't think it was legal for the preprocessor to form new tokens. e.g. to form `/*` should elicit an error like _pasting formed `/*`, an invalid preprocessing token_ – Wyck Dec 17 '19 at 16:35
  • @Cubic Changed it from ```__module__``` to ```_module```. Didn't help unfortunately. – James Dec 17 '19 at 16:36
  • 3
    Why are you using the C preprocessor to embed comments? Who will ever see them? What is your target audience for the comment? Where are you hoping this comment show up? – Wyck Dec 17 '19 at 16:37
  • 1
    [This answer](https://stackoverflow.com/a/1510919/1563833) may be relevant: regarding the behaviour of comments in the translation phase. – Wyck Dec 17 '19 at 16:40
  • @Wyck I use them for myself to remember which module exports which function I import. I don't want to write ```/*Exported by ...*/``` like 30 times. – James Dec 17 '19 at 16:40

1 Answers1

2

The result of the paste operator must be a valid token after macros are expanded.

Trying to create a comment fails because comments are removed before pre-processing starts.

See the reference.

jxh
  • 69,070
  • 8
  • 110
  • 193
  • You should quote the passage that goes: > A ## operator between any two successive identifiers in the replacement-list runs parameter replacement on the two identifiers (which are not macro-expanded first) and then concatenates the result. This operation is called "concatenation" or "token pasting". Only tokens that form a valid token together may be pasted: identifiers that form a longer identifier, digits that form a number, or operators + and = that form a +=. A comment cannot be created by pasting / and * because comments are removed from text before macro substitution is considered ... – Wyck Dec 17 '19 at 16:59