Go Down

Topic: Functions below code suddenly not declared (Read 7874 times) previous topic - next topic

Fruchti

Hi,

Code: [Select]
#include <ESP8266WiFi.h>

#define CSN_PIN 4 // GPIO 04 (actually labled GPIO 05 on some ESP-12 Boards)

void setup() {
  pinMode(CSN_PIN,OUTPUT);
  CSN(HIGH);
}

void loop() {
}

void CSN(int s) {
  digitalWrite(CSN_PIN,s);
}


Won't compile with error:
Code: [Select]
d:\Dokumente\Arduino\esp8266_nrf24l01_hub\esp8266_nrf24l01_hub.ino: In function 'void setup()':

esp8266_nrf24l01_hub:7: error: 'CSN' was not declared in this scope

   CSN(HIGH);

           ^

exit status 1
'CSN' was not declared in this scope


This code does compile (include removed):
Code: [Select]
//#include <ESP8266WiFi.h>

#define CSN_PIN 4 // GPIO 04 (actually labled GPIO 05 on some ESP-12 Boards)

void setup() {
  pinMode(CSN_PIN,OUTPUT);
  CSN(HIGH);
}

void loop() {
}

void CSN(int s) {
  digitalWrite(CSN_PIN,s);
}


this also (include present, declaration order changed):
Code: [Select]
#include <ESP8266WiFi.h>

#define CSN_PIN 4 // GPIO 04 (actually labled GPIO 05 on some ESP-12 Boards)

void CSN(int s) {
  digitalWrite(CSN_PIN,s);
}

void setup() {
  pinMode(CSN_PIN,OUTPUT);
  CSN(HIGH);
}

void loop() {
}


I don't get it... If ESP8266WiFi.h is included, CSN() is only declared when it is declared before setup?!

Using Arduino IDE 1.6.7, Board is generic ESP8266 Module...

The same project was compiling fine last week?! I don't know what I may have changed...

el_supremo

That's a 'feature' of the way the IDE preprocessor works.
Add this line at the beginning of your sketch to force the IDE to put all the function definitions at the top where they'll do some good:
Code: [Select]
char junk;

Or you could just move the CSN function in front of setup() as you did in your 4th code example. In general, if function A() calls function B(), put function B() ahead of function A() in the code.

Pete
Don't send me technical questions via Private Message.

BulldogLowell

you could also just try putting the function prototype in the header portion of the *.ino like this:

Code: [Select]
#include <ESP8266WiFi.h>

#define CSN_PIN 4 // GPIO 04 (actually labled GPIO 05 on some ESP-12 Boards)

//function prototypes
void CSN(int s);
// end function prototypes

void setup() {
  pinMode(CSN_PIN,OUTPUT);
  CSN(HIGH);
}

void loop() {
}

void CSN(int s)
{
  digitalWrite(CSN_PIN,s);
}


The IDE does that for you... but it can get confused from time to time.

I would watch out for creating functions variables with all caps like this:

Code: [Select]
void CSN(int s)

You never know, unless you exhaustively review all of the libraries you are using, if CSN in this case already has a definition.

So try using the regular CamelCase naming convention for functions and variables to try to avoid name collisions.

Fruchti

So... looks like the ESP library is confusing the preprocessor?

This fails too:
Code: [Select]
#include "SPI.h"
int i=42;
#include <ESP8266WiFi.h>
void setup() {
  myCsn(HIGH);
}
void loop() {
}
void myCsn(int s) {
}


This compiles:
Code: [Select]
#include "SPI.h"
int i=42;
//#include <ESP8266WiFi.h>
void setup() {
  myCsn(HIGH);
}
void loop() {
}
void myCsn(int s) {
}


Still wondering what might have changed, I'm working on this project (with los more code in it :D) for weeks now...

GoofBall2

Is there a solution for this?  Having the same problem, ESP8266WiFi.h  causes other functions to not be declared.  In my case, the MIN() function dies:

'min' was not declared in this scope


commenting out:     //#include "ESP8266WiFi.h"    compiles OK,

But leaving it in:   #include "ESP8266WiFi.h"    causes 'min' was not declared in this scope.


IDE 1.6.7, and 1.6.8 Jan27 hourly both suffer from same problem.

AWOL

Quote
MIN() function dies:

'min' was not declared in this scope
Big clue there.
"Pete, it's a fool (who) looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.


el_supremo

@Fruchti: I did say "at the beginning" of the file. Not "near" the beginning.

Change it to:
Code: [Select]
int i=42;
#include "SPI.h"
#include <ESP8266WiFi.h>

and try again.

Pete
Don't send me technical questions via Private Message.

GoofBall2

@Fruchti: I did say "at the beginning" of the file. Not "near" the beginning.

Change it to:
Code: [Select]
int i=42;
#include "SPI.h"
#include <ESP8266WiFi.h>

and try again.

Pete
int i = 46;  as Line#1 makes no difference for me (1.6.8Jan27):   'min' was not declared in this scope.  :(

KeithRB


GoofBall2

C is case sensitive. min is not MIN.
Yes, I understand.  I mis-typed above.   That is not the issue.

nickgammon

Please post technical questions on the forum, not by personal message. Thanks!

More info: http://www.gammon.com.au/electronics

terryking228

Quote
How to avoid the quirks of the IDE sketch file pre-preprocessing
Hi Nick,

As usual you are the Deep Detective on this stuff... 

I have a similar? problem with 1.6.6 and 1.6.7 ONLY (1.6.5 and earlier OK)...
Is the situation you show on your site version specific?? 

Any of the examples on the SM lib page fail on 1.6.6..1.6.7 
http://playground.arduino.cc/Code/SMlib

Is this the same problem??

Thanks! Terry
Regards, Terry King terry@yourduino.com  - Check great prices, devices and Arduino-related boards at http://YourDuino.com
HOW-TO: http://ArduinoInfo.Info

nickgammon

#13
Feb 02, 2016, 10:23 pm Last Edit: Feb 02, 2016, 10:23 pm by Nick Gammon
It looks like IDE 1.6.7 is not generating the prototypes. To fix that you have to do that yourself. In the case of concurrent_states sketch I added these lines at the top of the sketch (after the include):

Code: [Select]
// function prototypes
State m1s1h();
State m1s1b();
State m2s1h();
State m2s1b();
State m3s1h();
State m3s1b();
State On();


Then it compiles OK. It is probably good to program in the "proper" C++ way and declare your own function prototypes rather than relying on the (crutch of the) IDE doing it for you.

Or, move the functions around so they are defined before they are used.
Please post technical questions on the forum, not by personal message. Thanks!

More info: http://www.gammon.com.au/electronics

Go Up