A compile time error

in udp.cpp

#include "udp.h"

WiFiUDP udp;

uint8_t UDP_Start(char* ssid, char* pass, uint16_t loc_port)
{
    uint32_t timeout = 0;
    WiFi.mode(WIFI_STA);
    WiFi.begin(ssid, pass);

    while (WiFi.status() != WL_CONNECTED) 
    {
        #if DEBUG_ENA
        Serial.print('.');
        #endif
        delay(1);
        
        timeout++;
        if (timeout >= UDP_CONNECT_TIMEOUT)
        return 0;   
    }
    
    #if DEBUG_ENA
    Serial.print("Connected! IP address: ");
    Serial.println(WiFi.localIP());
    Serial.printf("UDP server on port %d\n", loc_port);
    #endif
    udp.begin(loc_port);

    return 1;
}

in udp.h

#ifndef _UDP_H
#define _UDP_H

#include <stdint.h>

#include <ESP8266WiFi.h>
#include <WiFiUdp.h>

void UDP_Start(char* ssid, char* pass);

#endif

in main.cpp

#include "udp.h"

void setup() 
{
  // put your setup code here, to run once:
   UDP_Start("aaa", "vvv");
}

void loop() 
{
  // put your main code here, to run repeatedly:

}

I get a compile time error

c:/users/evgeny/appdata/local/arduino15/packages/esp8266/tools/xtensa-lx106-elf-gcc/3.0.3-gcc10.3-9bcba0b/bin/../lib/gcc/xtensa-lx106-elf/10.3.0/../../../../xtensa-lx106-elf/bin/ld.exe: sketch\main.ino.cpp.o:(.text.setup+0x8): undefined reference to `Z9UDP_StartPcS'

c:/users/evgeny/appdata/local/arduino15/packages/esp8266/tools/xtensa-lx106-elf-gcc/3.0.3-gcc10.3-9bcba0b/bin/../lib/gcc/xtensa-lx106-elf/10.3.0/../../../../xtensa-lx106-elf/bin/ld.exe: sketch\main.ino.cpp.o: in function `setup':

D:\Documents\AgroSensor\arduino_8266\main/main.ino:6: undefined reference to `Z9UDP_StartPcS'

collect2.exe: error: ld returned 1 exit status

exit status 1

Error compiling for board LOLIN(WEMOS) D1 mini (clone).

Without UDP_Start - it's compiled OK.

What is wrong?

I tried compiling it and got a different error:

C:\Users\per\AppData\Local\Temp\arduino_modified_sketch_191465\upd.cpp: In function 'uint8_t UDP_Start(char*, char*, uint16_t)':
upd.cpp:19:24: error: 'UDP_CONNECT_TIMEOUT' was not declared in this scope
   19 |         if (timeout >= UDP_CONNECT_TIMEOUT)
      |                        ^~~~~~~~~~~~~~~~~~~
C:\Users\per\AppData\Local\Temp\arduino_modified_sketch_191465\sketch_jul15a.ino: In function 'void setup()':
C:\Users\per\AppData\Local\Temp\arduino_modified_sketch_191465\sketch_jul15a.ino:6:14: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
    6 |    UDP_Start("aaa", "vvv");
      |              ^~~~~
C:\Users\per\AppData\Local\Temp\arduino_modified_sketch_191465\sketch_jul15a.ino:6:21: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
    6 |    UDP_Start("aaa", "vvv");
      |                     ^~~~~

Do you know why that UDP_CONNECT_TIMEOUT identifier was not declared?

Is it really a .cpp file, or is instead in a .ino file?

These are different functions because they have different arguments. One of the functions is defined, one of them is not.

If you're not modifying the ssid and pass arguments, their type should be const char *, not char *. If you do intend to modify them, then you're not allowed to pass string literals to them.

1 Like

the files are - main.ino udp.cpp udp.h

OMG. Thank you. My typo. I was baffled with - undefined reference to `Z9UDP_StartPcS'.

Usually the linker demangles the name for you. Which version of the ESP8266 Arduino Core are you using?

I don't know what Arduino Core. I ordered this one

You can go to Tools > Boards > Boards manager and look for ESP8266, it will show the version you have installed.

It's 3.0.1