Skip to main content
Engineering LibreTexts

1.1: Building a C++ Program

  • Page ID
  • 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...