-1

I am having an issue with mDNS when it is not a part of the standard setup() loop() constructs. I could just leave it in main, however I have other processing I am wanting to do, and leaving it in there is messy and violates the "one purpose, one file" paradigm. Plus, this is driving me crazy.

The following will compile and run with no errors:

main.h

#ifndef MAIN_H
#define MAIN_H

#include <WiFiManager.h>
#include <ESP8266mDNS.h>

void setup();
void loop();
void mdnssetup();

#endif // MAIN_H

main.cpp:

#include "main.h"

void setup() {
    WiFiManager wifiManager;
    mdnssetup();
}

void loop() {
    MDNS.update();
    yield();
}

void mdnssetup() {
    MDNS.begin(WiFi.hostname());
    MDNS.addService("http", "tcp", 80);
}

Whenever I pull the function to a separate file I get errors. The following configuration (where I separate the MDNS setup) generates errors:

main.h:

#ifndef MAIN_H
#define MAIN_H

#include "mdns.h"
#include <WiFiManager.h>

void setup();
void loop();

#endif // MAIN_H

main.cpp:

#include "main.h"

void setup() {
    WiFiManager wifiManager;
    mdnssetup();
}

void loop() {
    MDNS.update();
    yield();
}

mdns.h:

#ifndef MDNS_H
#define MDNS_H

#include <ESP8266mDNS.h>

void mdnssetup();

#endif // MDNS_H

mdns.cpp

#include "mdns.h"

void mdnssetup() {
    MDNS.begin(WiFi.hostname());
    MDNS.addService("http", "tcp", 80);
}

The following are the errors I receive in the latter setup:

In file included from src\mdns.h:4:0,
                 from src\main.h:4,
                 from src\main.cpp:1:
C:\Users\foo\.platformio\packages\framework-arduinoespressif8266\libraries\ESP8266mDNS\src/ESP8266mDNS.h:52:27: error: expected type-specifier
     using MDNSResponder = esp8266::MDNSImplementation::MDNSResponder; //new
                           ^
C:\Users\foo\.platformio\packages\framework-arduinoespressif8266\libraries\ESP8266mDNS\src/ESP8266mDNS.h:54:12: error: 'MDNSResponder' does not name a type
     extern MDNSResponder MDNS;
            ^
src\main.cpp: In function 'void loop()':
src\main.cpp:9:5: error: 'MDNS' was not declared in this scope
     MDNS.update();
     ^
*** [.pio\build\d1_mini\src\main.cpp.o] Error 1
In file included from src\mdns.h:4:0,
                 from src\mdns.cpp:1:
C:\Users\foo\.platformio\packages\framework-arduinoespressif8266\libraries\ESP8266mDNS\src/ESP8266mDNS.h:52:27: error: expected type-specifier
     using MDNSResponder = esp8266::MDNSImplementation::MDNSResponder; //new
                           ^
C:\Users\foo\.platformio\packages\framework-arduinoespressif8266\libraries\ESP8266mDNS\src/ESP8266mDNS.h:54:12: error: 'MDNSResponder' does not name a type
     extern MDNSResponder MDNS;
            ^
src\mdns.cpp: In function 'void mdnssetup()':
src\mdns.cpp:4:5: error: 'MDNS' was not declared in this scope
     MDNS.begin(WiFi.hostname());
     ^
*** [.pio\build\d1_mini\src\mdns.cpp.o] Error 1

Obviously, I can read the errors. I have a faint understanding of what this is telling me in general. However, applying what it's telling me is beyond my comprehension. In other words, please use small words and please don't cut and paste the errors as if that's the solution as I've seen in some of my searches.

LCB
  • 91
  • 1
  • 11

2 Answers2

3

One of the headers you are using #include <ESP8266mDNS.h> includes #include "LEAmDNS.h" (see here), which in turn uses the same include guard as you (see here)

So to fix it change your include guard in mdns.h to something different than:

#ifndef MDNS_H
#define MDNS_H

Or use #pragma once if supported.

mortenvp
  • 1,095
  • 1
  • 7
  • 13
  • I'm gonna slap myself in the forehead (and drink a beer to your name) if that's it! – LCB Aug 05 '19 at 21:48
  • Yeah ... I'm an idiot and you schooled me.Jeremy pointed me in the right direction, for sure, but it would have taken me forever to figure out why. I'll add that to my bag of tricks though. Thanks @mortenvp, you saved me a ton of digging. You do not want to know how long I have been banging my head against this. And thank you Jeremy for the long-term view. – LCB Aug 05 '19 at 22:14
1

Often it's best to only pay attention to the first compiler-error generated, since the latter ones are often just follow-on effects of the compiler getting confused by the first error. So:

 C:\Users\foo\.platformio\packages\framework-arduinoespressif8266\libraries\ESP8266mDNS\src/ESP8266mDNS.h:52:27: error: expected type-specifier
 using MDNSResponder = esp8266::MDNSImplementation::MDNSResponder; //new
                       ^

.... here we see that the compiler came across the identifier esp8266 and doesn't know what it is. In fact it is (very likely) a namespace qualifier, but it seems that nothing has told the compiler about that namespace. I suggest looking through the header files to see if you can find the spot where namespace esp8266 is declared, and if so, maybe you can #include that header file first (i.e. near the top of your mdns.h file) and see if that helps the compiler along.

Another fairly likely scenario (assuming that the mdns.h file you listed is a file you created, and not a system-supplied file) is that your mdns.h file has the same exact name as a system-supplied mdns.h file, and so when the system-supplied headers try to #include "mdns.h" they end up getting your file instead of the one they expected to get, and that's why they don't pull in the namespace-definition that they depend on. If so, the easy fix is to rename your mdns.h file to e.g. my_mdns.h or something else that is spelled differently than any system-supplied include files. (And probably rename your mdns.cpp file to my_mdns.cpp file too, just to be consistent)

Jeremy Friesner
  • 70,199
  • 15
  • 131
  • 234
  • Not sure who marked me down, aside from me not being as smart as everyone else (apparently), I think it's a fairly thorough question that meets the rules. Thanks Jeremy, I'll give those ideas a look. – LCB Aug 05 '19 at 21:37
  • Thanks Jeremy. The solution I chose was the exact reason why I was hitting it - but your careful review gives me a lot to think about going forward when I hit these issues. – LCB Aug 05 '19 at 22:15