Preprocessor Bug Ignores #ifndef - Breaks Blink Example · Issue #933

What kind of issue is this?

  • PlatformIO Core. If you’ve found a bug, please provide an information below.

Configuration

Windows 10

PlatformIO Version (platformio --version): PlatformIO, version 3.4.0a4

Description of problem

Summary: An #ifndef preprocessor conditional is ignored, causing the conditionally included statements to be compiled despite the fact that the symbol is already defined.

Details: I was testing my new installation of PlatformIO IDE, and followed the steps to build the Blink example. I was astonished to find that it didn't work. What I found was the following code ignored the definition of LED_BUILTIN (found in pins_arduino.h, included by Arduino.h and set to 16) and instead redefined it to 13.

#include "Arduino.h" // #includes "pins_arduino.h", which #defines LED_BUILTIN 16 #ifndef LED_BUILTIN #define LED_BUILTIN 13 // shouldn't get processed since LED_BUILTIN already defined #endif

This code caused GPIO 13 to be toggled - which is incorrect, since the LED_BUILTIN value is 16. Changing the code to comment out the #define corrects the issue:

#ifndef LED_BUILTIN //#define LED_BUILTIN 13 #endif

Steps to Reproduce

  1. Build and install the source code below to a NodeMCU V1.0. Note serial output and behavior of built in LED.
  2. Comment out the LED_BUILTIN definition and again note serial output and built-in LED behavior.
#ifndef LED_BUILTIN //#define LED_BUILTIN 13 #endif

Actual Results

Step 1 Serial output: LED_BUILTIN = 13 Built in LED does not blink (but GPIO 13 toggles) Step 2 Serial output: LED_BUILTIN = 16 Built in LED blinks

Note that if LED_BUILTIN was truly undefined (as indicated by the results of Step 1), there should have been a compile failure due to the symbol not being found.

Expected Results

Step 1 Serial output: LED_BUILTIN = 16 Built in LED blinks Step 2 Serial output: LED_BUILTIN = 16 Built in LED blinks

If problems with PlatformIO Build System:

The content of platformio.ini:

[env:nodemcuv2] platform = espressif8266 board = nodemcuv2 framework = arduino upload_speed = 460800

Source file to reproduce issue:

#include "Arduino.h" #ifndef LED_BUILTIN #define LED_BUILTIN 13 #endif void setup() { // initialize LED digital pin as an output. pinMode(LED_BUILTIN, OUTPUT); Serial.begin(115200); } void loop() { // turn the LED on (HIGH is the voltage level) digitalWrite(LED_BUILTIN, HIGH); Serial.print("LED_BUILTIN = "); Serial.println(LED_BUILTIN); // wait for a second delay(1000); // turn the LED off by making the voltage LOW digitalWrite(LED_BUILTIN, LOW); // wait for a second delay(1000); }

Additional info

Tag » Arduino Ifndef Example