Undefined reference to `loop' and 'setup'

Hello I’m new here. I’m using VSCode with PlatformIO. Now my Problem: Every Time I create a new project and try it to run, it don’t work. Even if I use an .cpp file without any code
e.g.
main.cpp:

#include <Arduino.h>
void setup() {
}
void loop() {
}

platformio.ini:

[env:micro]
platform = atmelavr
board = micro
framework = arduino

ERROR:
C:\Users\JENSWI~1\AppData\Local\Temp\ccwbSejc.ltrans0.ltrans.o: In function main':*** ***<artificial>:(.text.startup+0x128): undefined reference tosetup’
:(.text.startup+0x130): undefined reference to `loop’
collect2.exe: error: ld returned 1 exit status
****** [.pio\build\micro\firmware.elf] Error 1***

Does anyone knows how to solve it and can help me?

Here that error is expected.

Where is this file? In the src/ folder or somewhere else?

1 Like

it’s in the src folder. It was by default created there as I created a new project

Can you execute a verbose compile by opening a PIO Terminal and executing

pio run -t clean
pio run -v > compile.log 2>&1

Then upload the resulting compile.log to https://pastebin.com/ and post the link.

1 Like

Sure this is the output? This is the .travis.yml.

1 Like

The log shows that compilation was successfull without any errors…

avr-g++ -o .pio\build\micro\firmware.elf -Os -mmcu=atmega32u4 -Wl,--gc-sections -flto -fuse-linker-plugin .pio\build\micro\src\main.cpp.o -L.pio\build\micro -Wl,--start-group .pio\build\micro\libFrameworkArduinoVariant.a .pio\build\micro\libFrameworkArduino.a -lm -Wl,--end-group
avr-objcopy -O ihex -R .eeprom .pio\build\micro\firmware.elf .pio\build\micro\firmware.hex
MethodWrapper(["checkprogsize"], [".pio\build\micro\firmware.elf"])
Advanced Memory Usage is available via "PlatformIO Home > Project Inspect"
DATA:    [=         ]   5.8% (used 149 bytes from 2560 bytes)
PROGRAM: [=         ]  12.6% (used 3620 bytes from 28672 bytes)
========================= [SUCCESS] Took 19.06 seconds =========================

In VSCode, when you clean the project (using the task in the PIO task explorer) and recompile it, do you still have an error?

Yes? It’s weird the error is still there

Can you execute a clean and a verbose complation in the task explorer and paste it to pastebin? Especially the first compilation command is important (where it builds src\main.cpp).

1 Like

I’ll try it tomorrow and send you the link

Ok. Another piece of advice I can offer is that your Windows username contains spaces, which has caused some problems in PlatformIO (but shouldn’t now anymore…). Maybe renaming your user will magically make it succeed in VSCode (although it already does on the CLI…)

2 Likes

Yeah I can try this to

Oh it only fails if I run it as test. If i run it in clean, there is no error. But if i hit test and run it after the test the error occure. Then i have to run it first with “pio run -t clean” and the error is gone 'till i use test again

Okay, that it fails when using “Test” is the entire gist of it, wasn’t quiet reflected in the sentence Every Time I create a new project and try it to run, it don’t work. . When you hit “Test” it means you want to compile the unit tests which are under test/, while ignoring src/. If test/ is an empty folder you’ll get that error for sure.

Example and documentation are available: Unit Testing of a “Blink” Project — PlatformIO latest documentation

2 Likes

Wait so you do what? Test → error or no error? → normal run / aka compile and upload → error? Which tasks exactly do you execute in what order?

1 Like

If I use run there is no error.

If I use test there is an error. If I use run than after the test error run is occurring an error.

The run error disappears again if I use

pio run -t clean
and then
pio run -v

But thanks a lot for your help. Maybe the compiler sets the path of the main.cpp to the test Ordner when I use test and reset it by using

pio run -t clean
pio run -v

If you mean when you do something like …

pio test -e micro
pio run -e micro

… that both the test AND the normal build/run fail, I can’t reproduce that behaviour. Only the test build fails for me. The errors you were getting about undefined references is normal, as Test is for unit testing, and uses/looks for code in the test directory, not the src directory - resulting in those functions not being used - but being declared as existing.