c++arduinoesp8266wifimanager

ESP8266 Async WiFiManager Linking Error using library in seperate file


I'm merging from the regular WiFiManager lib to the ESPAsync_WiFiManager by khoih-prog. Github

However this gives me some Linking Issues. As provided in the Readme I used ESPAsync_WiFiManager.hpp instead of the regular .h. This resolves most linking errors, however some remain unresolved:

Linking .pio\build\nodemcu\firmware.elf
c:/users/christopher/.platformio/packages/toolchain-xtensa/bin/../lib/gcc/xtensa-lx106-elf/10.3.0/../../../../xtensa-lx106-elf/bin/ld.exe: .pio\build\nodemcu\lib244\libAlkomat.a(wifi.cpp.o):(.text._ZN14WiFiManagement8initWifiEv+0x8): undefined reference to `_ZN20ESPAsync_WiFiManagerC1EP14AsyncWebServerP9DNSServerPKc'
c:/users/christopher/.platformio/packages/toolchain-xtensa/bin/../lib/gcc/xtensa-lx106-elf/10.3.0/../../../../xtensa-lx106-elf/bin/ld.exe: .pio\build\nodemcu\lib244\libAlkomat.a(wifi.cpp.o):(.text._ZN14WiFiManagement8initWifiEv+0xc): undefined reference to `_ZN20ESPAsync_WiFiManager11autoConnectEPKcS1_'
c:/users/christopher/.platformio/packages/toolchain-xtensa/bin/../lib/gcc/xtensa-lx106-elf/10.3.0/../../../../xtensa-lx106-elf/bin/ld.exe: .pio\build\nodemcu\lib244\libAlkomat.a(wifi.cpp.o):(.text._ZN14WiFiManagement8initWifiEv+0x10): undefined reference to `_ZN20ESPAsync_WiFiManagerD1Ev'
c:/users/christopher/.platformio/packages/toolchain-xtensa/bin/../lib/gcc/xtensa-lx106-elf/10.3.0/../../../../xtensa-lx106-elf/bin/ld.exe: .pio\build\nodemcu\lib244\libAlkomat.a(wifi.cpp.o): in function `_ZN14WiFiManagement8initWifiEv':
wifi.cpp:(.text._ZN14WiFiManagement8initWifiEv+0x3e): undefined reference to `_ZN20ESPAsync_WiFiManagerC1EP14AsyncWebServerP9DNSServerPKc'
c:/users/christopher/.platformio/packages/toolchain-xtensa/bin/../lib/gcc/xtensa-lx106-elf/10.3.0/../../../../xtensa-lx106-elf/bin/ld.exe: wifi.cpp:(.text._ZN14WiFiManagement8initWifiEv+0x4b): undefined reference to `_ZN20ESPAsync_WiFiManager11autoConnectEPKcS1_'
c:/users/christopher/.platformio/packages/toolchain-xtensa/bin/../lib/gcc/xtensa-lx106-elf/10.3.0/../../../../xtensa-lx106-elf/bin/ld.exe: wifi.cpp:(.text._ZN14WiFiManagement8initWifiEv+0x53): undefined reference to `_ZN20ESPAsync_WiFiManagerD1Ev'
collect2.exe: error: ld returned 1 exit status
*** [.pio\build\nodemcu\firmware.elf] Error 1

I think the issue is that I'm using the library in a different File than the main.cpp:

main.cpp:

#include <Arduino.h>
#include "wifi.h"


void setup()
{
    Serial.begin(115200);
    WiFiManagement::initWifi();
}

void loop()
{

}

wifi.h

#pragma once

#include <ESPAsync_WiFiManager.hpp>               //https://github.com/khoih-prog/ESPAsync_WiFiManager
 


namespace WiFiManagement 
{
    void initWifi();
}

wifi.cpp

#include "wifi.h"


using namespace WiFiManagement;


void WiFiManagement::initWifi(){
    AsyncWebServer  _ws(HTTP_PORT);
    DNSServer       _dnss;
    ESPAsync_WiFiManager wifiManager(&_ws, &_dnss);
    wifiManager.autoConnect(AP_NAME);
}

In a clean project using the library in the main file, it compiles fine. What am I doing wrong here?

Edit: (Standard) PlatformIO build command:

xtensa-lx106-elf-g++ -o .pio\build\nodemcuv2\firmware.elf -T eagle.flash.4m1m.ld -Os -nostdlib -Wl,--no-check-sections -Wl,-static -Wl,--gc-sections -Wl,-wrap,system_restart_local -Wl,-wrap,spi_flash_read -u app_entry -u _printf_float -u _scanf_float -u _DebugExceptionVector -u _DoubleExceptionVector -u _KernelExceptionVector -u _NMIExceptionVector -u _UserExceptionVector .pio\build\nodemcuv2\src\main.cpp.o .pio\build\nodemcuv2\src\network.cpp.o -L.pio\build\nodemcuv2 -L.pio\build\nodemcuv2\ld -LC:\Users\Christopher\.platformio\packages\framework-arduinoespressif8266\tools\sdk\lib -LC:\Users\Christopher\.platformio\packages\framework-arduinoespressif8266\tools\sdk\ld -LC:\Users\Christopher\.platformio\packages\framework-arduinoespressif8266\tools\sdk\lib\NONOSDK22x_190703 -Wl,--start-group .pio\build\nodemcuv2\lib622\libESPAsyncTCP.a .pio\build\nodemcuv2\lib77a\libHash.a .pio\build\nodemcuv2\libba0\libESP8266WiFi.a ".pio\build\nodemcuv2\libc45\libESP Async WebServer.a" .pio\build\nodemcuv2\lib220\libDNSServer.a .pio\build\nodemcuv2\libFrameworkArduinoVariant.a .pio\build\nodemcuv2\libFrameworkArduino.a -lhal -lphy -lpp -lnet80211 -lwpa -lcrypto -lmain -lwps -lbearssl -lespnow -lsmartconfig -lairkiss -lwpa2 -lm -lc -lgcc -llwip2-536-feat -lstdc++ -Wl,--end-group

Solution

  • You need to include the implementation h file into wifi.cpp to solve the undefined reference compile error. The hpp is just the definition.

    wifi.cpp

    #include <ESPAsync_WiFiManager.h>               //https://github.com/khoih-prog/ESPAsync_WiFiManager
    
    #include "wifi.h"
    
    using namespace WiFiManagement;
    
    #define HTTP_PORT       80
    #define AP_NAME         "AP_NAME"
    
    void WiFiManagement::initWifi(){
        AsyncWebServer  _ws(HTTP_PORT);
        DNSServer       _dnss;
        ESPAsync_WiFiManager wifiManager(&_ws, &_dnss);
        wifiManager.autoConnect(AP_NAME);
    }