Error "undefined reference" when compiling Arduino Uno

Hello! I'm programing on the Arduino IDE and I have the following error when compiling:

C:\Users\edelm\AppData\Local\Temp\cciLVR6L.ltrans0.ltrans.o: In function setup': C:\Users\edelm\Documents\Arduino-Clock2/Arduino-Clock2.ino:4: undefined reference to setDot(DigitPin, int)'
collect2.exe: error: ld returned 1 exit status

My Arduino-Clock.ino:

#include "SS5641AS.h"

void setup() {
	setDot(DIG1, 1);
}

void loop() {
}

My SS5641AS.h file:

#ifndef _SS5641AS_H
#define _SS5641AS_H

typedef enum {
	DIG1 = 1,
	DIG2 = 5,
	DIG3 = 6,
	DIG4 = 7,
} DigitPin;

typedef enum {
	A = 11,
	B = 0,
	C = 3,
	D = 8,
	E = 9,
	F = 10,
	G = 2,
	H = 4,
} SegmentPin;

void update(void);
void loadNum(int, DigitPin);
void setDot(DigitPin, int);
void clear(DigitPin);

#endif

My SS5641AS.c file:

#include "SS5641AS.h"
#include "Arduino.h"
#include <stdint.h>

const uint8_t NUMBERS[11] = { 0b00111111, 0b00000110, 0b01011011, 0b01001111, 0b01100110, 0b01101101, 0b01111101, 0b00000111, 0b01111111, 0b01101111, 0b00000000 };
const DigitPin DIGITS[] = { DIG1, DIG2, DIG3, DIG4 };
const SegmentPin SEGMENTS[] = { A, B, C, D, E, F, G, H };

int digits[4] = { 10, 10, 10, 10 };
int dot[4] = { 0, 0, 0, 0 };

void update() {
	static int current = 0;
	digitalWrite(DIGITS[current], LOW);
	for (int i = 1; i < 4; i++)
		digitalWrite(DIGITS[(current + i) % 4], HIGH);
	int state;
	for (int i = 0; i < 7; i++) {
		state = (NUMBERS[digits[current]] & 0x1 << i) ? HIGH : LOW;
		digitalWrite(SEGMENTS[i], state);
	}
	if (dot[current])
		digitalWrite(SEGMENTS[7], HIGH);
	else
		digitalWrite(SEGMENTS[7], LOW);
	current = (current + 1) % 4;
}

void loadNum(int num, DigitPin pos) {
	if (num < 0 || num > 9) return;
	switch (pos) {
		case DIG1:
			digits[0] = num;
			break;
		case DIG2:
			digits[1] = num;
			break;
		case DIG3:
			digits[2] = num;
			break;
		case DIG4:
			digits[3] = num;
			break;
	}
}

void setDot(DigitPin pin, int set) {
	switch (pin) {
		case DIG1:
			dot[0] = set;
			break;
		case DIG2:
			dot[1] = set;
			break;
		case DIG3:
			dot[2] = set;
			break;
		case DIG4:
			dot[3] = set;
			break;
	}
}

I would really appreciate any help. Thank you!

Which version of the Arduino IDE are you using?

Sometimes the preprocessor makes mistakes in placing function prototypes, and no one seems to know why or how to fix it.

When I run into this problem, I simply remove the #include statements and copy/paste the library source code before setup() and loop(). The problem usually goes away.

In other cases it seems to work to include the prototype(s) "by hand", e.g. try placing this line above setup():

void setDot(DigitPin, int);
1 Like

In wokwi it runs fine and without compilation errors.

That is consistent with an IDE preprocessor error.

Yes, I was just editing to add that it reinforced your explanation. :wink:

I'm using version 2.1.1

Adding the prototype to the ino did not work for me.

Moving everything to the ino did work but I would like to avoid it. But thanks for the idea.

Try 1.8.19. I'm interested in whether the problem persists.

I was about to try 1.8.19, and noticed the .c file. Try renaming that to .cpp

There are two different compilers.

The problem persists with 1.8.19, and renaming the file from .c to .cpp fixes it.

1 Like

Wow! Thank you!
Yeah, that fixed it.

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