Personal Library "collect2: error: ld returned 1 exit status"

Ok ok. Im making it as easy as I can.

I was replicating this

Morse::Morse(int pin)
{
  pinMode(pin, OUTPUT);
  _pin = pin;
}

and turning it into this

Morse::Morse();

I just removed just Gate::Gate(); , saved it, restarted Arduino, and compiled again. The errors are identical as last time.

What does it really mean when the error says this?

Exampe1_Gate:7:6: error: 'void Gate::setup()' cannot be overloaded with 'void Gate::setup()'
    7 | void setup(){
      |      ^~~~~

I had a Gate::setup() function but changed it to Gate::begin() because that error came up immediately afterwards. The error hasn't gone away though, although I thought I fixed it by getting rid of the "setup" function.

Everyone codes differently.
When I am working on a library, I build the .h and .cpp in the IDE with the main .ino which is simply the future example.

If you cannot get it to work in the IDE as multitab, you really are making things more difficult, IMO.

What Morse program are you writing? I am the author of Magic Morse.

Ray

Oh, I'm not writing a morse program. I got that part from the link in my original post on how to write a library. I am writing a automated gate library to go with my tutorial Automated Driveway Gates - Hackster.io
and instead of my really long code Automated-Driveway-Gate | Automated–Driveway-Gate

error: 'void Gate::setup()' cannot be overloaded with 'void Gate::setup()'

Overloading is the C++ methodology for having the compiler make a decision on what same-named function will be called by using the argument list which must be different variable type list(s).

Oh. I found something that somewhat helped:

There was an extra opening bracket after a function in the .h file on accident. I removed it and that fixed that error, but now there is an error for every function that I used from my library.

Arduino: 1.8.19 (Mac OS X), Board: "LOLIN(WEMOS) D1 R2 & mini, 80 MHz, Flash, Disabled (new aborts on oom), Disabled, All SSL ciphers (most compatible), 32KB cache + 32KB IRAM (balanced), Use pgm_read macros for IRAM/PROGMEM, 4MB (FS:2MB OTA:~1019KB), v2 Lower Memory, Disabled, None, Only Sketch, 921600"











/var/folders/2y/mn13ny595dzftc5swsy9_7940000gn/T/arduino_modified_sketch_894376/Exampe1_Gate.ino: In function 'void setup()':
Exampe1_Gate:8:10: error: request for member 'WiFiStart' in 'myGate', which is of non-class type 'Gate()'
    8 |   myGate.WiFiStart("myGateSSID", "myGatePASS");
      |          ^~~~~~~~~
Exampe1_Gate:9:10: error: request for member 'begin' in 'myGate', which is of non-class type 'Gate()'
    9 |   myGate.begin(D3,D4,D7,D8);
      |          ^~~~~
Exampe1_Gate:10:10: error: request for member 'Magnet' in 'myGate', which is of non-class type 'Gate()'
   10 |   myGate.Magnet(D5);
      |          ^~~~~~
Exampe1_Gate:11:10: error: request for member 'LEDStrip' in 'myGate', which is of non-class type 'Gate()'
   11 |   myGate.LEDStrip(D1, D2, D6);
      |          ^~~~~~~~
/var/folders/2y/mn13ny595dzftc5swsy9_7940000gn/T/arduino_modified_sketch_894376/Exampe1_Gate.ino: In function 'void loop()':
Exampe1_Gate:15:10: error: request for member 'gate' in 'myGate', which is of non-class type 'Gate()'
   15 |   myGate.gate(1);
      |          ^~~~
Exampe1_Gate:16:10: error: request for member 'waitForGate' in 'myGate', which is of non-class type 'Gate()'
   16 |   myGate.waitForGate(12,0.4,0.8);
      |          ^~~~~~~~~~~
Exampe1_Gate:17:10: error: request for member 'stop' in 'myGate', which is of non-class type 'Gate()'
   17 |   myGate.stop();
      |          ^~~~
Exampe1_Gate:18:10: error: request for member 'delayForGate' in 'myGate', which is of non-class type 'Gate()'
   18 |   myGate.delayForGate(5000);
      |          ^~~~~~~~~~~~
Exampe1_Gate:19:10: error: request for member 'gate' in 'myGate', which is of non-class type 'Gate()'
   19 |   myGate.gate(2);
      |          ^~~~
Exampe1_Gate:20:10: error: request for member 'waitForGate' in 'myGate', which is of non-class type 'Gate()'
   20 |   myGate.waitForGate(12,0.4,0.8);
      |          ^~~~~~~~~~~
Exampe1_Gate:21:10: error: request for member 'stop' in 'myGate', which is of non-class type 'Gate()'
   21 |   myGate.stop();
      |          ^~~~
Exampe1_Gate:22:10: error: request for member 'delayForGate' in 'myGate', which is of non-class type 'Gate()'
   22 |   myGate.delayForGate(5000);
      |          ^~~~~~~~~~~~
Multiple libraries were found for "WiFiUdp.h"
 Used: /Users/user/Library/Arduino15/packages/esp8266/hardware/esp8266/3.0.2/libraries/ESP8266WiFi
 Not used: /Applications/Electronics/Arduino.app/Contents/Java/libraries/WiFi
 Not used: /Users/user/Desktop/Arduino/libraries/WiFi101
exit status 1
request for member 'WiFiStart' in 'myGate', which is of non-class type 'Gate()'


This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

Thank you for explaining overloading. :grinning:

Check that you have the Constructor syntactically correct in the .cpp

Here is the C++ way that a class can be created directly in the main program:

https://www.tutorialspoint.com/cplusplus/cpp_constructor_destructor.htm

Leave the constructor in the .cpp

It could be in the main program if you were building the class in the main program, but you are wanting to write this as a library.

That syntax is incorrect. If you want a constructor with an empty body then the proper syntax is:

Morse::Morse() {}

That syntax is incorrect. It's not an instantiation of a Gate object. It's a prototype for a function that
takes no arguments and returns a Gate object. If the class's constructor takes no arguments, use:

Gate myGate;

Thank you @gfvalvo and @mrburnette

That got rid of all the errors except for a new one.

Arduino: 1.8.19 (Mac OS X), Board: "LOLIN(WEMOS) D1 R2 & mini, 80 MHz, Flash, Disabled (new aborts on oom), Disabled, All SSL ciphers (most compatible), 32KB cache + 32KB IRAM (balanced), Use pgm_read macros for IRAM/PROGMEM, 4MB (FS:2MB OTA:~1019KB), v2 Lower Memory, Disabled, None, Only Sketch, 921600"











/Users/user/Library/Arduino15/packages/esp8266/tools/xtensa-lx106-elf-gcc/3.0.4-gcc10.3-1757bed/bin/../lib/gcc/xtensa-lx106-elf/10.3.0/../../../../xtensa-lx106-elf/bin/ld: sketch/Exampe1_Gate.ino.cpp.o: in function `setup':
/Users/user/Desktop/Arduino/libraries/Gate/examples/Exampe1_Gate/Exampe1_Gate.ino:10: undefined reference to `_ZN4Gate11waitForGateEiii'
/Users/user/Library/Arduino15/packages/esp8266/tools/xtensa-lx106-elf-gcc/3.0.4-gcc10.3-1757bed/bin/../lib/gcc/xtensa-lx106-elf/10.3.0/../../../../xtensa-lx106-elf/bin/ld: sketch/Exampe1_Gate.ino.cpp.o:(.text.loop+0x1f): undefined reference to `_ZN4Gate11waitForGateEiii'
/Users/user/Library/Arduino15/packages/esp8266/tools/xtensa-lx106-elf-gcc/3.0.4-gcc10.3-1757bed/bin/../lib/gcc/xtensa-lx106-elf/10.3.0/../../../../xtensa-lx106-elf/bin/ld: sketch/Exampe1_Gate.ino.cpp.o: in function `loop':
/Users/user/Desktop/Arduino/libraries/Gate/examples/Exampe1_Gate/Exampe1_Gate.ino:16: undefined reference to `_ZN4Gate11waitForGateEiii'
collect2: error: ld returned 1 exit status
Multiple libraries were found for "WiFiUdp.h"
 Used: /Users/user/Library/Arduino15/packages/esp8266/hardware/esp8266/3.0.2/libraries/ESP8266WiFi
 Not used: /Applications/Electronics/Arduino.app/Contents/Java/libraries/WiFi
 Not used: /Users/user/Desktop/Arduino/libraries/WiFi101
exit status 1
Error compiling for board LOLIN(WEMOS) D1 R2 & mini.

What on earth is "_ZN4Gate11waitForGateEiii"?

When using Arduino IDE 1.6.5 or newer, this is unnecessary. The only exception is that the File > Examples and Sketch > Include Library menus do require a reload to update. But for a library author those convenience features are not so essential.

That is completely unnecessary. The library is loaded from disk on every compile if it has been modified. You are only adding unnecessary delay to your development workflow.

Probably Name Mangling. We can't see your current code.

Gate.h:

/*
  Gate.h - Library for controlling gates.
  Created by K Gray, December 26, 2021.
  Released into the public domain.
*/

#ifndef Gate_h
#define Gate_h

#include "Arduino.h"

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

class Gate
{
  public:
    Gate();
    void Magnet(int magnetPin);
    void LEDStrip(int r, int g, int b);
    
    void OTAStart(const char OTAPassword[]);
    void WiFiStart(char WiFiNetwork[], char WiFiPassword[]);
    void begin(int pin1, int pin2, int pin3, int pin4);
    
    void gate(int Direction);
    void stop();
    void magnet(int magnetValue);
    void ledStrip(int red, int green, int blue);
    float calcDist(float CurrentLatitude, float CurrentLongitude, float SavedLatitude, float SavedLongitude);
    void waitForGate(int travelDist, int travelSpeed, int percentOfTravel);
    void delayForGate(int DFGTime);

    void WFGFunction();
    
    int direction;
    boolean emergencyStop;
  private:
    int _pin1;
    int _pin2;
    int _pin3;
    int _pin4;
    int _magPin;
    int _r;
    int _g;
    int _b;
    char _OTAPassword;
    String _WiFiSSID;
    String _WiFiPASS;
    int _Direction;
    int _magnetValue;
    int _red;
    int _green;
    int _blue;
    float _CurrentLatitude;
    float _CurrentLongitude;
    float _SavedLatitude;
    float _SavedLongitude;
    int _travelDist;
    int _travelSpeed;
    int _percentOfTravel;
    int _DFGTime;
    
    int _wfg;
    int _totalDelay;
};

#endif

Gate.cpp:



/*
  Gate.cpp - Library for controlling gates.
  Created by K Gray, December 26, 2021.
  Released into the public domain.
*/

#include "Arduino.h"
#include "Gate.h"

Gate::Gate() {}

void Gate::Magnet(int magnetPin){
  pinMode(magnetPin, OUTPUT);
  _magPin = magnetPin;
}

void Gate::LEDStrip(int r, int g, int b){
  pinMode(r, OUTPUT);
  pinMode(g, OUTPUT);
  pinMode(b, OUTPUT);
  _r = r;
  _g = g;
  _b = b;
}

void Gate::OTAStart(const char OTAPassword[]){
  // Port defaults to 8266
  ArduinoOTA.setPort(8266);
  // Hostname defaults to esp8266-[ChipID]
  ArduinoOTA.setHostname("GateESP");
  // No authentication by default
  ArduinoOTA.setPassword(OTAPassword);//(const char 

  ArduinoOTA.onStart([]() {
    Serial.println("Start");
  });
  ArduinoOTA.onEnd([]() {
    Serial.println("\nEnd");
  });
  ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
    Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
  });
  ArduinoOTA.onError([](ota_error_t error) {
    Serial.printf("Error[%u]: ", error);
    if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
    else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
    else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
    else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
    else if (error == OTA_END_ERROR) Serial.println("End Failed");
  });
  ArduinoOTA.begin();
  Serial.println("Ready");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
}

void Gate::WiFiStart(char WiFiNetwork[], char WiFiPassword[]){
  WiFi.mode(WIFI_STA);
  WiFi.begin(WiFiNetwork, WiFiPassword);
  while (WiFi.waitForConnectResult() != WL_CONNECTED) {
    Serial.println("Connection Failed! Rebooting...");
    delay(5000);
    ESP.restart();
  }
}

void Gate::begin(int pin1, int pin2, int pin3, int pin4){
  pinMode(pin1, OUTPUT);
  pinMode(pin2, OUTPUT);
  pinMode(pin3, OUTPUT);
  pinMode(pin4, OUTPUT);
  _pin1 = pin1;
  _pin2 = pin2;
  _pin3 = pin3;
  _pin4 = pin4;
}

void Gate::gate(int Direction){
  if (direction == 1){
    if (Direction == 1){
      digitalWrite(_pin1, HIGH);
      digitalWrite(_pin2, LOW);
      digitalWrite(_pin3, HIGH);
      digitalWrite(_pin4, LOW);
    }
    if (Direction == 2){
      digitalWrite(_pin1, LOW);
      digitalWrite(_pin2, HIGH);
      digitalWrite(_pin3, LOW);
      digitalWrite(_pin4, HIGH);
    }
  }
  else if (direction == 2){
    if (Direction == 1){
      digitalWrite(_pin1, LOW);
      digitalWrite(_pin2, HIGH);
      digitalWrite(_pin3, LOW);
      digitalWrite(_pin4, HIGH);
    }
    if (Direction == 2){
      digitalWrite(_pin1, HIGH);
      digitalWrite(_pin2, LOW);
      digitalWrite(_pin3, HIGH);
      digitalWrite(_pin4, LOW);
    }
  }
}

void Gate::stop(){
  digitalWrite(_pin1, LOW);
  digitalWrite(_pin2, LOW);
  digitalWrite(_pin3, LOW);
  digitalWrite(_pin4, LOW);
}

void Gate::magnet(int magnetValue){
  digitalWrite(_magPin, magnetValue);
}

void Gate::ledStrip(int red, int green, int blue){
  analogWrite(red, _r);
  analogWrite(green, _g);
  analogWrite(blue, _b);
}

float Gate::calcDist(float CurrentLatitude, float CurrentLongitude, float SavedLatitude, float SavedLongitude){
  // HaverSine version
  const float Deg2Rad = 0.01745329252;               // (PI/180)  0.017453293, 0.0174532925
  //const double EarthRadius = 6372.795;              //6372.7976 In Kilo meters, will scale to other values
  const float EarthRadius = 20908120.1;              // In feet  20908128.6
  float DeltaLatitude, DeltaLongitude, a, Distance;

  // degrees to radians
  CurrentLatitude = (CurrentLatitude + 180) * Deg2Rad;     // Remove negative offset (0-360), convert to RADS
  CurrentLongitude = (CurrentLongitude + 180) * Deg2Rad;
  SavedLatitude = (SavedLatitude + 180) * Deg2Rad;
  SavedLongitude = (SavedLongitude + 180) * Deg2Rad;

  DeltaLatitude = SavedLatitude - CurrentLatitude;
  DeltaLongitude = SavedLongitude - CurrentLongitude;

  a =(sin(DeltaLatitude/2) * sin(DeltaLatitude/2)) + cos(CurrentLatitude) * cos(SavedLatitude) * (sin(DeltaLongitude/2) * sin(DeltaLongitude/2));
  Distance = EarthRadius * (2 * atan2(sqrt(a),sqrt(1-a)));
  return(Distance);
}



void waitForGate(int travelDist, int travelSpeed, int percentOfTravel){
  int _totalDelay = ((((travelDist*percentOfTravel)/travelSpeed)*1000)-500)/2;
  int _wfg;
  Begin:
  _wfg++;
  while(_wfg <= _totalDelay){
    void WFGFunction();
    goto Begin;
  }
  _wfg = 0;
}

void Gate::delayForGate(int DFGTime){
  delay(DFGTime);
}

void WFGFunction(){}

Example1_Gate.ino:

#include <Gate.h>

Gate myGate;

#define direction = 1;

void setup(){
  myGate.WiFiStart("MYSSID", "MYPASS");
  myGate.begin(D3,D4,D7,D8);
  myGate.Magnet(D5);
  myGate.LEDStrip(D1, D2, D6);
}

void loop(){
  myGate.gate(1);
  myGate.waitForGate(12,0.4,0.8);
  myGate.stop();
  myGate.delayForGate(5000);
  myGate.gate(2);
  myGate.waitForGate(12,0.4,0.8);
  myGate.stop();
  myGate.delayForGate(5000);
}

You defined "void waitForGate()" but did not define "void Gate::waitForGate()".

Note: The three arguments to 'waitForGate()' are integers so "myGate.waitForGate(12, 0.4, 0.8);" is equivalent to "myGate.waitForGate(12, 0, 0);". If you want to pass floats you should declare the arguments as floats.

Thank you @johnwasser

That solved it! I missed "Gate::" before one function. Well that is a stupid reason to sit here scratching my head for hours.

I also changed those three variables to floats.

It compiles ok, but what is all of this info?

Executable segment sizes:
ICACHE : 32768           - flash instruction cache 
IROM   : 239984          - code in flash         (default or ICACHE_FLASH_ATTR) 
IRAM   : 27013   / 32768 - code in IRAM          (IRAM_ATTR, ISRs...) 
DATA   : 1496  )         - initialized variables (global, static) in RAM/HEAP 
RODATA : 984   ) / 81920 - constants             (global, static) in RAM/HEAP 
BSS    : 26160 )         - zeroed variables      (global, static) in RAM/HEAP 
Sketch uses 269477 bytes (25%) of program storage space. Maximum is 1044464 bytes.
Global variables use 28640 bytes (34%) of dynamic memory, leaving 53280 bytes for local variables. Maximum is 81920 bytes.

I usually only see the sketch and global sizes that are at the bottom. What is the rest?

Details for the ESP8266. Just ignore them.