Skip to content
Advertisement

BUILD_BUG_ON_ZERO not working in a simple user space application

I tried to use BUILD_BUG_ON_ZERO in a simple user space application and it is failed to compile

#include <stdio.h>

#define BUILD_BUG_ON_ZERO(e) ((int)(sizeof(struct { int:(-!!(e)); }))) 
int main()
{
    int i;
    BUILD_BUG_ON_ZERO(i);
    return 0;
}

error: bit-field ‘<anonymous>’ width not an integer constant
 #define BUILD_BUG_ON_ZERO(e) ((int)(sizeof(struct { int:(-!!(e)); })))

Can anyone please provide me hints on the error.

Advertisement

Answer

The macro BUILD_BUG_ON_ZERO is intended to be used with a constant expression as defined in 6.6 constant expression.

i isn’t a constant expression – it’s just a local variable. Even const qualified objects are not considered “constant expressions” in C (unlike C++). So you’d have to use:

  • literal values (such as 0) or,
  • a macro (#define value 0) or,
  • use an enum constant enum { value = 0,};)

to get a constant expression

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement