dcrearer dcrearer - 2 months ago 9
C Question

undefined symbol when compiling C program

I'm getting the following error when compiling the source code that follows and I don't understand why. Can you please explain what I'm doing wrong. I have defined the signature of the failing method before using it however the linker can not find the symbol.

Undefined symbols for architecture x86_64:
"_numberOfDays", referenced from:
_main in determinetomorrow-240382.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

#include <stdio.h>
#include <stdbool.h>

struct Date
{
int month;
int day;
int year;
};

int numberOfDays(struct Date);
bool isLeapYear(struct Date);

int main (void) {


struct Date today, tomorrow;
printf("Enter todays date int format (mm dd yyyy): ");
scanf("%i%i%i", &today.month, &today.day, &today.year);

if(today.day != numberOfDays(today)) {
tomorrow.day = 1;
tomorrow.month = 1;
tomorrow.year = today.year + 1;
} else if(today.month == 12) {
tomorrow.day = 1;
tomorrow.month = today.month + 1;
tomorrow.year = today.year + 1;
} else {
tomorrow.day = 1;
tomorrow.month = today.month + 1;
tomorrow.year = today.year;
}

printf("Tomorrow's date is %i/%i/%.2i. \n", tomorrow.month, tomorrow.day, tomorrow.year % 100);

return 0;
}

int numberOfDay(struct Date d) {
int days;
const int daysPerMonth[12] =
{31,28,31,30,31,30,31,31,30,31,30,31};
if(isLeapYear(d) == true && d.month == 2)
days = 29;
else
days = daysPerMonth[d.month - 1];

return days;
}

bool isLeapYear(struct Date d) {
bool leapYearFlag;

if((d.year % 4 == 0 && d.year % 100 != 0) || d.year % 400 == 0)
leapYearFlag = true;
else
leapYearFlag = false;

return leapYearFlag;
}

Answer
int numberOfDays(struct Date);

int numberOfDay(struct Date d) {
    int days;
    const int daysPerMonth[12] = 
        {31,28,31,30,31,30,31,31,30,31,30,31};
    if(isLeapYear(d) == true && d.month == 2)
        days = 29;
    else
        days = daysPerMonth[d.month - 1];

    return days;
}

You have a typo in the function name.