With ARM GCC, I got this error message:
/.../foobar.c:376:9: error: redeclaration of 'myVariable' with no linkage
376 | float myVariable;
| ^~~~~~~
/.../foobar.c:365:9: note: previous declaration of 'myVariable' was here
365 | float myVariable;
| ^~~~~~~
I understand how to fix the error - this is not a duplicate of 1, 2, 3, or 4. 5 quotes the spec but still doesn't say what linkage is, 6 also explains the cause of the error but doesn't define "linkage"
I don't understand what "linkage" refers to here? I haven't seen this terminology before, and am looking for an explanation of what linkage is referring to, and why a redeclaration of 'myVariable' would be okay if there was linkage.
Linkage determines whether multiple declarations of an identifier (such as a variable or function) refer to the same entity or different entities across translation units or within the same translation unit.
When you declare a variable inside a function without extern
, it has no linkage. That means each declaration creates a completely separate variable in the same function scope, which is not allowed.
If myVariable is meant to be the same across multiple lines inside the function, remove the second declaration and declare it at file scope (outside the functions.)