C Undefined reference - c

I got some problem with the following codes particularly in header.c where i can't access the extern int x variable in header.h... Why? Does extern variable in .h not global? How can i use this on other files?
===header.h===
#ifndef HDR_H
#define HDR_H
extern int x;
void function();
#endif
===header.c===
#include <stdio.h>
#include "header.h"
void function()
{
printf("%d", x); //****undefined reference to x, why?****
}
===sample.c===
int main()
{
int x = 1;
function();
printf("\n%d", x);
return 0;
}

The declaration
extern int x;
tells the compiler that in some source file there will be a global variable named x. However, in the main function you declare a local variable x. Move that declaration outside of main to make it global.

The extern keyword say the variable exists but does not create it. The compiler expects that another module will have a global variable with that name, and the linker will do the right thing to join them up.
You need to change sample.c like this:
/* x is a global exported from sample.c */
int x = 1;
int main()
{
function();
printf("\n%d", x);
return 0;
}

extern declares a variable, but does not define it. It basically tells the compiler there is a definition for x somewhere else. To fix add the following to header.c (or some other .c file but only one .c file):
int x;
Note that in main() the local variable x will hide the global variable x.

Indeed extern int x; means x will be defined in another place/translation unit.
The compiler expects to find a definition of x in the global scope somewherelse.

I would reorganise/modify your code like this and get rid of header.c
===sample.h===
#ifndef SAMPLE_H
#define SAMPLE_H
extern int x;
void function();
#endif
===sample.c===
#include <stdio.h>
#include "sample.h"
int x;
void function()
{
printf("%d", x);
}
int main()
{
x = 1;
function();
printf("\n%d", x);
return 0;
}

Related

Example of extern global variable - Error

I'd like to understand which mistake i did in the following example. There are three file: main.c, libreria_mia.c and libreria_mia.h.
// main.c
#include <stdio.h>
#include "libreria_mia.h"
int x = 5;
int main()
{
int y = quadrato();
printf("%d\n", y);
return 0;
}
// libreria_mia.h
extern int x;
int quadrato(void);
// libreria_mia.c
int quadrato(void)
{
x = x * x;
}
Error:
libreria_mia.c:5:2: error: ‘x’ undeclared (first use in this function)
Thank you for your time.
When you compile libreria_mia.c, the compiler does not automatically know about libreria_mia.h or the declarations within it. To provide a declaration for x while compiling libreria_mia.c, libreria_mia.c must include a header that declares x or have a declaration of x directly in libreria_mia.c.
Additionally, it is conventional for a header named file.h to declare things defined in file.c (not necessarily all things defined in file.c, just those intended to be used outside it). But you have x declared in libreria_mia.h but defined in main.c. Normally, one would either define x in libreria_mia.c or declare it in main.h, and usually the former as main.c is more commonly a user of all other things in the program rather than a provider.

Is variable declaration necessary in C?

See the comments to see what is being referred as declaration.
If the whole variable declaration part was missing, what would be the problem?
Appears that variable definition and initialization either simultaneously or separately like in the example would suffice.
#include <stdio.h>
// Variable declaration:
extern int a, b;
extern int c;
extern float f;
int main () {
/* variable definition: */
int a, b;
int c;
float f;
/* actual initialization */
a = 10;
b = 20;
c = a + b;
printf("value of c : %d \n", c);
f = 70.0/3.0;
printf("value of f : %f \n", f);
return 0;
}
If the declaration was missing then it would create no problem in main function since the locally defined variables i.e. a,b,c,f will be used in the functionality of main till its scope ends.
The declaration merely tells that the definition lies elsewhere (in some other .c file) or the definition lies after the function main in the same .c file.
There will be no problem here if the mentioned declaration is missing.
// Variable declaration:
extern int a, b;
extern int c;
extern float f;
This tells the compiler that these variables are defined somewhere else(in another file).
/* variable definition: */
int a, b;
int c;
float f;
This is where you define variables but they are not the same as the external variables you declared since they are in the inner scope of the main function.
The scope is the place where variables live. extern keyword notes that the scope is global.
You can define variables with the same name in an inner scope and access only them as you did in the main function but it's not a good practice.
void foo()
{
int a = 5;
printf("%d\n", a); // 5
// Creating an inner scope
{
int a = 20;
printf("%d\n", a); // 20
}
printf("%d\n", a); // 5
}
The correct way to use the extern keyword with variables is like this.
//h1.h
extern int global_var; // Declaration of the variable
//c1.c
#include h1.h
int global_var = 0; // Definition of the global var. Memory is allocated here.
//main.c
#include h1.h
int main()
{
printf("global var value is %d\n", global_var); // use of the var defined and
// initialized in c1.c
return 0;
}
This program will print 0 since the variable is defined and initialized in c1.c.
Extern extends the visibility of the C variables and C functions. so that lets the compiler know that there is another place that those vars are declared and memory was allocated for them elsewhere.
for example in another c file.
if you compile a c file containing a global var for example:
int c = 5;
and you create a function on you c file that uses this c var, for example:
int someFunc(void){
return c;}
if you run someFunc in your main and print its return value, you will get 5. but you must compile both c files together.
in your program, you only use the locally allocated var declared in your main function.
When it comes to simple variables, there is really no difference between the declaration and definition. There is a difference when it comes to structs and functions. Here is an example:
// Declarations
struct myStruct;
int foo();
int main()
{
...
}
// Definitions
struct myStruct {
int a, b;
};
int foo() {
return 42;
}
In your case, you are hiding the previous declarations so that they are not accessible before the end of the scope. This is commonly called shadowing. It's basically the same thing as this:
int main()
{
int i=0;
printf("i: %d\n", i);
{
int i=42; // Now the previous i is inaccessible within this scope
printf("i: %d\n", i);
}
// And now it is accessible again
printf("i: %d\n", i);
}

Global value is not acessable in another file? [duplicate]

This question already has answers here:
How do I use extern to share variables between source files?
(19 answers)
Closed 8 years ago.
Global value is not accessible in another file?Mycode is below please help me to fix
flie1.c
#include<stdio.h>
extern int i=9;
int main()
{
printf("i m in main\n");
}
file2.c
printf("%d\n",i);
i am compiling both file at once as cc file1.c file2.c
Change it like this, and it will work:
file1.c
#include <stdio.h>
int i = 9; // define the variable
void print(); // declare print: say there is a function called `print` with no arguments and no return type (because the function is DEFINED in file2.c)
int main() {
printf("im in main");
print();
return 0;
}
file2.c
extern int i; // declare "i": say there is a variable called `i` with type of `int` (because the variable is DEFINED in file1.c)
void print() {
printf("%d\n", i);
}
When you have an extern variable, it should also have its 'original' declaration (without using extern). extern simply says ' this variable is defined elsewhere', so you need to define the variable somewhere.
Simply add:
int i=9;
to file2.c (in the top of the file, the 'globals' area')
and you can change your extern declaration to:
extern int i;
(without the assignment of the value 9) in file1.c

what is the difference between the two codes?

this is the first code
#include <stdio.h>
void a();
int i;
int main()
{
printf("%d\n",i);
a();
return 0;
}
int i=5;
void a()
{
printf("%d\n",i);
}
output
5
5
the second code is
#include <stdio.h>
void a();
extern int i;
int main()
{
printf("%d\n",i);
a();
return 0;
}
int i=5;
void a()
{
printf("%d\n",i);
}
ouput
5
5
what is the difference between the codes if their outputs are same...
and if they are same then what is the use of extern
extern is something like saying to the Linker that you have defined the variable in some other file( even global variables can be made extern inside other functions in same file) don't throw error now you will find the variable later then link it .
using extern your only declaring the variable not defining it(no memory allocated).
In Your second program for first declartion
extern int i;
No memory is allocated but later when you declare
int i = 5 ;
the memory is allocated for i. while linker searches for i in printf if i is not declared extern it throws a error since its extern linking takes place when it finds the defintion of i.
extern means that you are declaring the variable but not defining it (allocating memory for it). Have a look at this link for a good explanation of extern: http://www.geeksforgeeks.org/understanding-extern-keyword-in-c/

how to use extern in C?

I have this code:
#include <stdio.h>
extern int x;
void a() {
int x = 100;
printf("%d ",x );
x += 5;
}
void b() {
static int x = -10;
printf("%d ", x);
x += 5;
}
void c(){
printf("%d ", x);
x += 2;
}
int main() {
int x = 10;
a();
b();
c();
a();
b();
c();
printf("%d ", x);
getchar();
return 0;
}
int x = 0;
I was sure that the fact that extern in declared here, I will have a compilation error - but everything passed.
also , what is the meaning of extern when it's inside the C file itself? shouldn't it be in another file?
Is there a way to declare this variable in order for this not to compile?
The extern keyword declares a variable, and tells the compiler there is a definition for it elsewhere. In the case of the posted code, the definition of x occurs after main(). If you remove the int x = 0; after main() the code will not build (it will compile but will fail to link due to undefined symbol x).
extern is commonly used to declare variables (or functions) in header files and have the definition in a separate source (.c) file to make the same variable available to multiple translation units (and avoid multiple definition errors):
/* my.h */
#ifndef MY_HEADER
#define MY_HEADER
extern int x;
#endif
/* my.c */
#include "my.h"
int x = 0;
Note that the declaration of x in functions a(), b() and main() hide the global variable x.
You have a declaration for an identifier at file scope, so if no other declaration for the identifier would've been existing at file scope, the identifier would have had and external linkage. But, you've defined the identifier at file scope at the last line, in the pasted code.
So,extern int x;
refers to the globally defined: int x = 0; at the bottom of your file. :)
If you run this code you should get x's value as 2 and subsequently 4 because the externed x variable refers to the int x=0 after the main().
Extern is used for declaration a variable in a compilation unit, this variable was defined in other compilation unit.
What is the difference between a definition and a declaration?
For functions it is optional.
Read: http://en.wikipedia.org/wiki/External_variable
In your piece of code, each of the three function uses another 'i'. Only c() uses the global x.

Resources