Hello, I'm using KEIL V5.10 with STM32F103. My project has some enum type variables, such as typedef enum { a0 = 0, a1, a2, an } aType;
In source file, I used conditional compile statements:
#if (an > 0) xxxx #endif
But the body inside was not compiled whatever an is higher than zero, I don't know why.
An array can be correctly complied if I used an as array's length, such as
static uint16_t u16Buf[an];
There is no warning or error after compilation.
Anyone knows why the conditional compilation does not work?
Thanks!
Thanks for your reply!
But what do you think about GCC document which talks about #if directive, as shown below
gcc.gnu.org/.../cpp_4.html
========================== The '#if' directive allows you to test the value of an arithmetic expression, rather than the mere existence of one macro. Its syntax is
#if expression
controlled text
#endif /* expression */
expression is a C expression of integer type, subject to stringent restrictions. It may contain
Integer constants. Character constants, which are interpreted as they would be in normal code. Arithmetic operators for addition, subtraction, multiplication, division, bitwise operations, shifts, comparisons, and logical operations (&& and ||). The latter two obey the usual short-circuiting rules of standard C. Macros. All macros in the expression are expanded before actual computation of the expression's value begins. Uses of the defined operator, which lets you check whether macros are defined in the middle of an '#if'. Identifiers that are not macros, which are all considered to be the number zero. This allows you to write #if MACRO instead of #ifdef MACRO, if you know that MACRO, when defined, will always have a nonzero value. Function-like macros used without their function call parentheses are also treated as zero. In some contexts this shortcut is undesirable. The '-Wundef' option causes GCC to warn whenever it encounters an identifier which is not a macro in an '#if'.
The preprocessor does not know anything about types in the language. Therefore, sizeof operators are not recognized in '#if', and neither are enum constants. They will be taken as identifiers which are not macros, and replaced by zero. In the case of sizeof, this is likely to cause the expression to be invalid.
The preprocessor calculates the value of expression. It carries out all calculations in the widest integer type known to the compiler; on most machines supported by GCC this is 64 bits. This is not the same rule as the compiler uses to calculate the value of a constant expression, and may give different results in some cases. If the value comes out to be nonzero, the '#if' succeeds and the controlled text is included; otherwise it is skipped.
If expression is not correctly formed, GCC issues an error and treats the conditional as having failed.
I'm sorry I did not notice the detailed information in I posted gcc document.
Therefore, sizeof operators are not recognized in '#if', and neither are enum constants. They will be taken as identifiers which are not macros, and replaced by zero.
So enum constants can not be used in #if directive.
I wonder if there is any other better ways to realize what I want except defining a macro constant?
Look at what situations in your code compilation that would CAUSE 'an' to be zero?
Or just see if the optimizer will just jettison code in paths that are "never true"
The "an" can only be larger than zero if you have any other enum value before.
So it feels more like you should write the code:
#if HAVE_FEATURE_XX enum { a0,a1,...,an } #endif // HAVE_FEATURE_XX . . . #if HAVE_FEATURE_XX for (unsigned i = 0; i < an; ++i) { do_magic(i); } #endif // HAVE_FEATURE_XX
Because as I read it, "an" is intended to be the number of enumerated values, in which case the whole enumerator becomes irrelevant if you haven't any enumerated values to care about.
I defined four parameter groups of Modbus: coils, discrete inputs, holding registers, input registers. There is master and slave in system, they have different parameters, so there may be some groups have no parameter in slave, but they do have parameters in master.In order for consistence, I defined four groups enum type, the "an" is the count of parameters, then I originally want to use it for conditional compilation, but obviously KEIL (gcc) does not support it.
@Per Westermark
Yes, you are right, I use "an" as the number of enum parameters, and I did like you did in my project and there was no problem.
My problem is whatever "an" is, the conditional compilation always thinks an=0, so statements does not compiled, because gcc does not support enum type in conditional compilation directives.
Preprocessor stuff occurs BEFORE the compiler processes the source, it's not a KEIL or GCC thing.
Use a regular 'if' construct and the let the optimizer deal with the 'if (1 == 2)' paths that are never taken.
because gcc does not support enum type in conditional compilation directives.
but obviously KEIL (gcc) does not support it.
I said before:
That won't work with any c compiler.
If you find a compiler that supports it, then you should not use it; because it won't comply with the standard(s) expected of a C compiler.
As I said before:
If you find a compiler that does do it, then the best thing to do is discard it; because it wouldn't be compliant with the standards.