I am currently working with a PIC18F25k80 device using the XC8 compiler and MPLABX.
I spent the whole of yesterday trying to debug some code I ported from a PIC32 micro project. This code did not interact with any hardware, and was split into 2 different source files.
Eventually, I was able to solve the issue by identifying that the XC8 compiler produces unexpected results when variables of the same name exist in different source files. NOTE: I did not use the "extern" keyword, as I needed the variables to be logically different across the source files.
I had to prefix all my my variables with a source file specific prefix.
Is this a known issue with the XC8 compilers?
1 Answer 1
That is perfectly normal under any compiler.
By default, if you declare variable X in modules A and B then these will be the same variable, as by default these will have extern
linkage, unless otherwise told with static
linkage.
You would see that if you declare the variables with initial value, i.e. int X = 3;
in module A
and int X = 4;
in module B
, then the compiler will complain that you can't initialize the same variable twice.
static
. This is standard C, not specific to XC8 \$\endgroup\$static
you should'nt use the same name for different variables for readability. \$\endgroup\$