"undefined reference to main" error - can't upload any sketch!

Hello. Basically, whenever I upload or verify any sketch, even example ones, Blink, Fade, etc., I get the following error:

C:\Program Files (x86)\Arduino\arduino-builder -dump-prefs -logger=machine -hardware C:\Program Files (x86)\Arduino\hardware -tools C:\Program Files (x86)\Arduino\tools-builder -tools C:\Program Files (x86)\Arduino\hardware\tools\avr -built-in-libraries C:\Program Files (x86)\Arduino\libraries -libraries C:\Users\[REDACTED]\Documents\Arduino\libraries -fqbn=arduino:avr:uno -vid-pid=2341_0043 -ide-version=10813 -build-path C:\Users\[REDACTED]\AppData\Local\Temp\arduino_build_363939 -warnings=none -build-cache C:\Users\[REDACTED]\AppData\Local\Temp\arduino_cache_743699 -prefs=build.warn_data_percentage=75 -prefs=runtime.tools.arduinoOTA.path=C:\Program Files (x86)\Arduino\hardware\tools\avr -prefs=runtime.tools.arduinoOTA-1.3.0.path=C:\Program Files (x86)\Arduino\hardware\tools\avr -prefs=runtime.tools.avrdude.path=C:\Program Files (x86)\Arduino\hardware\tools\avr -prefs=runtime.tools.avrdude-6.3.0-arduino17.path=C:\Program Files (x86)\Arduino\hardware\tools\avr -prefs=runtime.tools.avr-gcc.path=C:\Program Files (x86)\Arduino\hardware\tools\avr -prefs=runtime.tools.avr-gcc-7.3.0-atmel3.6.1-arduino7.path=C:\Program Files (x86)\Arduino\hardware\tools\avr -verbose C:\Program Files (x86)\Arduino\examples\01.Basics\Blink\Blink.ino
C:\Program Files (x86)\Arduino\arduino-builder -compile -logger=machine -hardware C:\Program Files (x86)\Arduino\hardware -tools C:\Program Files (x86)\Arduino\tools-builder -tools C:\Program Files (x86)\Arduino\hardware\tools\avr -built-in-libraries C:\Program Files (x86)\Arduino\libraries -libraries C:\Users\[REDACTED]\Documents\Arduino\libraries -fqbn=arduino:avr:uno -vid-pid=2341_0043 -ide-version=10813 -build-path C:\Users\[REDACTED]\AppData\Local\Temp\arduino_build_363939 -warnings=none -build-cache C:\Users\[REDACTED]\AppData\Local\Temp\arduino_cache_743699 -prefs=build.warn_data_percentage=75 -prefs=runtime.tools.arduinoOTA.path=C:\Program Files (x86)\Arduino\hardware\tools\avr -prefs=runtime.tools.arduinoOTA-1.3.0.path=C:\Program Files (x86)\Arduino\hardware\tools\avr -prefs=runtime.tools.avrdude.path=C:\Program Files (x86)\Arduino\hardware\tools\avr -prefs=runtime.tools.avrdude-6.3.0-arduino17.path=C:\Program Files (x86)\Arduino\hardware\tools\avr -prefs=runtime.tools.avr-gcc.path=C:\Program Files (x86)\Arduino\hardware\tools\avr -prefs=runtime.tools.avr-gcc-7.3.0-atmel3.6.1-arduino7.path=C:\Program Files (x86)\Arduino\hardware\tools\avr -verbose C:\Program Files (x86)\Arduino\examples\01.Basics\Blink\Blink.ino
Using board 'uno' from platform in folder: C:\Program Files (x86)\Arduino\hardware\arduino\avr
Using core 'arduino' from platform in folder: C:\Program Files (x86)\Arduino\hardware\arduino\avr
Detecting libraries used...
"C:\\Program Files (x86)\\Arduino\\hardware\\tools\\avr/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10813 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\cores\\arduino" "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\variants\\standard" "C:\\Users\\[REDACTED]\\AppData\\Local\\Temp\\arduino_build_363939\\sketch\\Blink.ino.cpp" -o nul -DARDUINO_LIB_DISCOVERY_PHASE
Generating function prototypes...
"C:\\Program Files (x86)\\Arduino\\hardware\\tools\\avr/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10813 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\cores\\arduino" "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\variants\\standard" "C:\\Users\\[REDACTED]\\AppData\\Local\\Temp\\arduino_build_363939\\sketch\\Blink.ino.cpp" -o "C:\\Users\\[REDACTED]\\AppData\\Local\\Temp\\arduino_build_363939\\preproc\\ctags_target_for_gcc_minus_e.cpp" -DARDUINO_LIB_DISCOVERY_PHASE
"C:\\Program Files (x86)\\Arduino\\tools-builder\\ctags\\5.8-arduino11/ctags" -u --language-force=c++ -f - --c++-kinds=svpf --fields=KSTtzns --line-directives "C:\\Users\\[REDACTED]\\AppData\\Local\\Temp\\arduino_build_363939\\preproc\\ctags_target_for_gcc_minus_e.cpp"
Compiling sketch...
"C:\\Program Files (x86)\\Arduino\\hardware\\tools\\avr/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -MMD -flto -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10813 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\cores\\arduino" "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\variants\\standard" "C:\\Users\\[REDACTED]\\AppData\\Local\\Temp\\arduino_build_363939\\sketch\\Blink.ino.cpp" -o "C:\\Users\\[REDACTED]\\AppData\\Local\\Temp\\arduino_build_363939\\sketch\\Blink.ino.cpp.o"
Compiling libraries...
Compiling core...
Using precompiled core: C:\Users\[REDACTED]\AppData\Local\Temp\arduino_cache_743699\core\core_arduino_avr_uno_0c812875ac70eb4a9b385d8fb077f54c.a
Linking everything together...
"C:\\Program Files (x86)\\Arduino\\hardware\\tools\\avr/bin/avr-gcc" -w -Os -g -flto -fuse-linker-plugin -Wl,--gc-sections -mmcu=atmega328p -o "C:\\Users\\[REDACTED]\\AppData\\Local\\Temp\\arduino_build_363939/Blink.ino.elf" "C:\\Users\\[REDACTED]\\AppData\\Local\\Temp\\arduino_build_363939\\sketch\\Blink.ino.cpp.o" "C:\\Users\\[REDACTED]\\AppData\\Local\\Temp\\arduino_build_363939/..\\arduino_cache_743699\\core\\core_arduino_avr_uno_0c812875ac70eb4a9b385d8fb077f54c.a" "-LC:\\Users\\[REDACTED]\\AppData\\Local\\Temp\\arduino_build_363939" -lm
c:/program files (x86)/arduino/hardware/tools/avr/bin/../lib/gcc/avr/7.3.0/../../../../avr/lib/avr5/crtatmega328p.o:(.init9+0x0): undefined reference to `main'
collect2.exe: error: ld returned 1 exit status
exit status 1
Error compiling for board Arduino Uno.

I enabled verbose output during compiling, hoping to pinpoint the error, but to no avail. I don't know if this is the source for my problem (but it is unlikely), but I installed a dark theme on the IDE just recently from GitHub, I didn't delete anything, just replaced the theme folder in the Arduino Program Files directory.

A couple of things I tried:

  • Press reset before and after pressing upload on the IDE
  • Upload regularly and using the programmer
  • Fixed encoding and reload (I don't know what this does but I gave it a shot)
  • Removing anything connected to the board
  • Stopping the board from printing to serial (code in the Arduino prints to Serial, but stops on a certain condition, which I activated, so it doesn't print to Serial)
  • Burned bootloader (same error after some time)
  • Tried different programmers
  • Copying and pasting the code to a new sketch (I saw that it worked for someone, not for me)

I can usually solve these types of problems myself, but this one caught me dumbfounded. Any help would be appreciated. Thanks!

It's a compile error and has nothing to do with the upload and hence has nothing to do with drivers, the board etc.

You might or might not have tried it but if not, I suggest that you close and open the IDE again and try again.

Some more details

Any C/C++ program has a main function. Arduino has hidden it for you but you can find the source file in the following possible locations:
C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino\main.cpp
C:\Users\yourUsername\AppData\Local\Arduino15\packages\arduino\hardware\avr*1.6.21*\cores\arduino\main.cpp

I suspect that you only have the first directory which is for a fresh install of the IDE, the second one if you updated the AVR boards packages (I'm using IDE 1.8.5 and upgraded the AVR boards to version 1.6.21, hence the version 1.6.21 in the path, yours might be different).
First check if you have the second directory. If yes, check if it contains main.cpp. If not, repeat for the second directory.

When the IDE verifies/compiles, it copies all needed files to a temp directory first; in your case C:\Users\[REDACTED]\AppData\Local\Temp\arduino_build_363939. Note that this directory can change (e.g. when you close and open the IDE). In that directory you should find a main.cpp.o in the subdirectory core.
I suggest that you close and open the IDE, enable verbose output during compilation and check the result for a line like below that indicates that main.cpp is indeed compiled (see the bold filenames at the end of the line, one for the source file and one for the object file)

"C:\Users\yourUsername\AppData\Local\Arduino15\packages\arduino\tools\avr-gcc\4.9.2-atmel3.5.4-arduino2/bin/avr-g++" -c -g -Os -Wall -Wextra -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -MMD -flto -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10805 -DARDUINO_AVR_NANO -DARDUINO_ARCH_AVR "-IC:\Users\yourUsername\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.21\cores\arduino" "-IC:\Users\yourUsername\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.21\variants\eightanaloginputs" "C:\Users\yourUsername\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.21\cores\arduino\main.cpp" -o "C:\Users\yourUsername\AppData\Local\Temp\arduino_build_327767\core\main.cpp.o"

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