Preprocessor Bug Ignores #ifndef - Breaks Blink Example · Issue #933
Maybe your like
-
Uh oh!
There was an error while loading. Please reload this page.
- Notifications You must be signed in to change notification settings
- Fork 856
- Star 8.9k
- Code
- Issues 274
- Pull requests 15
- Actions
- Security 0
- Insights
Description
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 #endifThis 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 #endifSteps to Reproduce
- Build and install the source code below to a NodeMCU V1.0. Note serial output and behavior of built in LED.
- Comment out the LED_BUILTIN definition and again note serial output and built-in LED behavior.
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 = 460800Source 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
Metadata
Metadata
Assignees
No one assignedLabels
Type
No typeProjects
No projectsMilestone
No milestoneRelationships
None yetDevelopment
No branches or pull requestsIssue actions
You can’t perform that action at this time.Tag » Arduino Ifndef Example
-
Arduino Preprocessor Directives Tutorial - Ifdef & Endif - Device Plus
-
Arduino #ifndef Compiler Directive Issues - Programming Questions
-
Purpose Of #ifndef And #define? - Help & Information - Arduboy
-
Arduino IDE #ifdef
-
8.2 Conditional Compilation (#if, #ifdef, #ifndef, #else, #elif, #endif ...
-
#ifdef - #ifndef - #elseif - #elif - #else - #endif
-
Confusing Nesting Of Numerous #ifndef Statements (with No Values ...
-
Chapitre 5 : Les Directives Au Préprocesseur
-
#ifndef - YouTube
-
What Are #ifndef And #define Used For In C++?
-
Problems With Library, #include And #ifdef - PlatformIO Community
-
Compilation Conditionnelle
-
Conditional Inclusion
-
#ifndef - C And C++ Syntax Reference