0

I'm having some problems with extern variables. It's the first time I use it, so I don't know how it works. I have this code:

#include <stdlib.h>
#include <stdio.h>
#include <time.h>

extern double *x, *y, a;
extern unsigned int N;

inline void f1 ()
{
    int i;

    for (i=0; i<N; i++)
    {
        y[i] = a*x[i] + y[i];
    }
}

And the error that I get when I compile it it's that x,y,a and N are not defined. So, could someone help me please?

Kernel
  • 107
  • 1
  • 10
  • You declared them as extern, meaning that they are declared in some other compilation unit (aka source code file). As it seems that this is your only compilation unit, you never define them, so the linker is going to complain that they are not found. In this case you don't the `extern` keyword at all. In fact, you don't need global variables at all. – Pablo May 30 '20 at 11:23

2 Answers2

1
extern double *x, *y, a;
extern unsigned int N;

The extern qualifier for variables at global space tells the compiler/linker that the respective variable is defined in another preprocessing translation unit (another source file) instead of the actual one.

You have no other source file, so the compiler can´t find the definition of the variables anywhere. Thus, it throws an appropriate warning.

If you don´t have multiple source files, you don´t need to use the extern qualifier by *x, *y, a and N. Remove them.


Have a look at here:

How do I use extern to share variables between source files?

0

This program throws an error in compilation because *x,*y and a etc are declared but not defined anywhere. Essentially, *x,*y and a etc isn’t allocated any memory. And the program is trying to change the value of these variable that doesn’t exist at all.

checkout this link for better understanding of extern keyword. https://www.geeksforgeeks.org/understanding-extern-keyword-in-c/

Deviljee
  • 71
  • 1
  • 4