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
.
Three steps done with just one command. That's cool!
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.
Like saying “having a burger” might actually mean MORE, it might mean “having a burger, fries and a drink”.
Exactly. You have to tell from context whether someone means the middle step in the three step process, or all three steps.
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.
And why is the option
-c
?The first C compiler in the early 1970s used
-c
, so it is traditional.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
We could have picked another name,
-o prog42
would produce an executable calledprog42
.I just tried it without any
-o
option to specify a name, and it called the executablea.out
!!??!That's right.
That's a pretty strange name.
It stands for “assembler output“, which is actually totally wrong because it's the linker output. Gah!
It seemed like a good idea in 1972 when C was invented, and no one has dared change it!
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)
I ran
./fourtwo.cpp
and it gave me aPermission denied
error! Can someone give me the necessary permission?? Do I need superuser power?You don't run
fourtwo.cpp
, that's our source file—you need to run the executable we just made. No.cpp
extension.Sometimes our fingers type things like that without us even realizing! If you see this error, that's probably what you did.
That's a confusing error.
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
(When logged in, completion status appears here.)