Arduino 2.0 Error - undefined reference to `main' Compilation error: exit status 1

I installed the latest Arduino 2.0 and noticed a bug, probably.

The newest IDE requires you to select a board when compiling a sketch.
I promptly selects Arduino Nano and it has been working perfectly fine but somehow whenever I change it to Arduino Uno, I'll get this error.

Out of curiousity, I tried every single AVR boards available and needless to say every board works except only Arduino Uno

But why is that? Is this a bug? and how to fix this error.

You need to add `#include <arduino.h> in the first line, then setup and loop works.

Should not need to include arduino.h.

I did find some funny behaviour (not necessarily the same as yours), for the UNO and also the Leonardo, which disappears when I do a "Save As.." giving it a new name. Try that.

Hi @hizon. I believe the cause of the error is a corrupted core compilation cache file.

For the sake of efficiency, the Arduino IDE caches the compiled "core" (the code that defines the standard Arduino API) and libraries the first time you compile them for a given board. This means that all subsequent compilations for that board will be much faster because only the sketch needs to be compiled if the other components are already cached. Any available caches will be reused until the source files have changed, either due to you updating to a new version, or else editing them directly.

I'm not sure exactly why, but one rare occasions the archive files in the cache might get corrupted in some way. That causes this type of confusing error.

I think the most straightforward solution is to delete the file at this path:

:warning: Please be very careful when deleting things from your computer. When in doubt, back up!

C:\Users\INTEL\AppData\Local\Temp\arduino-core-cache\core_arduino_avr_uno_df83f6d5d05a15a4e6f20a13f5554071.a

:exclamation: If looking for it with your file browser or command line, note that the C:\Users\INTEL\AppData folder is hidden by default. On Windows "File Explorer", you can make it visible by opening the "View" menu, then checking the box next to "☐ Hidden items".

After deleting that file, try compiling a sketch once more. Hopefully the error will no longer occur.

Please let me know if you have any questions or problems.


In case you are interested to learn more about this subject, I'll share some additional information:

Arduino IDE 1.x deleted the cache when you exited the IDE. That made it easy to recover from a corrupted cache simply by restarting the IDE, but also meant that everything had to be recompiled at the start of each IDE session.

Arduino IDE 2.x does not do that cache removal on exit. This is the reason for the significant decreases in compilation time compared to Arduino IDE 1.x that some users have noted. Unfortunately this new "persistent cache" approach does make it less easy to recover from a corrupted cache.

The cache is stored in the system temporary folder, so this is dependent on how the operating system treats that folder. On my Linux machine, the operating system automatically clears the temporary folder when I restart the computer. On my Windows machine, the operating system never automatically clears the folder. This is the reason I suggested the approach of manually deleting the bad file.

An alternative fix would be to uninstall and then reinstall the boards platform that contains the core. This will cause the cache to be invalidated and a new cache generated on the next compile. I'm not sure whether it is a more user friendly approach. It can become more tricky in the case of 3rd party platforms which may reference the core from another platform.

2 Likes

Thank you. Deleted the file and the sketch now run.

You are welcome. I'm glad it is working now.

Regards,
Per

1 Like

Worked for me. Thanks

Thanks Per, you saved my life! Only that I finally had to delete all arduino*.* files in that directory.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.