Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There is nothing wrong with the layout you are suggesting. Perhaps some clarification on what <strong>extern</strong> and <strong>#include</strong> do would be helpful. 1) <strong>#include</strong> is a preprocessor directive which essentially says: `take the named file and pretend it is pasted in place of this directive'</p> <p>2) <strong>extern</strong> is a C reserved word. Not to get into too many technicalities, but its meaning is: `the variable named in this statement is defined in a different place'. The space for a variable is reserved by the compiler exactly once, so if a function needs access to the variable in question, some information is needed before the definition is seen by the compiler. An <strong>extern</strong> declaration has enough information for the function to use the variable and the linker makes sure that a correct variable is used at a later stage.</p> <p>So in your scenario, the file with type definitions will be <strong>#include</strong>'d in every file that refers to those types. If you want to collect all the variable definitions in one file, which will be compiled separately from other parts of your project, any file that uses those variables and will be compiled separately, needs to be supplied an <strong>extern</strong> declaration for each variable defined elsewhere. Note that if you simply include th file with variable definitions, the compiler will see the definition twice (first in the file with the definitions, then in the file that includes it) and assume you are trying to define each variable twice and will issue an error.</p> <p>Finally, here is a simple scenario (it does not really make sense and is in bad style):</p> <p>a.c---------</p> <pre><code>#include "t.h" mytype a; mytype b; int f( int x, int y ) { return (x + y)*a - b; } </code></pre> <p>m.c---------</p> <pre><code>#include &lt;stdio.h&gt; // for stdout #include "t.h" #include "v.h" int main () { fprintf( stdout, "%d", a + b - f(1, 2) ); return 0; } </code></pre> <p>t.h-----------</p> <pre><code>typedef int mytype; </code></pre> <p>v.h-----------</p> <pre><code>#include "t.h" extern mytype a, b; int f( int, int ); </code></pre> <p>v.h and t.h can be combined (it is a question of style and the project requirements). Note that a declaration of f in v.h has an implied extern in front of it.</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. VO
      singulars
      1. This table or related slice is empty.
    2. 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