Page 114 - C-Language
P. 114
doesn't matter.
Note that on many systems, if you are using mathematical functions (from <math.h>), you need to
specify -lm to load the mathematics library — but Mac OS X and macOS Sierra do not require this.
There are other libraries that are separate libraries on Linux and other Unix systems, but not on
macOS — POSIX threads, and POSIX realtime, and networking libraries are examples.
Consequently, the linking process varies between platforms.
Other compilation options
This is all you need to know to begin compiling your own C programs. Generally, we also
recommend that you use the -Wall command-line option:
% gcc -Wall -c foo.cc
The -Wall option causes the compiler to warn you about legal but dubious code constructs, and
will help you catch a lot of bugs very early.
If you want the compiler to throw more warnings at you (including variables that are declared but
not used, forgetting to return a value etc.), you can use this set of options, as -Wall, despite the
name, doesn't turn all of the possible warnings on:
% gcc -Wall -Wextra -Wfloat-equal -Wundef -Wcast-align -Wwrite-strings -Wlogical-op \
> -Wmissing-declarations -Wredundant-decls -Wshadow …
Note that clang has an option -Weverything which really does turn on all warnings in clang.
File Types
Compiling C programs requires you to work with five kinds of files:
1. Source files: These files contain function definitions, and have names which end in .c by
convention. Note: .cc and .cpp are C++ files; not C files.
e.g., foo.c
2. Header files: These files contain function prototypes and various pre-processor statements
(see below). They are used to allow source code files to access externally-defined functions.
Header files end in .h by convention.
e.g., foo.h
3. Object files: These files are produced as the output of the compiler. They consist of function
definitions in binary form, but they are not executable by themselves. Object files end in .o
by convention, although on some operating systems (e.g. Windows, MS-DOS), they often
end in .obj.
e.g., foo.o foo.obj
4. Binary executables: These are produced as the output of a program called a "linker". The
linker links together a number of object files to produce a binary file which can be directly
https://riptutorial.com/ 90

