Use headers give me an error

Hello,

When I try to use a header, #ifdef doesn’t work properly, the code inside is gray out, if I call de function, I get an error. the identifier "blink" is not defined.

If I remove #ifdef, I get a new error when I try to compile, undefined reference to 'blink()'.
My headers are in include folder.

header.h

#ifdef HEADER_H
#define HEADER_H
#pragma once
#include <Arduino.h>
void blink();
#endif

header.cpp

#include "header.h"
void blink(){
//code here
}
#endif

main.cpp

#include <Arduino.h>
#include "header.h"
void setup(){
blink();
}
void loop(){
//code here
}

in your file header.h

must be

#ifndef HEADER_H

and delete

in your file header.cpp

2 Likes

This was a mistake when I redacted this.

Now my If it works, but I continue with the same problem undefined reference to 'blink()'
Look the complete message:

c:/users/<myuser>/.platformio/packages/toolchain-xtensa/bin/../lib/gcc/xtensa-lx106-elf/4.8.2/../../../../xtensa-lx106-elf/bin/ld.exe: .pio\build\esp12e\src\main.cpp.o:(.text._Z9reconnectv+0x38): undefined reference to `wificonfig(char const*, char const*)'
c:/users/<myuser>/.platformio/packages/toolchain-xtensa/bin/../lib/gcc/xtensa-lx106-elf/4.8.2/../../../../xtensa-lx106-elf/bin/ld.exe: .pio\build\esp12e\src\main.cpp.o:(.text._Z9reconnectv+0x4d): undefined reference to `wificonfig(char const*, char const*)'
collect2.exe: error: ld returned 1 exit status
*** [.pio\build\esp12e\firmware.elf] Error 1

Your error message and description don’t match.

What’s the complete code and platformio.ini with which your problem can be reproduced?

1 Like

I get the same error from two different headers.

Is just init the wifi settings on esp, and put the code in header for use in other projects, I just call the function wificonfig in the setup with the SSID and Password.
Here is the code:

wificonfig.h

#ifndef wificonfig_h
#define wificonfig_h
    #pragma once
    #include <Arduino.h>
    void wificonfig(const char *SSID, const char *PSK);
#endif

wificonfig.cpp

#include "wificonfig.h"
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
void wificonfig(const char *SSID, const char *PSK){
    WiFiClient client;
    ESP8266WiFiMulti WiFiMulti;
    while (1){
        WiFi.mode(WIFI_STA);
        //WiFi.config(staticIP, subnet, gateway, dns);
        WiFiMulti.addAP(SSID, PSK);
        //WiFi.begin(APSSID,APPSK);
        if (WiFi.status() == WL_IDLE_STATUS) { // when not connected to a network, but powered on
            digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
            Serial.println("WL_IDLE_STATUS");
        }
        else if (WiFi.status() == WL_CONNECTED) {
            digitalWrite(LED_BUILTIN, LOW);
            Serial.print("WL_CONNECTED ");
            Serial.println(WiFi.localIP());
            break;
        }
        else if (WiFi.status() == WL_NO_SSID_AVAIL) {
            digitalWrite(LED_BUILTIN, HIGH);
            Serial.println("WL_NO_SSID_AVAIL");
        }
        else if (WiFi.status() == WL_CONNECT_FAILED) {
            digitalWrite(LED_BUILTIN, HIGH);
            Serial.println("WL_CONNECT_FAILED");
        }
        else if (WiFi.status() == WL_CONNECTION_LOST) {
            digitalWrite(LED_BUILTIN, HIGH);
            Serial.println("WL_CONNECTION_LOST");
        }
        else if (WiFi.status() == WL_DISCONNECTED) {
            digitalWrite(LED_BUILTIN, HIGH);
            Serial.println("WL_DISCONNECTED");
        }
        else {
            digitalWrite(LED_BUILTIN, LOW);
        }
    Serial.println("Attempting again...");
    delay(3000);
    }
}

platfiomio.ini

[env:esp12e]
platform = espressif8266
board = esp12e
framework = arduino
build_flags =
    -D PIO_FRAMEWORK_ARDUINO_LWIP2_LOW_MEMORY ;No sé bien que hace esto, solo se que ayuda a reservar espacio para el update.
monitor_speed = 115200
lib_deps =
    PubSubClient
    arduino-timer@^2.0.1
    ArduinoJson@^6.15.2
    arduino-timer@^2.0.1
    Time

Where did you place the cpp-files corresponding to the header-files?

I assume that you have placed them in include:

If your project looks like that:

include
-> header.h
header.cpp

src
-> main.cpp

/home/hst/.platformio/packages/toolchain-xtensa/bin/../lib/gcc/xtensa-lx106-elf/4.8.2/../../../../xtensa-lx106-elf/bin/ld: .pio/build/d1_mini/src/main.cpp.o:(.text.setup+0x0): undefined reference to `blink()'
/home/hst/.platformio/packages/toolchain-xtensa/bin/../lib/gcc/xtensa-lx106-elf/4.8.2/../../../../xtensa-lx106-elf/bin/ld: .pio/build/d1_mini/src/main.cpp.o: in function `setup':
main.cpp:(.text.setup+0xa): undefined reference to `blink()'

pio run ends with the error message described by you.

Instead if your project looks like that:

include
-> header.h

src
-> main.cpp
header.cpp

Advanced Memory Usage is available via "PlatformIO Home > Project Inspect"
RAM:   [===       ]  32.7% (used 26776 bytes from 81920 bytes)
Flash: [==        ]  24.6% (used 256584 bytes from 1044464 bytes)
Creating BIN file ".pio/build/d1_mini/firmware.bin" using ".pio/build/d1_mini/firmware.elf"
=============================================================== [SUCCESS] Took 4.83 seconds ==============================================================

everything should work fine :wink:

1 Like

Thanks, the problem was the location of .cpp files. Now its work. :grin: