Multiple definitions of 'loop'

fitball

Member
I developed some MIDI code for a hobby project and Aurduino worked, ie, compiled, loaded, Teensy ran, and Serial and MIDI monitors worked.
Also Windows 10 accepted the MIDI data and played correctly.

Recently I began to get the 'multiple...loop' error. even when I removed all the temporary files between each compile.
Now it's become permanent. So I reinstalled twice now but the error is permanent.

Uninstalled Arduino and deleted all instances of Arduino stuff in App-Data.
Reinstalled Arduino latest stable nightly and Teensy add-on
Compile the empty file that comes up when opened -> OK
Type in a few lines of code to the setup and loop sections and recompile -> Get the "multiple...loop" error

I've been programming computers for about 60 years but I'm stumped with this one.
 
Static code upon startup:
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
}

Complier msg:
ening Teensy Loader...
Memory Usage on Teensy 4.1:
FLASH: code:8932, data:3016, headers:8528 free for files:8105988
RAM1: variables:3808, code:6240, padding:26528 free for local variables:487712
RAM2: variables:12416 free for malloc/new:511872

New code typed into the static code. Had to remove the Setup block to compile:
int velocity = 80; // Velocity
int note=20; // Piano note
void loop() {
// put your main code here, to run repeatedly:
usbMIDI.sendNoteOn(note, velocity, 1);
delay(1000);
usbMIDI.sendNoteOff(note, velocity, 1);
delay(1000);
note++;
if (note > 100) {note = 20;}
}

Compiler msg:
c:/users/john/appdata/local/arduino15/packages/teensy/tools/teensy-compile/11.3.1/arm/bin/../lib/gcc/arm-none-eabi/11.3.1/../../../../arm-none-eabi/bin/ld.exe: C:\Users\john\AppData\Local\Temp\arduino\sketches\C5ED626E8196BBA31D29C5615AA5BDE1/core\core.a(Blink.cc.o): in function `loop':
C:\Users\john\AppData\Local\Arduino15\packages\teensy\hardware\avr\1.59.0\cores\teensy4/Blink.cc:11: multiple definition of `loop'; C:\Users\john\AppData\Local\Temp\arduino\sketches\C5ED626E8196BBA31D29C5615AA5BDE1\sketch\sketch_oct4a.ino.cpp.o:C:\Users\john\AppData\Local\Temp\.arduinoIDE-unsaved202494-12212-sp6pie.y4ii\sketch_oct4a/sketch_oct4a.ino:6: first defined here
collect2.exe: error: ld returned 1 exit status
exit status 1
Compilation error: exit status 1

What is Blink.cc.0 doing in there? How did it get there?
 
It looks like you are linking with some Arduino core other than Teensy, i.e. core.a. Is that the entire sketch? Please post the entire sketch, and please use the "code" tag (</> in the message window) when you post code to the forum.
 
You need the setup() function in the same file as loop(). Why did you remove it?

The copy of the Blink program in the core usually gets ignored because it contains only a setup() and loop() function, which both get overridden by your program. But since you have no setup(), it's being used by the linker and causes the multiple loop() symbol definition error.
 
What you've done is add a loop() function inside the loop function like this:
Code:
void loop() {
// put your main code here, to run repeatedly:
  int velocity = 80; // Velocity
  int note=20; // Piano note
void loop() {
// put your main code here, to run repeatedly:
  usbMIDI.sendNoteOn(note, velocity, 1);
  delay(1000);
  usbMIDI.sendNoteOff(note, velocity, 1);
  delay(1000);
  note++;
  if (note > 100) {note = 20;}
}
}
Remove the first line (EDIT: AND the last line) and it will/should compile.

Pete
 
Thanks for your attention and replies.
I don't know how to use tag (</> for the code? Please advise. Here I'm using BOLD to distinguish comment from code.
I didn't have a Setup in the code that was working before. Only a lot of initializing code and a lot of complex logic in the loop.
I removed Setup because the compiler complained about variables not being declared in the scope.
If I leave the Setup in I get:

C:\Users\john\AppData\Local\Temp\.arduinoIDE-unsaved202494-2304-yzpbff.wp40m\sketch_oct4a\sketch_oct4a.ino: In function 'void setup()':
C:\Users\john\AppData\Local\Temp\.arduinoIDE-unsaved202494-2304-yzpbff.wp40m\sketch_oct4a\sketch_oct4a.ino:5:19: warning: unused variable 'channel' [-Wunused-variable]
5 | const int channel = 1; // the MIDI channel number to send messages
| ^~~~~~~
C:\Users\john\AppData\Local\Temp\.arduinoIDE-unsaved202494-2304-yzpbff.wp40m\sketch_oct4a\sketch_oct4a.ino:7:13: warning: unused variable 'cable' [-Wunused-variable]
7 | int cable = 0; // the MIDI virtual cable to use
| ^~~~~
C:\Users\john\AppData\Local\Temp\.arduinoIDE-unsaved202494-2304-yzpbff.wp40m\sketch_oct4a\sketch_oct4a.ino:9:13: warning: unused variable 'velocity' [-Wunused-variable]
9 | int velocity = 80; // Velocity
| ^~~~~~~~
C:\Users\john\AppData\Local\Temp\.arduinoIDE-unsaved202494-2304-yzpbff.wp40m\sketch_oct4a\sketch_oct4a.ino:11:13: warning: unused variable 'note' [-Wunused-variable]
11 | int note=20; // Piano note
| ^~~~
C:\Users\john\AppData\Local\Temp\.arduinoIDE-unsaved202494-2304-yzpbff.wp40m\sketch_oct4a\sketch_oct4a.ino: In function 'void loop()':
C:\Users\john\AppData\Local\Temp\.arduinoIDE-unsaved202494-2304-yzpbff.wp40m\sketch_oct4a\sketch_oct4a.ino:15:24: error: 'note' was not declared in this scope
15 | usbMIDI.sendNoteOn(note, velocity, channel);
| ^~~~
C:\Users\john\AppData\Local\Temp\.arduinoIDE-unsaved202494-2304-yzpbff.wp40m\sketch_oct4a\sketch_oct4a.ino:15:30: error: 'velocity' was not declared in this scope
15 | usbMIDI.sendNoteOn(note, velocity, channel);
| ^~~~~~~~
C:\Users\john\AppData\Local\Temp\.arduinoIDE-unsaved202494-2304-yzpbff.wp40m\sketch_oct4a\sketch_oct4a.ino:15:40: error: 'channel' was not declared in this scope
15 | usbMIDI.sendNoteOn(note, velocity, channel);
| ^~~~~~~
exit status 1
Compilation error: 'note' was not declared in this scope

Until a few weeks ago I was finishing up small details of my keyboard scanning program which complied, loaded, and worked.
Then it all stopped. Does the IDE have hidden variable storage that I haven't found?
 
What is Blink.cc.0 doing in there? How did it get there?

That is very strange. We don't have any "Blink.cc" file in the core library code, which would be installed at AppData\Local\Arduino15\packages\teensy\hardware\avr\1.59.0\cores\teensy4/Blink.cc.

Are you using Arduino IDE 2.3.3? Or another recent 2.3.x version?

If so, click Boards Manager, search "teensy" and click the REMOVE button. Or hover your mouse over "1.59.0 installed" and it becomes a remove button. If using Arduino IDE older than 2.3.0, find the small Teensy Loader window and File > Quit before using the remove button. Removal with 2.3.0 it will automatically quit Teensy Loader (but on Windows, it if doesn't quit Windows will not allow its file to be removed, so this is an important detail to get right - used to be a common problem before Arduino 2.3.0).

Removal will completely delete that folder.

Then just use Boards Manager to add 1.59.0 back again. This will give you a freshly installed AppData\Local\Arduino15\packages\teensy install.

Please understand this stuff in AppData does NOT go away if you reinstall the Arduino IDE. It lingers there forever. To refresh it, you need to follow these steps with Boards Manager.
 
I don't know how to use tag (</> for the code? Please advise.

In Arduino IDE, click Ctrl-A to select all code, and Ctrl-V to copy to clipboard.

Then in the forum on your browser, click this button. In the popup box, press Ctrl-C to copy your code.

1728063396284.png


Arduino IDE can open more than 1 file. If you have done this, they appear as tabs. If you're unsure, use a program Shift-PrtScr to make a screenshot and use a program like Paint to clip it, then Ctrl-C to clipboard and Ctrl-V to paste here on the forum (yes, pasting images works).

If you have 2 or more files, repeat this copy copy for each file. Also clearly say the filenames, as that can matter. Or show a screenshot if you're unsure. Remember, we can't see your screen. The better you show us, the more we can help.
 
Thanks for the Code tag usage. Only the default file loaded (no tabs) and MIDI code typed in on top of default code.
I removed 1.59.0 and reinstalled. Same result. There must be hidden variables somewhere in App_Data or the code folder.

Code:
/* Transmit USB MIDI message.  You must select MIDI from the "Tools > USB Type" menu */
    int velocity = 80;        // Velocity
    int note=20;            // Piano note
void loop()
{
    usbMIDI.sendNoteOn(note, velocity, 1);
    delay(1000);
    usbMIDI.sendNoteOff(note, velocity, 1);
    delay(1000);
    note++;
    if (note > 100) {note = 20;}
}

Same error.

I deleted saved files from: C:\Users\john\AppData\Local\Temp\arduino\sketches\*.*\sketch\*.*
and Cores files. Same error.

Just for fun I deleted the inserted code to nothingness:

Code:
/* Transmit USB MIDI message.  You must select MIDI from the "Tools > USB Type" menu */
void loop()
{
 }

Same error!

Code:
c:/users/john/appdata/local/arduino15/packages/teensy/tools/teensy-compile/11.3.1/arm/bin/../lib/gcc/arm-none-eabi/11.3.1/../../../../arm-none-eabi/bin/ld.exe: C:\Users\john\AppData\Local\Temp\arduino\sketches\17D2A3E20CC66E180CB80E8027FB7819/..\..\cores\teensy_avr_teensy41_usb_midi,speed_600,opt_o2std,keys_en-us_d572ae07f2443ba1a75ed9510909b9f2\core.a(Blink.cc.o): in function `loop':
C:\Users\john\AppData\Local\Arduino15\packages\teensy\hardware\avr\1.59.0\cores\teensy4/Blink.cc:11: multiple definition of `loop'; C:\Users\john\AppData\Local\Temp\arduino\sketches\17D2A3E20CC66E180CB80E8027FB7819\sketch\sketch_oct4a.ino.cpp.o:C:\Users\john\AppData\Local\Temp\.arduinoIDE-unsaved202494-1080-17rfgef.cu2d\sketch_oct4a/sketch_oct4a.ino:7: first defined here
collect2.exe: error: ld returned 1 exit status
exit status 1
Compilation error: exit status 1

Do you think it would help to remove the program with Windows and then clean the Registry with RegEdit?
 
I put the SETUP example in a prior post but here it is again:
Code:
void setup(){
    int velocity = 80;        // Velocity
    int note=20;            // Piano note
}
void loop()
{
    usbMIDI.sendNoteOn(note, velocity, 1);
    delay(1000);
    usbMIDI.sendNoteOff(note, velocity, 1);
    delay(1000);
    note++;
    if (note > 100) {note = 20;}
}

And here is the error msg:
Code:
C:\Users\john\AppData\Local\Temp\.arduinoIDE-unsaved202494-19096-cbhf0z.n54um\sketch_oct4a\sketch_oct4a.ino: In function 'void setup()':
C:\Users\john\AppData\Local\Temp\.arduinoIDE-unsaved202494-19096-cbhf0z.n54um\sketch_oct4a\sketch_oct4a.ino:2:13: warning: unused variable 'velocity' [-Wunused-variable]
    2 |         int velocity = 80;              // Velocity
      |             ^~~~~~~~
C:\Users\john\AppData\Local\Temp\.arduinoIDE-unsaved202494-19096-cbhf0z.n54um\sketch_oct4a\sketch_oct4a.ino:3:13: warning: unused variable 'note' [-Wunused-variable]
    3 |         int note=20;                    // Piano note
      |             ^~~~
C:\Users\john\AppData\Local\Temp\.arduinoIDE-unsaved202494-19096-cbhf0z.n54um\sketch_oct4a\sketch_oct4a.ino: In function 'void loop()':
C:\Users\john\AppData\Local\Temp\.arduinoIDE-unsaved202494-19096-cbhf0z.n54um\sketch_oct4a\sketch_oct4a.ino:7:24: error: 'note' was not declared in this scope
    7 |     usbMIDI.sendNoteOn(note, velocity, 1);
      |                        ^~~~
C:\Users\john\AppData\Local\Temp\.arduinoIDE-unsaved202494-19096-cbhf0z.n54um\sketch_oct4a\sketch_oct4a.ino:7:30: error: 'velocity' was not declared in this scope
    7 |     usbMIDI.sendNoteOn(note, velocity, 1);
      |                              ^~~~~~~~
exit status 1
Compilation error: 'note' was not declared in this scope
 
Many thanks to the reviewers who have joined in.
In response to jmarsh: Setup left in and declarations placed below it. The error changed to:


Code:
C:\Users\john\AppData\Local\Temp\.arduinoIDE-unsaved202494-11624-zk2z2r.or5j\sketch_oct4a\sketch_oct4a.ino: In function 'void setup()':
C:\Users\john\AppData\Local\Temp\.arduinoIDE-unsaved202494-11624-zk2z2r.or5j\sketch_oct4a\sketch_oct4a.ino:5:1: error: a function-definition is not allowed here before '{' token
    5 | {
      | ^
C:\Users\john\AppData\Local\Temp\.arduinoIDE-unsaved202494-11624-zk2z2r.or5j\sketch_oct4a\sketch_oct4a.ino:12:2: error: expected '}' at end of input
   12 | }
      |  ^
C:\Users\john\AppData\Local\Temp\.arduinoIDE-unsaved202494-11624-zk2z2r.or5j\sketch_oct4a\sketch_oct4a.ino:1:14: note: to match this '{'
    1 | void setup() {   // put your setup code here, to run once: }
      |              ^
C:\Users\john\AppData\Local\Temp\.arduinoIDE-unsaved202494-11624-zk2z2r.or5j\sketch_oct4a\sketch_oct4a.ino:2:13: warning: unused variable 'velocity' [-Wunused-variable]
    2 |         int velocity = 80;              // Velocity
      |             ^~~~~~~~
C:\Users\john\AppData\Local\Temp\.arduinoIDE-unsaved202494-11624-zk2z2r.or5j\sketch_oct4a\sketch_oct4a.ino:3:13: warning: unused variable 'note' [-Wunused-variable]
    3 |         int note=20;                    // Piano note
      |             ^~~~
exit status 1
Compilation error: a function-definition is not allowed here before '{' token

It has to be something very simple but hidden very well since it used to work. My program reads Clavinova keys, calculates the velocity via the time delay between two sensors, makes other adjustments, and plays the note. It was working and then progressively begin to have problems during fine tuning as you've seen here.
If we cannot solve this would it help if I moved to Linux for this project? Is the IDE more stable there?
Is there another Windows approach to compiling and loading that won't take me a lifetime to learn?
 
Post the code which produced the errors in your message #17. You've got some misplaced braces somewhere.

Pete
 
Code:
void setup() {   // put your setup code here, to run once: }
    int velocity = 80;        // Velocity
    int note=20;            // Piano note
void loop()
{
    usbMIDI.sendNoteOn(note, velocity, 1);
    delay(1000);
    usbMIDI.sendNoteOff(note, velocity, 1);
    delay(1000);
    note++;
    if (note > 100) {note = 20;}
}
 
It has to be something very simple

It is indeed something very simple, exactly has jmarsh explained. Your variables "velocity" and "note" are local to only the setup() function. Using them in the loop function is an error. You need to create the code like this:

Code:
int velocity = 80;  // Velocity
int note = 20;      // Piano note

void setup() {
}

void loop() {
  usbMIDI.sendNoteOn(note, velocity, 1);
  delay(1000);
  usbMIDI.sendNoteOff(note, velocity, 1);
  delay(1000);
  note++;
  if (note > 100) { note = 20; }
}

I checked just now. With Tools > USB Type set to MIDI, it does indeed compile without errors. Here's a screenshot to confirm.

1728074788785.png
 
To explain what went wrong with the code in msg #19, is also a simple mistake in your code. Arduino IDE and the compiler are working exactly as they should.

The problem is the close bracket that ends your setup() function went away. This is because "//" causes the rest of the line to be a comment. The compiler no longer sees a } close bracket, because it is now part of a comment. Of course compiling gives an error, because your setup() function doesn't have a close bracket.

1728075370464.png
 
Last edited:
Look, I know you've had a rough and frustrating experience, and right now you're probably not feeling very confident in the tools and compiler. But it really is working properly.
 
One IDE Tool that is very helpful is " Ctrl + T ":
1728076040218.png


Taking the Code from post #19 and then hitting Ctrl+T here results in this that quickly presents a view of the source as the compiler might see it.
The indenting goes very wrong and on inspection it might help find the start of the trouble - where that closing brace is lost in the comment.
Code:
void setup() {   // put your setup code here, to run once: }
  int velocity = 80;        // Velocity
  int note = 20;          // Piano note
  void loop()
  {
    usbMIDI.sendNoteOn(note, velocity, 1);
    delay(1000);
    usbMIDI.sendNoteOff(note, velocity, 1);
    delay(1000);
    note++;
    if (note > 100) {
      note = 20;
    }
  }
 
Yes, I'm sorry that the closed bracket issue was my mistake made in the heat of battle.

The latest recommendation works as it should. My code, with similar structure, also compiles now.
However my code getting the "multiple...loop" error caused this conversation.
Maybe we'll never know what was it was.

I'm happy to get back to coding rather than fighting the IDE. However, if the "...loop" error comes back I may call on you again.
Many thanks to all who contributed.

My first coding effort was with the hardware design and process control programming team for Westinghouse's PRODAC 4449 in 1959.
It was a room-sized monster with a fraction of the power of this teensy.
 
programming computers for about 60 years but I'm stumped with this one.
first coding effort was with the hardware design and process control programming team for Westinghouse's PRODAC 4449 in 1959.
It was a room-sized monster with a fraction of the power of this teensy.

Wow - That adds up ...
Code:
 Westinghouse Electric Company was involved in the project during the late 1950s  ...
The first process computer in the PRODAC family was the PRODAC 4449, which had the following innovative solutions:

Priority interrupts
Separate processing units for mathematical and logical operations.
Discrete logic units with an on/off speed of 3 microseconds

Westinghouse built 4 machines that were installed in various steel mills around the United States,
and they were so successful that the last PRODAC 4449 was taken out of service in 1983.
 
Back
Top