CS1010 Compilation Guide
1. Compile a standalone C program
Suppose we have a standalone C program teh.c
that does not use any external libraries. We can compile the program using the command
1 |
|
This command should create an executable called a.out
in the current directory, which you can then run with:
1 |
|
If you are looking for a test teh.c
to try this out, there is one
at ~cs1010/teh.c
, which you can copy over.
2. Renaming executable file
The name a.out
is an abbreviation for assembler output, a name that many compilers kept as the default output name since the 60s. We should, however, give our executable more descriptive name, by using the -o
flag. (o
is the mnemonic for output).
1 |
|
or
1 |
|
The command above would create an executable called teh
.
Beware of the order
If you are not careful and run the following command instead:
1 |
|
clang
would overwrite your code teh.c
-- all your hard work will be gone!!
3. Warning for possible bugs.
The clang
checks for syntax errors in your C files -- i.e., things that violate the C syntax rules. The compiler, however, is smart enough to identify possible bugs -- errors that will cause your program to behave incorrectly, even if the syntax follows C's rules. You can ask clang
to warn you about this, using the -W
flag (W
is the mnemonic for warning -- note the capital W). The manual for clang
lists different types of warnings that clang
can warn you about. For instance, we can ask clang
to warn us by enabling -Wall
warnings. The command to do so is:
1 |
|
For beginners, it is highly recommended that you always compile with at least -Wall
, -Wextra
, and the -Wpedantic
flag.
clang warning flags
-Wall
in clang
does not catually enable all warnings.
-Weverything
enables every warning but it could be overwhelming
for beginners. In CS1010 assignments, we will provide a Makefile
so that you can use make
to automate the compilation process.
Appropriate warning flags will be enabled for you.
4. Generating additional information for debugging.
In order to use the debugger lldb
to trace through and debug your program, clang
needs to generate additional information and store them in the executable file. We can instruct clang
to generate them with the flag -g
(g
for generate).
1 |
|
It is recommended that you always compile with -g
flags during the development phase. If you need to measure the performance (e.g., how fast it runs) of your program or when you are releasing the program to the public, you can remove the -g
flag and compile with the optimization flags (e.g., -O
) instead.
5. Linking with the standard library.
To link with a standard library, we use the -l
flag to specify the name of the library to link. For instance, to link with the C standard math library (abbreviated as m
), you issue the command:
1 |
|
6. Linking with 3rd party library
By default, clang
looks for headers and libraries in the systems directories (/usr/include
, /usr/lib
, etc) and the current working directory.
If you use a third-party library, you usually need to tell clang
where to look for the corresponding headers and libraries. You can use the -I
flag and the -L
flag for these purposes. For instance, if you have a library installed under your home called citadel
, and the file citadel.h
can be found under ~/citadel/include
and the file libcitadel.a
can be found under ~/citadel/lib
, to tell clang
where to find these files, you can compile with:
1 |
|
For instance, to link with the CS1010 I/O library on the PE nodes, you can run
1 |
|