I'm trying to compile linux kernel verison 4.14.41 and got the following error:

./arch/x86/include/asm/acpi.h: In function ‘void arch_acpi_set_pdc_bits(u32*)’:
./include/linux/build_bug.h:30:45: error: types may not be defined in ‘sizeof’ expressions
 #define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:(-!!(e)); }))
./arch/x86/include/asm/required-features.h:110:29: note: in expansion of macro ‘BUILD_BUG_ON_ZERO’
./arch/x86/include/asm/cpufeature.h:84:5: note: in expansion of macro ‘REQUIRED_MASK_CHECK’
     REQUIRED_MASK_CHECK       || \
./arch/x86/include/asm/cpufeature.h:111:32: note: in expansion of macro ‘REQUIRED_MASK_BIT_SET’
  (__builtin_constant_p(bit) && REQUIRED_MASK_BIT_SET(bit) ? 1 : \
./arch/x86/include/asm/acpi.h:118:6: note: in expansion of macro ‘cpu_has’
  if (cpu_has(c, X86_FEATURE_EST))

I would like to know what the error means and what does sizeof(struct { int:(-!!(e)); }) returns. Is there any simpler alternative for the same.

  • What compiler are you using? – ilkkachu Jun 11 '18 at 17:23
  • I am adding c++ run time support to the library. So I'm using both g++ and gcc. This error occurred as a libsupc++ file has included the header file ./arch/x86/include/asm/acpi.h. The version of the compiler is 4.5 – Kommuru jai shankar reddy Jun 11 '18 at 17:37

That macro tries to produce a compile error if the argument is true (non-zero). It does that by defining a struct with a bitfield whose length becomes negative if e is true.

Based on the answers on this question on Stackoverflow, the issue is just that the trickery used in the macro isn't compatible with C++, for exactly the reason stated in the error message: unlike C, C++ doesn't allow defining a struct inside sizeof.

The Linux kernel only uses C, so they probably don't care about incompatibilities with C++ in the headers used by the kernel.

The answers to the linked question contains C++ compatible alternatives to the same effect.

  • So, what could be the alternative so that I can compile with C++. Basically what does sizeof(struct { int:(-!!(e)); }) return ? Does it return the size of an int and e combined in a struct ? please correct me if I am wrong – Kommuru jai shankar reddy Jun 11 '18 at 17:56
  • @Kommurujaishankarreddy, -(!!(e)) becomes -1 if e is non-zero, producing an error. The linked question has C++-compatible alternatives, too. – ilkkachu Jun 11 '18 at 18:13
  • Can it by doing this: #define BUILD_BUG_ON_ZERO(e) if( e!=0) return -1; – Kommuru jai shankar reddy Jun 11 '18 at 18:19
  • @ikkachu Can it be done by something like this '#ifdef __cplusplus #define BUILD_BUG_ON_ZERO(e) [ e ? -1 : 1 ] #else #define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:(-!!(e)); })) #endif – Kommuru jai shankar reddy Jun 11 '18 at 18:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.