Collect2.exe: error: ld returned 1 exit status

Full error code: collect2.exe: error: ld returned 1 exit status
*** [.pio\build\nanoatmega328\firmware.elf] Error 1

Project code: https://github.com/SaV91/LaserTagGame.git

Please help

Ok! I checked out your github and found the screenshot. You have undefined references to setup and loop.

In your main.cpp you have defined a function main(). Don’t! Not when using the Arduino framework. If you want to code main() you can’t be doing it in the Arduino framework. If you want to use the Arduino framework, you need to code setup() and loop().

I think your version of main() is being (somehow) over-ridden by the Arduino one - you are using the Arduino framework in platformio.ini, and that version always calls setup() and loop(), which don’t appear in your source files. Hence the errors.

This code:

int main()
{

  pinMode(SHOOT_BUTT , INPUT);
  pinMode(REL_BUTT , INPUT);
  pinMode(SWITCH_BUTT , INPUT);
  Serial.begin(9600);
  Serial.setTimeout(50);
  playerConfig();
  
  for(;;)
  {
    receiveData();
    displayData();
    shootHandler();
   reloadHandler();
  }

}

Should be this:

void setup()
{

  pinMode(SHOOT_BUTT , INPUT);
  pinMode(REL_BUTT , INPUT);
  pinMode(SWITCH_BUTT , INPUT);
  Serial.begin(9600);
  Serial.setTimeout(50);
  playerConfig();
}

void loop() {
    receiveData();
    displayData();
    shootHandler();
   reloadHandler();
}

(Off the top of my head!)

HTH

Cheers,
Norm.

Also, you have defined this line in Iguns.h:

IRsend *irrecvObj;

That header is included in main.cpp and in Iguns.cpp hence the error about multiple definitions of ireccvObj.

Remove the line from the header file, add it to main.cpp and add this line to Iguns.cpp:

extern IRsend *irrecvObj;

That should fix that error.

Cheers,
Norm.

Thank you! problems with IRsend object are solved, but compiler still returned the collect2.exe error .
Also compiler says that i have undefined reference to loop and setup

(Open image in new tab to see)

Strange! You don’t still have a main function somewhere, do you?

Cheers,
Norm.

No , i dont have the main function. Maybe its a platformo bug or kind of…

Maybe i should compile it in arduino ide?

It wouldn’t compile there either.

Here is the problem, from main.cpp:

namespace MyLaserTag_Gun
...
void setup() {
...
}

void loop() {
...
}

...
}

Your setup() and loop() functions need to be outside of your namespace.

It might work fine if you add using namespace MyLaserTag_Gun; at the start of main.cpp, but I would just keep them both out of the namespace.

Cheers,
Norm.

1 Like

So , i deleted the namespace and … get the collect2.exe error and undefined reference receiveData() function warning. It seems that PlatformIO is kiding on me

Here is the screenshot:

I dont know how , but after // the receiveData() it worked . Thank you for your big feedback. The problem was in this method.

All your functions are in that namespace. If you add using namespace MyLaserTagGun; you can uncomment it again, it should be fine.

If you don’t add the above, you will need to call them as MyLaserTagGun::receiveData() etc.

Alternatively, lose the namespace, and the corresponding ‘}’ at the very end of main.cpp.

Cheers,
Norm.

1 Like