Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>"extern" tells the C compiler this function or variable was declared elsewhere, so you can use them even without declare them in the header files and include .h in .c files. Beware, "extern" means this variable was NOT the initial declare, the initial declare MUST NOT have the extern keyword. </p> <p>In your case, you declared an extern variable in one file without add the "static" keyword, so the variable's scope will be the whole project. Then you declared "uint16_t externVariable;" at 2 places, the complier thought they were all initial declares have the same name, so a conflict begun.</p> <p>For example, you can do something like this:</p> <p>Example 1:</p> <blockquote> <p>a.c: uint16_t externVariable;</p> <p>b.c: extern uint16_t externVariable;</p> </blockquote> <p>Don't worry about your header file, you can use externVariable in both a.c and b.c.</p> <p>Example 2:</p> <blockquote> <p>a.c: #include "a.h"</p> <p>b.c: #include "a.h"</p> <p>a.h: uint16_t externVariable;</p> </blockquote> <p>This works.</p> <p>Example 3:</p> <blockquote> <p>a.c: extern uint16_t externVariable;</p> <p>b.c: uint16_t externVariable;</p> <p>c.c: uint16_t externVariable;</p> </blockquote> <p>This won't work since you declared externVariable at multiple places.</p> <p>Example 4:</p> <blockquote> <p>a.c: static uint16_t externVariable;</p> <p>b.c: static uint16_t externVariable;</p> </blockquote> <p>This WILL work since "static" keyword limited their scopes so they won't conflict.</p> <p>Example 5:</p> <blockquote> <p>a.c: static uint16_t externVariable;</p> <p>b.c: static uint16_t externVariable;</p> <p>c.c: extern uint16_t externVariable;</p> </blockquote> <p>This still won't work since "static" keyword limited their scopes, and the complier can't find where you declared externVariable in c.c.</p> <p>You can also view TCPL, it gave a bigger picture about these keywords, hope these examples can help :)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload