c extern undefined reference [duplicate] - c

This question already has answers here:
Why is creating a variable using 'extern' a declaration and not a definition?
(5 answers)
Closed 2 years ago.
I'm sorry for what I'm sure is a simple mistake. But after a few hours I can't figure out what I'm doing wrong. I understand that extern needs to be declared outside a function and defined within a function. But I can't get it to work.
Here is my minimal code error.
extern double d;
int main(void) {
d = 0;
return 0;
}
/home/0KzRYK/ccCTD3Lf.o: In function `main':
prog.c:(.text.startup+0x3): undefined reference to `d'
collect2: error: ld returned 1 exit status

Thank you all for the comments. My mistake was to assume that extern double d defines the variable in the same way as double d. But apparently it doesn't.
I would say this is a non-intuitive property of the language, at least for a beginner (eg. extern double isn't a definition but static double is).
Following your comments I found a related question that talks exactly about this: Why is creating a variable using 'extern' a declaration and not a definition?

Your understanding is wrong. Variables with extern need not be defined within a function. But it must be defined somewhere (maybe another source file).
Example:
main.c:
extern double d;
int main(void) {
d = 0;
return 0;
}
d.c:
double d;
compilation:
gcc -o main main.c d.c

Related

compiler error when returning reference in c, but works in c++ [duplicate]

This question already has answers here:
Does C have references?
(2 answers)
Closed 4 years ago.
I'm trying to return reference to variable in c code, however this code:
int& f()
{
static int l = 10;
return l;
}
doesn't compile via gcc:
main.c:5:4: error: expected identifier or ‘(’ before ‘&’ token
int& f()
But compiles well with g++. How can I achieve this in c?
C doesn't know what a reference is, so it gives up parsing at that point.
If you're compiling this "C" code in C++ mode then it will work.
C doesn't support references. To perform something similar, you need to return a pointer:
int *f()
{
static int l = 10;
return &l;
}

Behavior of extern keyword in C [duplicate]

This question already has an answer here:
Why extern int a ; initialization giving error locally but not globally? [duplicate]
(1 answer)
Closed 5 years ago.
I surprised when compiled following program on GCC compiler. It's successfully worked. Compiler gives only warning.
warning: 'i' initialized and declared 'extern' [enabled by default] extern int i = 10; ^
My code:
#include <stdio.h>
//Compiler version gcc 4.9
extern int i = 10;
int main()
{
printf("%d\n",i);
return 0;
}
Output:
10
Why doesn't give compiler error? Is it undefined behavior?
You should not put your main function body in a header, but rather in a .c-file. Vice versa, you should not put extern in a .c-file but only in a header. That is the difference between declaration and definition.
Extern means, make this variable known, but there is no memory to be reserved for it. The compiler now says: Ok, I know you want this variable to be used, but it is only promised to be there, not actually defined.
The compiler anyway does not know about other objects (other .c-files) that might define this variable. So it keeps it to the linker to actually try to gather all variables.
If the linker now does not find that variable elsewhere, it implicitly makes the variable local, but warns about the broken C-standard.

extern storage class variable inside main function [duplicate]

This question already has answers here:
How do I use extern to share variables between source files?
(19 answers)
Closed 7 years ago.
I get very confused with the static and extern storage classes. I do not understand what is wrong with the below code snippet. I expect the printf to print the value Zero. The build is failing with the error "Undefined reference to 'i' ". I expect the statement "extern int i" to be a valid C statement. Is it not?
#include<stdio.h>
void main()
{
extern int i;
printf("%d", i);
}
In the function main
extern int i;
is a declaration of i, not definition. It must be defined somewhere.
#include<stdio.h>
int i; //definition
int main()
{
extern int i; //declaration
printf("%d", i);
}
In this example, the declaration is valid, but can be omitted.
When you declare a variable as extern inside a function, the compiler thinks that the variable is defined in some other translation unit. If it's not defined anywhere else, then you will get a linker error saying that the linker can't find the variable.
see when you are using extern storage class in the main then our compiler use to search the declaration of the variable in the perticular location ,here extern stands for compiler that this variable is declared at any location in the program it can be local or outside the scope , if it dont found any declaration then it gives this linking error becoz it is unable to found the variable declaration.

Double definition of variable [duplicate]

This question already has answers here:
About Tentative definition
(2 answers)
Closed 9 years ago.
As we know extern declaration of variables can be initialized.
#include<stdio.h>
extern int a=5; //will work as both definition and declaration.
int main()
{
}
Why this programme is compiling and running with no error.
extern int a=5; //will work as both definition and declaration.
int main()
{
}
int a; // isn't it second definition??
C has a concept of a "tentative definition". Most definitions without initializers are "tentative", so an arbitrary number of them are allowed, as long as they don't conflict with each other. At the end of the translation unit, the definition of the variable becomes basically the composite of those (e.g., if one definition says the variable is const and another says it's volatile, the variable will end up as both const and volatile.
A definition with an initializer is never tentative, so only one of them is allowed.

extern with global definition of variable in c

I have the following source code which interests me.
#include <stdio.h>
extern int foo;
int foo = 32;
int main()
{
printf("%d", foo);
}
This a perfectly normal piece of code, and when I compile it with
gcc -Wall -Wextra -pedantic foo.c
I get no warnings.
And it seems weird, because a variable is defined both as external, and also global in the same file.
I'm quite sure that it's easy to the linker to find the reference for the external variable in the same file, but doesn't it look like a coding error? And if so, why doesn't the compiler warn about this?
There's nothing weird. You first made a declaration of a variable (you promised the compiler that it exist) and then you actually defined it. There's no problem in that.
Also, by default, all variables that aren't local to functions and aren't defined as static are extern.
You seem to misunderstand what extern does. extern simply makes your declaration just a declaration instead of a definition.
int i; //definition of i
extern int i; //declaration of i
It is perfectly normal to have multiple declarations of the same variable, but only one definition should be present in the whole program. Compare this with a function
void f(void); //declaration
void f(void) //definition(and redeclaration)
{
} //definition
In order to use a variable or function, you only need its declaration. Its definition may appear anywhere in the program (the linker will find it). Anywhere can be the same file, another file, or even an external library.
And it's seems weired, because a variable is defined both as external, and also global in the same file.
extern int foo;
says: it declares without defining an object of type int named foo.
int foo = 32;
it declares and defines an object of type int named foo with external linkage.
There is no contradiction and it is valid C code.
The difference is that the former is a declaration -> extern declares a variable and says it will be available somewhere around. You can have as many declarations as you want and the latter is a definition which must be there exactly once.
So there should be no warning and no error.
extern is a way to provide visibility to a variable that is defined elsewhere...
extern is like a promise...
in example.h
extern int g;// promises that this will be in the object code to anything that includes example.h
example. c
int g;

Resources