Skip to main content
Engineering LibreTexts

1.1: Building a C++ Program

  • Page ID
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)

    ( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\id}{\mathrm{id}}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\kernel}{\mathrm{null}\,}\)

    \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\)

    \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\)

    \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    \( \newcommand{\vectorA}[1]{\vec{#1}}      % arrow\)

    \( \newcommand{\vectorAt}[1]{\vec{\text{#1}}}      % arrow\)

    \( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vectorC}[1]{\textbf{#1}} \)

    \( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)

    \( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)

    \( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)

    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    What's in a File?

    There is nothing special about a file that contains computer source code. It is simply a text file that contains text written in a specific syntax that defines what language is being used. So, in reality you can use any text editor to write your code - something as simple as Notepad, or for you Linux fans out there I used to write all my code in Vi (now known mostly as vim). However, I prefer to use an integrated development environment, more commonly referred to as an IDE. My personal favorite right now is MS Visual Studio Code (this is the community edition - its FREE). There are several IDE's to choose from, you should look around, and find one you like and use it. 

    It is the convention that source code have a specific extension on the filename to signify what language the code is written in. For C++ the extension is .cpp. Now if you have C++ code in a file with an extension of, lets say .xyz, it will still compile and run, but anyone looking at your files would have no idea it is C++ code. You may also have header files which have a .h extension, but more on that in a bit.

    Back to our source code may be thinking (at least I hope you are), "Wait, the computer doesn't execute a text file??!!". You are correct. The file that contains the computer source code must go through a translation process that creates the file that we can then run on our computer. The term "compiling" is often used to refer to the entire process of translating the source doe to executable code. However, there is an entire series of steps that actually occur during this process. We will briefly review the three major steps individually: preprocessing, compiling, and linking. 

    From a broad perspective the compiling process can include multiple source code files, perhaps one or more header files, and generates an object file for each of the source files. These object files have their own extension: a .o in Linux, or a .obj file for Windows. Once we have the object files created they are all pulled together and linked to the system libraries, which include certain functions necessary for the program to run. The output is a file that is able to execute on the specific processor and operating system it needs to run on.

    Let's move along and take a deeper dive into this entire proces...

    This page titled 1.1: Building a C++ Program is shared under a CC BY-SA license and was authored, remixed, and/or curated by Patrick McClanahan.