Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><code>#pragma once</code> instructs the preprocessor not to include a header file twice. This is mostly used to prevent recursive inclusions and multiple indirect inclusions, e.g.:</p> <pre><code>#include &lt;a.h&gt; // a.h already includes b.h #include &lt;b.h&gt; </code></pre> <p>Without <code>#pragma once</code> at the beginning of <code>b.h</code>, its content will get included twice and possibly lead to redefinition of some symbols.</p> <p>What happens in your case is an entirely different matter. By default functions in C and C++ have external linkage. It means that if you have function <code>foo()</code> defined in file <code>bar.c</code> and then you compile <code>bar.c</code> to the object file <code>bar.o</code>, the object file exports a global symbol by the name of <code>foo</code> (actually C++ will decorate the name in order to support overloading), which symbol can be accessed (referred to) from other object files. Now if file <code>baz.c</code> contains the definition of another function <code>foo()</code> (which in the case of C++ has the same signature), the object file <code>baz.o</code> also exports a global symbol by the name of <code>foo</code>. When the object files get linked together in order to produce an executable file, the linker tries to resolve each symbol to a unique memory address. But now there is a problem: there are two symbols <code>foo</code> and they both have different addresses. The linker is (usually) not a psychic, so it simply gives you an error message about symbol redefinition and terminates.</p> <p>Both C and C++ provide a mechanism to control the linkage of functions. If you add the <code>static</code> keyword, the function symbol is no longer global and become only visible to the code that shares the same unit of compilation. Such functions have static linkage. That's why functions that are defined in header files almost always come with the <code>static</code> keyword:</p> <pre><code>#pragma once #include &lt;mpi.h&gt; #include &lt;cstddef&gt; struct ProcessStats { int rank, itLeft, crtIt, processFlag; float speed; }; static MPI_Datatype MPI_Cust_ProcessStats_create() { ... } </code></pre> <p>Now <code>MPI_Cust_ProcessStats_create()</code> is only going to be visible in the source file that includes the header file.</p> <p>Unsolicited advice: The <code>MPI_</code> prefix is reserved for MPI API calls. Using it for user functions is a bad programming practice since there are tools that rely on the fact that only MPI calls begin with <code>MPI_</code> and might get confused.</p>
 

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