CS 70

Compilation Commands

As we saw in the video, to actually make compilation happen, we run the program clang++. Let's recap.

Compilation Stages: C++ source → Object File

We began with a source file, fourtwo.cpp, and ran the following command:

clang++ -c fourtwo.cpp

This command performed the first three steps in making an executable (preprocessing, compilation and assembly), and created an object file fourtwo.o.

  • Dog speaking

    Three steps done with just one command. That's cool!

  • LHS Cow speaking

    Yes, although it also does cause a bit of confusion. People often call all three steps “compiling” too. Because we just typed one command to do it, and compilation is the most important aspect.

  • Pig speaking

    Like saying “having a burger” might actually mean MORE, it might mean “having a burger, fries and a drink”.

  • LHS Cow speaking

    Exactly. You have to tell from context whether someone means the middle step in the three step process, or all three steps.

  • RHS Cow speaking

    Usually it's obvious. If someone says “what comes after preprocessing”, you'd say “compilation”. And we won't try to ask weird tricky questions where it's ambiguous.

  • Duck speaking

    And why is the option -c?

  • Bjarne speaking

    The first C compiler in the early 1970s used -c, so it is traditional.

  • LHS Cow speaking

    Yes, it's a decision that was made a long time ago, but you can think -c is for “Compile (only)”, if you like.

Linking Stage: Object File(s) → Executable

Then, in the video we created an executable fourtwo by running

clang++ -o fourtwo fourtwo.o
  • RHS Cow speaking

    We could have picked another name, -o prog42 would produce an executable called prog42.

  • Duck speaking

    I just tried it without any -o option to specify a name, and it called the executable a.out!!??!

  • LHS Cow speaking

    That's right.

  • Goat speaking

    That's a pretty strange name.

  • LHS Cow speaking

    It stands for “assembler output“, which is actually totally wrong because it's the linker output. Gah!

  • RHS Cow speaking

    It seemed like a good idea in 1972 when C was invented, and no one has dared change it!

  • Bjarne speaking

    It is part of our heritage.

You might wonder why we have two commands to compile and link. Technically, we don't need to; we could run

clang++ -o fourtwo fourtwo.cpp

But when we have multiple source files, it makes most sense to compile each one to make a .o file, and then link all the .o files together. And if we're going to do that with multiple source files, we might as well get into the habit now.

Running the Executable

Finally, we ran the executable with by writing

./fourtwo

(the ./ tells the system to look for the executable file in the current directory, rather than in the usual places that executable files are stored)

  • Duck speaking

    I ran ./fourtwo.cpp and it gave me a Permission denied error! Can someone give me the necessary permission?? Do I need superuser power?

  • LHS Cow speaking

    You don't run fourtwo.cpp, that's our source file—you need to run the executable we just made. No .cpp extension.

  • RHS Cow speaking

    Sometimes our fingers type things like that without us even realizing! If you see this error, that's probably what you did.

  • Hedgehog speaking

    That's a confusing error.

  • Bjarne speaking

    It is part of our heritage.

Note: these are the simplest compilation commands we could run. In general, we will typically add more command-line options to our compilation commands, as you'll see in Homework 01.

Summary of Compilation

The commands to compile a single-source-file program and run the executable file are:

clang++ -c myprogram.cpp
clang++ -o myprogram myprogram.o
./myprogram

And if we had multiple source files:

clang++ -c myprogram-part1.cpp
clang++ -c myprogram-part2.cpp
clang++ -o myprogram myprogram-part1.o myprogram-part2.o
./myprogram

Generating an Object File

Say we have a source file cowtime.cpp. Give the command to generate an object file cowtime.o.

Generating an Executable

Now give the command to generate an executable file cowtime.

Running cowtime

Now give the command to actually run cowtime.

(When logged in, completion status appears here.)