Home > OS >  Why does the compiler is giving me this: undefined reference to function?
Why does the compiler is giving me this: undefined reference to function?

Time:09-29

Ok I have been trying to figure this out for 3 hours and I have search a bunch of stack overflow questions and answers but I have this same error:

/usr/bin/ld: /tmp/ccXbXNRV.o: in function 'main':
main.c:(.text 0x5a): undefined reference to 'plus'
/usr/bin/ld: main.c:(.text 0x67): undefined reference to `minus'
collect2: error: ld returned 1 exit status

And I cant figure this out because my code doesn't seem that he is the problem

main.c:

#include <stdio.h>
#include "funcs.c"

int main()
{
    int z = 0;
    int wh = 1;
    while (wh == 1)
    {
        printf("What you want?\n1-Plus\n2-Minus\n");
        scanf("%d", &z);
        if (z == 1)
        {
            plus();
        }
        if (z == 2)
        {
            minus();
        }
    }
    printf("The program ended\n");

    return 0;
}

funcs.c

#include <stdio.h>

inline void plus(void)
{
    int a = 0;
    int b = 0;
    printf("Pls insert a numb\n");
    scanf("%d", &a);
    printf("Pls insert a numb\n");
    scanf("%d", &b);
    a = a   b;
    printf("The result is: %d\n", a);
}

inline void minus(void)
{
    int a = 0;
    int b = 0;
    printf("Pls insert a numb\n");
    scanf("%d", &a);
    printf("Pls insert a numb\n");
    scanf("%d", &b);
    a = a - b;
    printf("The result is: %d\n", a);
}

help.h

extern int a;
extern int b;
extern int z;
extern int wh;
inline void minus(void);
inline void plus(void);

I have try to compile it with this command gcc funcs.c main.c I know that is a simple program but I really want to learn c

If you could help I would be very thankful!

CodePudding user response:

You can fix this by doing three things:

  1. Don't include a .c file. You're already providing it to gcc on the command line.
  2. Include help.h in files where you use those functions.
  3. Not using inline. You can't use inline when the caller and callee are in different translation units.

main.c:

#include <stdio.h>
// #include "funcs.c"
#include "help.h"

int main()
{
    int z = 0;
    int wh = 1;
    while (wh == 1)
    {
        printf("What you want?\n1-Plus\n2-Minus\n");
        scanf("%d", &z);
        if (z == 1)
        {
            plus();
        }
        if (z == 2)
        {
            minus();
        }
    }
    printf("The program ended\n");

    return 0;
}

funcs.c

#include <stdio.h>

void plus(void)
{
    int a = 0;
    int b = 0;
    printf("Pls insert a numb\n");
    scanf("%d", &a);
    printf("Pls insert a numb\n");
    scanf("%d", &b);
    a = a   b;
    printf("The result is: %d\n", a);
}

void minus(void)
{
    int a = 0;
    int b = 0;
    printf("Pls insert a numb\n");
    scanf("%d", &a);
    printf("Pls insert a numb\n");
    scanf("%d", &b);
    a = a - b;
    printf("The result is: %d\n", a);
}

help.h:

extern int a;
extern int b;
extern int z;
extern int wh;
void minus(void);
void plus(void);

Compile and run like so:

$ gcc -Wall -Werror funcs.c main.c
$ ./a.out 
What you want?
1-Plus
2-Minus
^C

Other thoughts:

extern int a;
extern int b;
extern int z;
extern int wh;

You're already declaring these variables locally. This is unneeded. The extern keyword tells the compiler that these variables are defined in another translation unit that it can't see. This isn't true, so you should just remove these.

  • Related