This is a minimal example to reproduce a linker error from a program that worked in C but failed when compiling with C++ (gcc/g++ 8.1.0). I already know how to solve it using extern, what i want to find out is why it results in a linker error only in C++:
main.c
#include <stdio.h>
int var; //a definition in C++, declaration in C?
int main()
{
printf("%i\n", var);
return 0;
}
sec.c
int var = 10;
Just so i haven't misunderstood the terms:
declaration: allocating memory for a variable
definition: declaring and assigning it a meaningful value
This will compile with no complaints in C, but throw a multiple definition error in C++.
My running theory is that this is because C++ considers int var;
a definition while C only considers it a declaration (maybe by assuming extern in main.c) this is only an assumption through experimentation (and i don't think it's right), i want to find out how it actually functions according to documentation.
I couldn't tell from the sources i looked at how exactly it interprets int var;
between the two languages.
There are two supposed definitions of var, why does it compile in C but not C++?