This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

TMS320F28P650DK: C2000 compiler fails to give error on duplicate global variables

Part Number: TMS320F28P650DK

Compiling a function:

long func(void)
{
long x,x,x,x,x;
x=1;
return x;
}
gives an error: error #102: "x" has already been declared in the current scope

But the following line declaring the variable globally (outside of a function) does not cause any warning or error:

long x,x,x,x,x;

  • Hi Alon,

    This behavior is almost certainly not a compiler bug — it's a consequence of how the C language standard handles declarations at file scope versus block scope.

    Why This Happens: Tentative Definitions in C

    In the C standard (C89/C99/C11), a declaration at file scope (global) without an initializer is treated as a tentative definition. The standard explicitly allows multiple tentative definitions of the same identifier within a single translation unit, as long as they are compatible types. At the end of the translation unit, if no external definition has appeared, the tentative definition becomes a full definition.

    So this at file scope:

    long x,x,x,x,x;

    is equivalent to writing:

    long x;
    long x;
    long x;
    long x;
    long x;

    Each of these is a valid tentative definition under the C standard, and the compiler is correct not to flag it as an error.

    Why It Errors Inside a Function

    Inside a block (function body), declarations are not tentative definitions — they are ordinary declarations subject to the rule that an identifier cannot be declared more than once in the same scope. Hence error #102 is correctly issued for:

    long func(void) {long x,x,x,x,x; // error #102}

    What You Can Do

    • Enable stricter modes: If the C2000 compiler supports a --strict_ansi or similar flag, or if you can compile in C++ mode, duplicate file-scope declarations may be flagged.
    • Use initializers: If you write long x = 0, x = 0; at file scope, this should produce an error because you'd have multiple external definitions of the same object, which violates the one-definition rule.
    • Static analysis tools: Tools like PC-Lint or Polyspace can catch this pattern if it's unintentional in your codebase.

    Summary

    The compiler is behaving in accordance with the C language standard. The asymmetry between file-scope and block-scope behavior is a well-known subtlety of C's tentative definition rules, not a defect in the C2000 toolchain.


    To help confirm this further, it would be useful to know:

    • Which C2000 compiler version and build number you're using
    • Whether you're compiling in C89, C99, or C11 mode (e.g., --c99 flag)
    • Whether enabling --strict_ansi or increasing the diagnostic level (e.g., --diag_warning) changes the behavior

    These details could help determine if there's a way to get the compiler to emit at least a warning for this pattern, even though it's technically standards-compliant.

    Best Regards,

    Zackary Fleenor