-1

The wrote the following code for avr MCU and I compiled and uploaded the code with Arduino IDE, I did not get any error and the code works but I remembered C program does not support bool data type. Will this code still be compiled with avr-gcc on Linux.


//The following code is written for ATmega328P microcontroller
// Turns ON the led on a click of a switch and Turns OFF it again another click
#include <avr/io.h>
#include <avr/interrupt.h>


#define LED_PIN   PB5
#define SW_PIN    PD2


volatile bool ledOn = false;


// Function to initialize pinmode and interrupt
void init() {
 // Set LED pin as output
  DDRB = DDRB |(1 << DDB5);
  // Set switch pin as  pull-up resistor
  PORTD = PORTD |(1 << SW_PIN);
  // Enable interrupt on the switch pin
  EIMSK = EIMSK |(1 << INT0);
  // Trigger interrupt on failing edge of the switch signal
  EICRA = (1 << ISC01);
  // Enable global interrupts
  sei();
}


// Interrupt service routine for the switch
ISR(INT0_vect) {
  // If the switch is pressed, toggle the LED state
  if ((~PIND & (1 << SW_PIN)) && !ledOn) {
   PORTB = PORTB | (1 << LED_PIN);
    ledOn = true;
  }
  else{
  PORTB = PORTB & ~(1 << LED_PIN);
    ledOn = false;
  }

}

int main(void) {
 // Initialization
  init();

 // Loop forever
 while (1) {

    // NOP
  }

  return 0;
}```
hcheung
  • 3,377
  • 3
  • 11
  • 23
  • 3
    `bool` support is not a function of the hardware but of the compiler. Nevertheless, you should include `` – Eugene Sh. Mar 28 '23 at 18:36
  • The standard C spelling for the data type is `_Bool`. However, standard C does not define any corresponding constants, so if you also want `false` and `true` to be defined for you (instead of simply using 0 and 1, respectively) then `stdbool.h` will get you those, plus a typedef for `bool`. – John Bollinger Mar 28 '23 at 18:43
  • @JohnBollinger `bool` is a preprocessor macro not a typedef. – Tom V Mar 28 '23 at 18:53
  • You are correct, @TomV, I misspoke. However, the situations in which the difference would be important are rare. – John Bollinger Mar 28 '23 at 19:03
  • 1
    Arduino is very likely using C++. Step 1: find out what compiler you are actually using. – Lundin Mar 29 '23 at 06:35
  • @JohnBollinger C23 will actually promote them to proper keywords. The macros with the same names will be deprecated, as will stdbool.h. However, I doubt the OP is using C23 but instead they are very likely using a C++ compiler. – Lundin Mar 29 '23 at 06:37
  • @hcheung Avr-libc has nothing to do with _recognition_ of `bool`. It's the compiler by its language with C++ or the inclusion of "stdbool.h" with C. – the busybee Mar 29 '23 at 10:07

2 Answers2

2

Will this code still be compiled with avr-gcc on Linux

I kind of doubt you will be able to run Linux on an AVR :)

But no it will not compile with gcc under Linux since it is invalid C. From C99 to C17 versions of the standard you need to #include <stdbool.h> to use bool. C uses the keyword _Bool and bool from stdbool.h is just a macro for that.

Upcoming C23 will remove all this nonsense and then bool, true and false will become proper language keywords. So it will actually compile on Linux if you use "gcc trunk" version and compile with -std=c2x, which is about to be formally released as gcc 13 -std=c23 later this year.

The reason why it compiles on your Arduino is because you are using a C++ compiler. That's a different programming language.

Lundin
  • 195,001
  • 40
  • 254
  • 396
  • thank you and i know i wont be able to run linux on ATmega328p.I meant will it be compiled on linux using avr-gcc, with which we compile c for avr – Jag Mohan Ray Mar 29 '23 at 18:54
  • 1
    @JagMohanRay Which host OS it runs on doesn't matter for a cross-compiler. – Lundin Mar 30 '23 at 06:44
0

The Arduino IDE uses C++ compilation - you are using C++ not C - even if it is otherwise valid C code, and you are not using the Arduino setup()/loop() framework or Arduino libraries..

With respect to C however, C99 defines a built-in type _Bool. It does that because of all the pre-C99 code that exists that might define types or macro aliases for Boolean in various ways. It prevents breaking existing code that the introduction of a new keywords bool, true and false might otherwise cause.

C99 also defines a header stdbool.h which provides a C++-like bool alias for _Bool, and symbols for true and false values. Including stdbool.h or cstdbool in C++ code has no effect.

The Arduino compiler is the same AVR GCC cross-compiler you are proposing to use on linux. It supports both C and C++ compilation. You can use the avr-g++ driver for C++ compilation, but its only difference to the avr-gcc driver is that it links C++ library support implicitly when invoking ld. Otherwise C or C++ compilation is determined by command line switch or by source file extension.

So your code will compile as-is with AVR GCC if you use the same C++ compilation that Arduino applies, but if you want to ensure it builds with either C or C++ compilation (or you just want to make it valid C code) you can simply add #include <stdbool.h> and it will work in either case.

Clifford
  • 88,407
  • 13
  • 85
  • 165