-2

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
StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
Akim
  • 147
  • 1
  • 8
  • You only have a declaration, no definition. – JCWasmx86 Sep 02 '20 at 13:26
  • isn't d=0 a definition? – Akim Sep 02 '20 at 13:28
  • It's an assignment – JCWasmx86 Sep 02 '20 at 13:29
  • @Akim Can you really talk about definitions for simple variables? I would just call it an assignment. – klutt Sep 02 '20 at 13:29
  • Ok, but I still don't understand. The following answer equates declaration with definition. According to it ```extern double d``` is declaration or definition. Which means d is defined in my case? https://stackoverflow.com/questions/671925/what-exactly-are-c-definitions-declarations-and-assignments – Akim Sep 02 '20 at 13:33
  • `extern` is a promise to the compiler that the variable is defined somewhere else and will be supplied at linking. The classic example is `errno`. – stark Sep 02 '20 at 13:34
  • Bettter link is here: https://stackoverflow.com/q/46988095/1216776 – stark Sep 02 '20 at 13:37
  • thank you! here is another i just found that deals specifically with extern: https://stackoverflow.com/questions/11507423/why-is-creating-a-variable-using-extern-a-declaration-and-not-a-definition – Akim Sep 02 '20 at 13:39
  • 1
    This does answer the question (confirmed by OP in their answer). [Why is creating a variable using 'extern' a declaration and not a definition?](https://stackoverflow.com/questions/11507423/why-is-creating-a-variable-using-extern-a-declaration-and-not-a-definition) I don't want the question deleted, but closed-as-duplicate is an appropriate state in my opinoin and all rep changes stay. – Yunnosch Sep 02 '20 at 13:46
  • yes, that seems to be the answer! – Akim Sep 02 '20 at 13:48
  • I would say it's a non-apparent property of the language, at least for a beginner (cf. `static double` which is a definition as far as I understand). – Akim Sep 02 '20 at 13:51

2 Answers2

1

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?

Akim
  • 147
  • 1
  • 8
0

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
MikeCAT
  • 73,922
  • 11
  • 45
  • 70