我正在制作一个关于动物和其他功能的结构来控制它。当来自其他文件的所有代码都在一个文件中时,它运行良好,但是当我决定将代码设置为其他文件时,我收到以下错误:
d:/programms/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.0.1/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\yuriy\AppData\Local\Temp\ccAi1tOR.o:main.c:(.text+0x2a): undefined reference to `new_animal'
d:/programms/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.0.1/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\yuriy\AppData\Local\Temp\ccAi1tOR.o:main.c:(.text+0x36): undefined reference to `animal_info'
collect2.exe: error: ld returned 1 exit status
main.c
#include <stdio.h>
#include <stdlib.h>
#include "animal.h"
int main() {
animal jack = new_animal("jack", 23, 2.4f);
animal_info(&jack);
return 0;
}
animal.h
#pragma once
typedef struct animal {
char name[25];
int age;
float happiness;
} animal;
struct animal new_animal(char name[25], int age, float happiness);
void animal_info(struct animal *an);
animal.c
#include <stdio.h>
#include <stdlib.h>
#include "animal.h"
#include "charfunc.h"
struct animal new_animal(char name[25], int age, float happiness) {
struct animal *an = malloc(sizeof(struct animal));
copy_paste(name, an->name);
an->age = age;
an->happiness = happiness;
return *an;
free(an);
}
void animal_info(struct animal *an) {
printf("ANIMAL INFO:\n");
printf(" name -> %s\n", an->name);
printf(" age -> %d\n", an->age);
printf(" happiness -> %f\n", an->happiness);
}
charfunc.h
#pragma once
void copy_paste(const char* from, char* to);
charfunc.c
#include <stdio.h>
#include <stdlib.h>
#include "charfunc.h"
void copy_paste(const char* from, char* to) {
int i = 0;
for (i; from[i]; i++)
{
to[i] = from[i];
}
to[i] = 0;
}