-->
Page 1 of 1

Including ESP8266WiFi.h forces use of function declarations?

PostPosted: Thu Jan 14, 2016 12:23 am
by rocket_man
I'd like to preface this by saying I'm new to C++ and ESP8266 programming, so please don't laugh too hard if it's a stupid question :D

I noticed that when I include the ESP8266WiFi.h in a sketch, it forces the use of function declarations, otherwise I get a "not declared in this scope" error.

Here's a minimum code example:

This compiles just fine
Code: Select all#include <Ethernet.h>

void setup() {
  Serial.begin(9600);
  testFunction();
}

void loop() {
}

void testFunction()
{
  Serial.println("test");
}


This on the other hand, gives a compilation error complaining that testFunction was not declared in the scope
(error: 'testFunction' was not declared in this scope)
Code: Select all#include <ESP8266WiFi.h>

void setup() {
  Serial.begin(9600);
  testFunction();
}

void loop() {
}

void testFunction()
{
  Serial.println("test");
}


Therefore, if I include the ESP8266WiFi.h I need to add the function declaration "void testFunction();" somewhere before the setup() function.

So the question is why does including the ESP8266WiFi.h have this effect?

Thanks!

Re: Including ESP8266WiFi.h forces use of function declarati

PostPosted: Thu Jan 14, 2016 2:13 am
by WereCatf
I can't say why including ESP8266WiFi.h does that, but having to either declare a function or having the function-code itself appear before it's called is ANSI-standard C++ and thus it's a good habit to learn anyways -- if you ever decide to code in C/C++ in a non-Arduino environment you'd run into the exact same thing. In your example, either move the testFunction() itself above setup() or, indeed, declare the function there first, and just learn to live with it :)

Re: Including ESP8266WiFi.h forces use of function declarati

PostPosted: Thu Jan 14, 2016 10:16 am
by jmehan
I get this problem too but only with the latest Arduino IDE - 1.6.7.
Apparently they did some work on their prototype generation code in 1.6.7 which is now causing us issues.

Re: Including ESP8266WiFi.h forces use of function declarati

PostPosted: Thu Jan 14, 2016 11:32 pm
by rocket_man
Ah, I see - thank you both for the info. As WereCatf suggested, I'll just stick to declaring functions as the C++ standards stipulate. That way the code will work no matter what happens with the Arduino IDE, and I'll learn to use good C++ coding practices.

As jmehan mentioned, the change is indeed causing problems. The way I came across the problem is by trying to use an example ESP8266 sketch from somebody's github and it wouldn't compile. Same thing - errors about functions not being declared in this scope fixed with function declarations.