Note that there are some explanatory texts on larger screens.

plurals
  1. POUnderstanding C++ Compilation
    primarykey
    data
    text
    <p>I have recently become aware that I have no idea, genericly speaking, how a c/c++ compiler works. I will admit this initialy came from trying to understand header guards but came to the realization that I am lacking in how compiling works.</p> <p>Take Visual C++ for instance; Theres the "Header Files" folder, the "Resources Files" folder, and "Source Files" folder. Is there any significance to the separation of these folders and what you put in them? To me, they are all source files. Take the code snippets:</p> <h1>Snippet 1</h1> <pre><code>//a1.h int r=4; </code></pre> <p>and </p> <pre><code>//a1.cpp int b //&lt;--semicolon left out on purpose </code></pre> <p>and</p> <pre><code>//main.cpp #include &lt;iostream&gt; #include "a1.h" void main() { cout &lt;&lt; r; } </code></pre> <p>The compiler errors out saying "a1.cpp(3) : fatal error C1004: unexpected end-of-file found" where I would expect it wouldn't because the a1.cpp file is not #included where the main method exists where in the next code snippet</p> <h1>Snippet 2</h1> <pre><code>//a1.h int r=4 //&lt;--semicolon left out on purpose </code></pre> <p>and </p> <pre><code>//a1.cpp int b = 4; </code></pre> <p>and</p> <pre><code>//main.cpp #include &lt;iostream&gt; void main() { cout &lt;&lt; b; } </code></pre> <p>Errors out because "main.cpp(6) : error C2065: 'b' : undeclared identifier". If you include the a1.cpp like so</p> <h1>Snippet 3</h1> <pre><code>//a1.h int r=4 //&lt;--semicolon left out on purpose </code></pre> <p>and </p> <pre><code>//a1.cpp int b = 4; </code></pre> <p>and</p> <pre><code>//main.cpp #include &lt;iostream&gt; #include "a1.cpp" void main() { cout &lt;&lt; b; } </code></pre> <p>the compiler complains "a1.obj : error LNK2005: "int b" (?b@@3HA) already defined in main.obj". Both snippets 2 and 3 ignore the fact that <code>int r = 4</code> does not have a semicolon missing as I suspect that it has something to do with its a xxxx.h file. If I remove the a1.cpp file from the project on snippet 1, then it compiles fine. Clearly what I have expected is not what I am getting. Theres plenty of books and tutorials on how to code in cpp, but not much in the way cpp handles files and source code in the complition process. What on earth is going on here?</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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