0
\$\begingroup\$

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?

\$\endgroup\$
4
  • 5
    \$\begingroup\$ You are not allowed to use same name for global variables, unless these are static. This is standard C, not specific to XC8 \$\endgroup\$
    – Eugene Sh.
    Jun 30 '20 at 15:02
  • \$\begingroup\$ @EugeneSh. Thanks for this. I just read up on the "static" keyword and it makes sense now. If i do use the "static" keyword to limit the scope to a single source file, i am assuming references in the source wont conflict right ? \$\endgroup\$
    – Cerezo
    Jun 30 '20 at 15:31
  • \$\begingroup\$ Even with staticyou should'nt use the same name for different variables for readability. \$\endgroup\$
    – Mike
    Jul 2 '20 at 4:41
  • \$\begingroup\$ @Mike In my scenario, i have different source files that i manage as "classes / objects". A variable name like "count" used in each context should not affect readability. or ? \$\endgroup\$
    – Cerezo
    Jul 6 '20 at 16:16
2
\$\begingroup\$

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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.