Linking problems: collect2 error

Hello and thanks for PlatformIO!

I am coding my first Arduino project in C++. It seems to be compiling correctly when I put all code in one file. However, when I divide my program in multiple files, I get the following error message:

collect2: error: ld returned 1 exit status

*** [.pioenvs/uno/firmware.elf] Error 1

The complete output can be found here:

The whole project is on github:
https://github.com/speendo/AutomaticMorse-A-Com/tree/withoutTemplates

Probably it’s some basic error I’m making, however, I can’t find it.

Can you help me?

Thank you in advance!

You do weird stuff in there. E.g. in InputSignal.cpp, you declare the class and start implementing functions as

#include <Arduino.h>
#include "SignalStorage.hpp"

class InputSignal {
  const unsigned int waitMs;
  const unsigned int minEventMs;
  const unsigned int maxSigMs;

  bool signal;
  bool prevSignal;
  bool error;
  bool listening;

  unsigned long validEventMs;

  unsigned long lastEventMs;

  SignalStorage signalStorage;

public:
  InputSignal(unsigned int waitMs, unsigned int minEventMs, unsigned int maxSigMs, SignalStorage signalStorage) :
  waitMs(waitMs),
  minEventMs(minEventMs),
  maxSigMs(maxSigMs),
  signalStorage(signalStorage)
  {
  }

  void setup() {
    reset();
  }
//...

but then in InputSignal.hpp you declare it all over again?

#ifndef InputSignal_hpp
#define InputSignal_hpp

class InputSignal {
public:
  InputSignal(unsigned int waitMs, unsigned int minEventMs, unsigned int maxSigMs);
  void setup();
  void loop();
  void reset();
  bool getError();

protected:
  const unsigned int waitMs;
  const unsigned int minEventMs;
  const unsigned int maxSigMs;

  bool getSmoothSignal();
  virtual bool getSignal();
};
#endif

You must put your class declarations in the hpp file and the definitions/implementations in the cpp file without re-declaring the entire class again. They also have completely different definitions of the same functions like the constructor.

hpp:

  InputSignal(unsigned int waitMs, unsigned int minEventMs, unsigned int maxSigMs);

cpp:

InputSignal::InputSignal(unsigned int waitMs, unsigned int minEventMs, unsigned int maxSigMs, SignalStorage signalStorage)

Thus it’s impossible to know what you’ve actually meant.

A starting point would be to read https://www.learncpp.com/cpp-tutorial/89-class-code-and-header-files/ and follow the style starting at " Putting class definitions in a header file"

2 Likes

Thank you for looking at it in such a detailed manner.

To be honest, I didn’t understand header files at all. Coming from Java, I didn’t know what their purpose was.

The tutorial you mentioned cleared things up a little bit. I will try to replicate this knowledge in my project and report back if everything works as intended.

Thank you once more!

1 Like

Thank you!

That solved my problem… at least this one :smiley:

1 Like