Undefined error when trying to link

I'm trying to get some firmware originally compiled in C for an AVR chip to compile in Arduino IDE as a sketch. I copied over all the code and setup the tabs for each file (*.c and *.h) but when I compile I get this verbose output error:

Using previously compiled file: /var/folders/zw/ymsbqm_j7nsdp4bp00087zk80000gr/T/arduino_build_834128/sketch/wmath.c.o
/Users/maureencole/Desktop/Arduino.app/Contents/Java/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_PRO -DARDUINO_ARCH_AVR -I/Users/maureencole/Desktop/Arduino.app/Contents/Java/hardware/arduino/avr/cores/arduino -I/Users/maureencole/Desktop/Arduino.app/Contents/Java/hardware/arduino/avr/variants/eightanaloginputs /var/folders/zw/ymsbqm_j7nsdp4bp00087zk80000gr/T/arduino_build_834128/sketch/main.c.ino.cpp -o /var/folders/zw/ymsbqm_j7nsdp4bp00087zk80000gr/T/arduino_build_834128/sketch/main.c.ino.cpp.o
Compiling libraries...
Compiling core...
Using precompiled core: /var/folders/zw/ymsbqm_j7nsdp4bp00087zk80000gr/T/arduino_cache_67136/core/core_arduino_avr_pro_cpu_16MHzatmega328_0aea8ac1ee3e8efc18f761f97efe0efe.a
Linking everything together...
/Users/maureencole/Desktop/Arduino.app/Contents/Java/hardware/tools/avr/bin/avr-gcc -w -Os -g -flto -fuse-linker-plugin -Wl,--gc-sections -mmcu=atmega328p -o /var/folders/zw/ymsbqm_j7nsdp4bp00087zk80000gr/T/arduino_build_834128/main.c.ino.elf /var/folders/zw/ymsbqm_j7nsdp4bp00087zk80000gr/T/arduino_build_834128/sketch/MP3sound.c.o /var/folders/zw/ymsbqm_j7nsdp4bp00087zk80000gr/T/arduino_build_834128/sketch/fifo.c.o /var/folders/zw/ymsbqm_j7nsdp4bp00087zk80000gr/T/arduino_build_834128/sketch/i2c.c.o /var/folders/zw/ymsbqm_j7nsdp4bp00087zk80000gr/T/arduino_build_834128/sketch/realtime.c.o /var/folders/zw/ymsbqm_j7nsdp4bp00087zk80000gr/T/arduino_build_834128/sketch/sequencer.c.o /var/folders/zw/ymsbqm_j7nsdp4bp00087zk80000gr/T/arduino_build_834128/sketch/serial.c.o /var/folders/zw/ymsbqm_j7nsdp4bp00087zk80000gr/T/arduino_build_834128/sketch/servo.c.o /var/folders/zw/ymsbqm_j7nsdp4bp00087zk80000gr/T/arduino_build_834128/sketch/suart.c.o /var/folders/zw/ymsbqm_j7nsdp4bp00087zk80000gr/T/arduino_build_834128/sketch/wmath.c.o /var/folders/zw/ymsbqm_j7nsdp4bp00087zk80000gr/T/arduino_build_834128/sketch/main.c.ino.cpp.o /var/folders/zw/ymsbqm_j7nsdp4bp00087zk80000gr/T/arduino_build_834128/../arduino_cache_67136/core/core_arduino_avr_pro_cpu_16MHzatmega328_0aea8ac1ee3e8efc18f761f97efe0efe.a -L/var/folders/zw/ymsbqm_j7nsdp4bp00087zk80000gr/T/arduino_build_834128 -lm
/var/folders/zw/ymsbqm_j7nsdp4bp00087zk80000gr/T//ccnylTg4.ltrans0.ltrans.o: In function `SendSetupToSlave':
/Users/maureencole/Documents/Arduino/main.c/main.c.ino:2170: undefined reference to `suart_puts(char*)'
/var/folders/zw/ymsbqm_j7nsdp4bp00087zk80000gr/T//ccnylTg4.ltrans0.ltrans.o: In function `StartSlaveSequence':
/Users/maureencole/Documents/Arduino/main.c/main.c.ino:2155: undefined reference to `suart_puts(char*)'
/var/folders/zw/ymsbqm_j7nsdp4bp00087zk80000gr/T//ccnylTg4.ltrans0.ltrans.o: In function `HPFlash':
/Users/maureencole/Documents/Arduino/main.c/main.c.ino:1969: undefined reference to `suart_puts(char*)'

I know the functions are defined, what am I doing wrong?

...But forgot to post it here.

That happens a lot.

Well, between your word and compiler message I know who iā€™m going to trust more. If mr. C says the functions are undefined then they are undefined.

Something important to understand is that, after some minimal preprocessing, the .ino files of the sketch are compiled as C++ (not C).

The .c files of the sketch are compiled as C.

This means that if you want to call a function defined in the .c file from the .ino file, you must wrap the declaration in extern "C" {}

Minimal demo of the problem:

CFunctionCall.ino

void foo();
void setup() {
  foo();
}
void loop() {}

foo.c

void foo() {}

Compilation of the above sketch fails:

C:\Users\asdf\AppData\Local\Temp\cc5NQZOE.ltrans0.ltrans.o: In function `setup':
C:\Users\asdf\Documents\Arduino\CFunctionCall/CFunctionCall.ino:3: undefined reference to `foo()'

Now the corrected version:

CFunctionCall.ino

extern "C" {
  void foo();
}
void setup() {
  foo();
}
void loop() {}

foo.c

void foo() {}

Or, 99% of the time, you can just rename all your ".c" files to ".cpp" (or even ".ino").

You should probably just get rid of the suart_puts() function and use Serial.print().

1 Like

Good point!

Here is an example of where using C++ instead of C can be beneficial. Even though you can use C in your C++ code, the reverse not so much. Serial is a class object and classes are a C++ only feature. Much of the Arduino core API and most library code is C++, so you would need to do without them in your C programs.

That said, I do think it is cool that Arduino users are given the option of using C, and assembly components in their Arduino sketches in addition to C++, even though I have never found a need for such a thing.

1 Like

This was a step in the right direction. Instead of undefined now I'm getting duplicate definition errors.

The reason I didnt post the entire code is because its 7400+ lines of code, across 20 something files, and NOT written by me.

I'm simply trying to take this firmware and translate it over so I can edit it as a sketch and upload it to an Arduino Pro Mini as opposed to having to edit it in Eclipse and Upload using AVRdude or something similar.

The original source code can be found here:
Marcduino V3

The duplicate definitions were from some include files having global definitions and being included in more than one place.

I think I got all the compile-time errors (and most of the warnings) out.
MarcDuino.zip (72.2 KB)

Thank you! This compiles fine, now if I can just get it to upload. Getting an error programmer not responding when trying to upload to my Arduino Pro Mini using both Adafruits TinyUSB programmer OR Arduino Uno with the chip removed. Neither seem to connect. Its late and I'll try troubleshooting tomorrow after work.

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