0.5 — Introduction to the compiler, linker, and libraries

Continuing our discussion of this diagram from the previous lesson (0.4 -- Introduction to C++ development):

The software development process

Let’s discuss steps 4-7.

Step 4: Compiling your source code

In order to compile a C++ program, we use a C++ compiler. The C++ compiler sequentially goes through each source code (.cpp) file in your program and does two important tasks:

First, it checks your code to make sure it follows the rules of the C++ language. If it does not, the compiler will give you an error (and the corresponding line number) to help pinpoint what needs fixing. The compilation process will also be aborted until the error is fixed.

Second, it translates your C++ source code into a machine language file called an object file. Object files are typically named name.o or name.obj, where name is the same name as the .cpp file it was produced from.

If your program had 3 .cpp files, the compiler would generate 3 object files:

The compilation process

C++ compilers are available for many different operating systems. We will discuss installing a compiler shortly, so there is no need to do so now.

Step 5: Linking object files and libraries

After the compiler creates one or more object files, then another program called the linker kicks in. The job of the linker is three fold:

First, to take all the object files generated by the compiler and combine them into a single executable program.

The linking process

Second, in addition to being able to link object files, the linker also is capable of linking library files. A library file is a collection of precompiled code that has been “packaged up” for reuse in other programs.

The C++ core language is actually fairly small and concise (and you’ll learn much of it in these tutorials). However, C++ also comes with an extensive library called the C++ Standard Library (usually shortened to standard library) that provides additional functionality that you can use in your programs. One of the most commonly used parts of the C++ standard library is the iostream library, which contains functionality for printing text on a monitor and getting keyboard input from a user. Almost every C++ program written utilizes the standard library in some form, so it’s very common for the standard library to get linked into your programs. Most linkers will automatically link in the standard library as soon as you use any part of it, so this generally isn’t something you need to worry about.

You can also optionally link in other libraries. For example, if you were going to write a program that played sounds, you probably would not want to write your own code to read in the sound files from disk, check to ensure they were valid, or figure out how to route the sound data to the operating system or hardware to play through the speaker -- that would be a lot of work! Instead, you’d probably download a library that already knew how to do those things, and use that. We’ll talk about how to link in libraries (and create your own!) in the appendix.

Third, the linker makes sure all cross-file dependencies are resolved properly. For example, if you define something in one .cpp file, and then use it in another .cpp file, the linker connects the two together. If the linker is unable to connect a reference to something with its definition, you’ll get a linker error, and the linking process will abort.

Once the linker is finished linking all the object files and libraries (assuming all goes well), you will have an executable file that you can then run!

For advanced readers

For complex projects, some development environments use a makefile, which is a file that describes how to build a program (e.g. which files to compile and link, or otherwise process in various ways). Entire books have been written about how to write and maintain makefiles, and they can be an incredibly powerful tool. However, because makefiles are not part of the C++ core language, nor do you need to use them to proceed, we’ll not discuss them as part of this tutorial series.

Steps 6 & 7: Testing and Debugging

This is the fun part (hopefully)! You are able to run your executable and see whether it produces the output you were expecting!

If your program runs but doesn’t work correctly, then it’s time for some debugging to figure out what’s wrong. We will discuss how to test your programs and how to debug them in more detail soon.

Integrated development environments (IDEs)

Note that steps 3, 4, 5, and 7 all involve software (editor, compiler, linker, debugger). While you can use separate programs for each of these functions, a software package known as an integrated development environment (IDE) bundles and integrates all of these features together. We’ll discuss IDEs, and install one, in the next section.

0.6 -- Installing an Integrated Development Environment (IDE)
0.4 -- Introduction to C++ development

20 comments to 0.5 — Introduction to the compiler, linker, and libraries

  • Arend

    where is appendix ;-;

  • Logan

    Hi Alex, I am a returning student; it has been sometime since I had a crack at C++. So I am getting back to basics and following your tutorial; which is great and very appreciated!

    I am curious ask about the library "using namespace std;."  I saw many other videos showing how to program in C++ using that library. I understand it must add to the memory if you include it, and some people mentioned problems using it; if you have big program. I believe they were referring to the possibility of functions being updated and conflicts with the older updates of the library. Some programmers said they got used to typing the extra code, and were better off without the library.  I wanted to ask you, what is the main problem using it? And what does the industry do with regards to this library? Do they avoid using it in companies?

    • doesn't mean that you want to use the @std library. You can always use the @std library, if you include a header from it. The "using" statement allows you to access the members of @std without using the "std::" prefix. eg.

      This can lead to name conflicts, if you have an object with the same name as an object in the @std library.

      • Logan

        Is that a realistic problem just name conflicts? I thought there were other bigger issues at work here. Again you or anyone what does the industry use? How is this deemed in the actual development world as opposed to what universities teach?

        • > I thought there were other bigger issues at work here.
          No, just name conflicts.

          "using namespace" shouldn't be and mostly isn't used in practice. Of course, there will be that one guy or company who uses it.
          A lot of teaching institutes use "using namespace", because it makes the code more readable and thus friendly for beginners. This comes with the downside of teaching bad practice.

  • Puya

    Grateful reader here. I've heard this phrase that we cannot debug some issue because "we don't have the symbols" for some library (e.g. when stepping through code). What does that mean? Is that related to having the source code?

    • Hi!

      When compiling C++, pretty much all information about the source code is lost, because your computer doesn't need to know how you named things or what your file structure was.
      In debug mode, symbols can be added to the binary. Those are the names of functions, variables, etc., which of course help immensely when debugging.
      You can still debug programs without symbols, but you'll have to figure out what is what on your own.

  • Robert Young

    Comlete newbie here, feeling ambitious about maybe trying to make my own music recording software. Is this possible with C & C++ orwill I need other languages

  • Bruno

    Hey, Alex!

    I'm loving this tutorial, ty so much for this. I have some question tho. Is it ok if i print it? I'm making a "summary", don't know the right word for it, as i study these. At the end i'm gonna have what i wrote down from the tutorial and i think it's wise to also have the source stored with it. I know its silly to ask but i just feel like i should. Also, i bought this book "C++ Primer Plus - 5th Ed - PHE - Pearson Higher Education" to help me with the learning. Is it a good idea to use it as i go through the tutorial. Ty again so much for this :D.

    • Alex

      You are free to print the tutorials for your own personal use, so long as you do not distribute them. :) Have fun!

      It's always best to learn from multiple sources, as they will give you different perspectives.

  • Phil

    "If the linker is unable to connect a reference to a thing with the definition of that thing, you’ll get a linker error, and the linking process will abort."

    would sound better as:

    "If the linker is unable to connect a referenced thing with its definition, you’ll get a linker error, and the linking process will abort.

  • Hassan Muhammad

    Hi Alex,
    I am new to c++;
    assuming I'm n't interested in other libraries and i want to build my own independent OS, can i possibly write my own code to implement it with my own libraries/code without those libraries?

    • Alex

      Sure. You can avoid use of any of the code in the standard library (or any other library) and only use the code you've written yourself.

      This generally isn't recommended, because the code in the standard library is useful and tested, and using it will dramatically shorten the time it takes to write functional programs.

  • Rob

    Ah, the English language is alive and well as long as we have people like Ryan!

    And with such great attention to detail, I expect that you’ No slouch as a programmer.

  • Ryan

    Hello! I just wanted to point out a couple small typos I noticed. In the Step 5 section, you used 'a' instead of 'an' in "However, C++ also comes with a extensive library". In the box at the bottom, I think you meant to say something like 'describing', or 'which is a file that describes' in "which is a file describes how to build a program".

Leave a Comment

Put all code inside code tags: [code]your code here[/code]