0

I'm making a structure about animal and other functions to control it. When all code from other files was in one file it was working good, but when I decided to set code to other files I'm getting these errors:

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;
}

CH_YUR
  • 25
  • 6
  • What are you typing into the terminal to build it? – yano Jul 19 '21 at 14:58
  • gcc -o main.exe main.c – CH_YUR Jul 19 '21 at 15:00
  • 3
    You need to include all your source files when you build. Try `gcc -o main.exe main.c animal.c charfunc.c`. The linker is saying "I don't know where the `new_animal` function is defined." It's defined in animal.c. The tool chain doesn't know that until you tell it. – yano Jul 19 '21 at 15:01
  • can I type another command into the terminal if I want to include all my files not typing separately every file? – CH_YUR Jul 19 '21 at 15:04
  • an aside, there's no need for `malloc` and `free`ing in `new_animal`. You can simply use automatic storage with `struct animal an;`, change the `->` to `.`, and `return an;` – yano Jul 19 '21 at 15:05
  • 2
    You might be able to do `gcc -o main.exe *.c`,,, not sure. But I would discourage something like that, as it would only work if all your source files are in the same directory. If you want to simplify your build procedure, a far better practice is to use an established build system, like [Make](https://en.wikipedia.org/wiki/Make_(software)) or [CMake](https://en.wikipedia.org/wiki/CMake) – yano Jul 19 '21 at 15:08
  • 1
    a `Makefile` would be helpful. – alex01011 Jul 19 '21 at 15:43
  • the primary answer of interest for this post is https://stackoverflow.com/a/12574400/77580 – n. m. could be an AI Jul 19 '21 at 16:53

0 Answers0