Undefined reference to `setup', undefined reference to `loop'

I just added a library, and then included Arduino without raising any errors.

But now I see an error I’ve seen before, but it doesn’t make any sense to me here.

This is a C problem, not a PlatforrmIO problem, but the program compiles successfully. It fails on test. Code below. Unmodified Arduino .ini file.

//Ingeimaks
#include <Arduino.h>
#include <Servo.h>
//definiamo i servomotori orizzontale e verticale
Servo servohori;
int servoh = 0;
int servohLimitHigh = 160;
int servohLimitLow = 60;
Servo servoverti; 
int servov = 0; 
int servovLimitHigh = 160;
int servovLimitLow = 60;
//Pin fotoresistenze
int ldrtopl = 2; //top left 
int ldrtopr = 1; //top right 
int ldrbotl = 3; // bottom left 
int ldrbotr = 0; // bottom right 
 void setup () 
 { 
  Serial.begin(9600);
  servohori.attach(10);
  servohori.write(60);
  servoverti.attach(9);
  servoverti.write(60);
  delay(500)
 }
void loop()
{
  servoh = servohori.read();
  servov = servoverti.read();
  //Valore Analogico delle fotoresistenza
  int topl = analogRead(ldrtopl);
  int topr = analogRead(ldrtopr);
  int botl = analogRead(ldrbotl);
  int botr = analogRead(ldrbotr);
  // Calcoliamo una Media
  int avgtop = (topl + topr) ; //average of top 
  int avgbot = (botl + botr) ; //average of bottom 
  int avgleft = (topl + botl) ; //average of left 
  int avgright = (topr + botr) ; //average of right 
  if (avgtop < avgbot)
  {
    servoverti.write(servov +1);
    if (servov > servovLimitHigh) 
     { 
      servov = servovLimitHigh;
     }
    delay(10);
  }
  else if (avgbot < avgtop)
  {
    servoverti.write(servov -1);
    if (servov < servovLimitLow)
  {
    servov = servovLimitLow;
  }
    delay(10);
  }
  else 
  {
    servoverti.write(servov);
  }
    if (avgleft > avgright)
  {
    servohori.write(servoh +1);
    if (servoh > servohLimitHigh)
    {
    servoh = servohLimitHigh;
    }
    delay(10);
  }
  else if (avgright > avgleft)
  {
    servohori.write(servoh -1);
    if (servoh < servohLimitLow)
     {
     servoh = servohLimitLow;
     }
    delay(10);
  }
  else 
  {
    servohori.write(servoh);
  }
  delay(50);
}

“Test” will ignore files in src/ by default and use the sources from test/ of the project. If you want to get into unit testing, Redirecting... and platformio-examples/unit-testing/calculator at develop · platformio/platformio-examples · GitHub are good starting projects.

Testing is a rabbit hole I’d rather save for later. I really only want the code to run. I’ve never had a program compile and upload successfully without actually working or doing something. But I just want to make it work.

What’s the error telling me to do?

1 Like

Reading about it made me want to try, but I can’t make sense of the testing stuff.

I used the correct address, but my C prompt only changed directories as far as my user folder. I’m trying to follow the first tutorial.

I’ve done this before. I’ve stripped everything out and built it back piecemeal until I found the error. But once again I see something in PIO that doesn’t make sense. This is the whole program now:

//Ingeimaks
#include <Arduino.h>
//definiamo i servomotori orizzontale e verticale
//Pin fotoresistenze
int ldrtopl = 2; //top left
int ldrtopr = 1; //top right
int ldrbotl = 3; // bottom left
int ldrbotr = 0; // bottom right
void setup ()
{
Serial.begin(9600);
delay(500);
}
void loop()
{
}

Here are the errors:
Linking .pio\build\uno\firmware.elf
.pio\build\uno\src\main1.cpp.o (symbol from plugin): In function setup': (.text+0x0): multiple definition of setup’
.pio\build\uno\src\main.cpp.o (symbol from plugin):(.text+0x0): first
defined here
.pio\build\uno\src\main1.cpp.o (symbol from plugin): In function setup': (.text+0x0): multiple definition of loop’
.pio\build\uno\src\main.cpp.o (symbol from plugin):(.text+0x0): first
defined here
.pio\build\uno\src\main1.cpp.o (symbol from plugin): In function setup': (.text+0x0): multiple definition of ldrbotr’
.pio\build\uno\src\main.cpp.o (symbol from plugin):(.text+0x0): first
defined here
.pio\build\uno\src\main1.cpp.o (symbol from plugin): In function setup': (.text+0x0): multiple definition of ldrbotl’
.pio\build\uno\src\main.cpp.o (symbol from plugin):(.text+0x0): first
defined here
.pio\build\uno\src\main1.cpp.o (symbol from plugin): In function setup': (.text+0x0): multiple definition of ldrtopr’
.pio\build\uno\src\main.cpp.o (symbol from plugin):(.text+0x0): first
defined here
.pio\build\uno\src\main1.cpp.o (symbol from plugin): In function setup': (.text+0x0): multiple definition of ldrtopl’
.pio\build\uno\src\main.cpp.o (symbol from plugin):(.text+0x0): first
defined here
collect2.exe: error: ld returned 1 exit status
*** [.pio\build\uno\firmware.elf] Error 1
==================== [FAILED]

To create (define) the code that defines the setup and loop functions. In a nutshell (although I haven’t tested this), you should be able to create a main.cpp in your /test folder (since unit testing uses this folder, not the main source folder), and

#include <Arduino.h>             // Arduino core

void setup()
{
}

void loop()
{
}

will get it past the error (because setup and loop are now defined), but will clearly do nothing at all. This is where the rest of the example code linked above comes in.

There is little point in using Test unless you are going to implement unit testing properly - i.e. automated code testing, automatic upload to device to test code on the actual hardware, etc. It’s not ‘test’ as in ‘have I put together valid code’ but test as in ‘try to compile and run this code, and check it responds to pre-programmed input the way it should’. ‘Build’ is the normal test of if the code will compile (and hopefully run).

I’m not ready for testing, but loop and setup are defined in my code.

You think Main.cpp will fix it?
I’ll give it a shot.

It’s Arduino framework.

1 Like

Do you have two main.cpp files? Main and main1? If so pio is trying to compile both of them and your are getting duplicate definitions

1 Like

Actually, that’s what happened here. I edited Main to remove everything but the declarations. Thanks.

1 Like

If you are using PlatformIO chekc if you have your code placed on root with all the folders needed(include,src,lib,test) or you have selected the new paths in the compiler parameters