-1

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!

ops400
  • 3
  • 1
  • 3
  • Does this answer your question? [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Ulrich Eckhardt Sep 28 '21 at 18:22
  • Don't [Include c file in another](https://stackoverflow.com/q/10448047/10077) in your `main.c`. Rather, `#include "help.h"`. – Fred Larson Sep 28 '21 at 18:25
  • Apart from the above, it helps if you make smaller steps and use a version control system in order to be able to revert steps. For this case in particular, I see that you never use help.h. Also, what if you remove one of the two functions not found? This might seem silly, but it reduces the problem. Further, the manual input in `main()`. In the end, you get a [mcve]. – Ulrich Eckhardt Sep 28 '21 at 18:26

1 Answers1

3

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.

Nick ODell
  • 15,465
  • 3
  • 32
  • 66
  • 1
    Also, all of the `extern` variables are pointless. There are no global variables by those names, and the variables that have those names are at function scope. – Fred Larson Sep 28 '21 at 18:36
  • 1
    And `wh` never changes, so the `while` loop will never terminate. – Fred Larson Sep 28 '21 at 18:38
  • It would be a good suggestion to OP that `extern` variables declared in `.h` be _defined_ and initialized in _global_ space at top of `main.c` rather then in function scope. – ryyker Sep 28 '21 at 18:44
  • Thanks a lot for your answer, it helped me a lot to understand how I should make the variables and the functions, I edited my code with your response to see if it would finally work and used your compile command but it gave me the same error, what can I do? – ops400 Sep 28 '21 at 19:56
  • @ops400 Dunno what to tell you. I set up all of the files in the exact way you described them, made the changes above, and it worked. – Nick ODell Sep 28 '21 at 20:06
  • Well thanks any way I will close this post and move on and try other methods that I find. – ops400 Sep 28 '21 at 20:12
  • @ryyker: Why would that be a good suggestion?? – Fred Larson Sep 28 '21 at 20:22
  • @NickODell it was my bad sorry I forgot to remove the `inline` , silly mistake on my part sorry to disturb you again. – ops400 Sep 28 '21 at 20:27
  • @FredLarson - OP original intent seemed to suggest the desire to use globally scoped variables. `extern` variables declared and defined properly will provide that nicely. Why do you ask? (Your first comment makes the point. As created, all the variable intended to have global scope, do not. That is because they are created incorrectly.) – ryyker Sep 28 '21 at 21:03
  • 1
    @ryyker: I didn't infer an intent to use global variables, but more likely learning that `extern` exists and not knowing what it's actually for. And none of the local variables would be helpful as globals, and globals are generally a bad idea anyway. – Fred Larson Sep 28 '21 at 21:07