I have C code file containing the following include:
#include MYHEADER
I know such include using variable is a bad practice but it is not my code, I just want to compile it. I want MYHEADER variable to have <some_header.h> value, rather than "some_header.h".
I have tried may ways to pass it, so far none of them worked.
E.g. arm-none-eabi-gcc -c -DMYHEADER=<some.h> -o sample.o sample.c does not work,
I always get error: #include expects "FILENAME" or \<FILENAME\>. Using ARM GCC compiler on Windows.
Please advise.
The message you get is produced by GCC if
MYHEADERis defined to be empty (there are no tokens in its replacement list). If the command-line processor (also called a shell) you are using takes<some.h>to indicate file redirection, with<indicating to take input fromsome.hand>taking the-othat follows it to indicate writing to-o, then it leaves-DMYHEADER=with an empty replacement list.If so, a fix may be to quote the switch:
Here are two tests for this, in addition to trying the above command:
Check whether your directory contains a file named
-o. If so, it was created by the indirection, confirming that is what is happening.Create a file named
x.c, putMYHEADER(and nothing else) in it, and executearm-none-eabi-gcc -E x.c -DMYHEADER=<some.h>. If the command-line processor complains about no file name following>, that confirms the hypothesis. If it executes the command, or if it executes the command after you add, say,fooafter the>, the output will reveal whatMYHEADERis replaced with. (The-Eswitch requests preprocessing, so the single line containingMYHEADERwill be replaced by a blank line if-DMYHEADER=is passed to GCC.)One problem with this hypothesis is the command-line processor should have complained it could not open
some.hfor the input redirection. But perhaps you have a file of that name in your directory, which would satisfy the command-line processor.Another possibility is that the C standard does not fully specify how tokens resulting from macro replacement in an
#includeare processed. When<name.h>appears directly in an#include, it is processed by a special grammar token, an h-char-sequence. When macro replacement is used instead, the standard says, in C 2018 6.10.2 4:I do not expect this is the problem, as a test on Compiler Explorer suggests GCC processes a replacement of
MYHEADERwith<stdio.h>in an ordinary suitable way.