This section deals with the gcc
and clang compilers for C and C++,
since they come with the FreeBSD base system. Starting with
FreeBSD 10.X clang
is installed as
cc
. The
details of producing a program with an interpreter vary
considerably between interpreters, and are usually well covered
in the documentation and on-line help for the
interpreter.
Once you have written your masterpiece, the next step is to convert it into something that will (hopefully!) run on FreeBSD. This usually involves several steps, each of which is done by a separate program.
Pre-process your source code to remove comments and do other tricks like expanding macros in C.
Check the syntax of your code to see if you have obeyed the rules of the language. If you have not, it will complain!
Convert the source code into assembly language—this is very close to machine code, but still understandable by humans. Allegedly.
Convert the assembly language into machine code—yep, we are talking bits and bytes, ones and zeros here.
Check that you have used things like functions and global variables in a consistent way. For example, if you have called a non-existent function, it will complain.
If you are trying to produce an executable from several source code files, work out how to fit them all together.
Work out how to produce something that the system's run-time loader will be able to load into memory and run.
Finally, write the executable on the filesystem.
The word compiling is often used to refer to just steps 1 to 4—the others are referred to as linking. Sometimes step 1 is referred to as pre-processing and steps 3-4 as assembling.
Fortunately, almost all this detail is hidden from you, as
cc
is a front end that manages calling all these
programs with the right arguments for you; simply typing
%
cc foobar.c
will cause foobar.c
to be compiled by all the
steps above. If you have more than one file to compile, just do
something like
%
cc foo.c bar.c
Note that the syntax checking is just that—checking the syntax. It will not check for any logical mistakes you may have made, like putting the program into an infinite loop, or using a bubble sort when you meant to use a binary sort. [2]
There are lots and lots of options for cc
, which
are all in the manual page. Here are a few of the most important
ones, with examples of how to use them.
-o filename
The output name of the file. If you do not use this
option, cc
will produce an executable called
a.out
.
[3]
%
cc foobar.c
executable is a.out%
cc -o foobar foobar.c
executable is foobar
-c
Just compile the file, do not link it. Useful for toy
programs where you just want to check the syntax, or if
you are using a Makefile
.
%
cc -c foobar.c
This will produce an object file (not an
executable) called foobar.o
. This
can be linked together with other object files into an
executable.
-g
Create a debug version of the executable. This makes
the compiler put information into the executable about
which line of which source file corresponds to which
function call. A debugger can use this information to show
the source code as you step through the program, which is
very useful; the disadvantage is that
all this extra information makes the program much bigger.
Normally, you compile with -g
while you
are developing a program and then compile a “release
version” without -g
when you are
satisfied it works properly.
%
cc -g foobar.c
This will produce a debug version of the program. [4]
-O
Create an optimized version of the executable. The
compiler performs various clever tricks to try to produce
an executable that runs faster than normal. You can add a
number after the -O
to specify a higher
level of optimization, but this often exposes bugs in the
compiler's optimizer.
%
cc -O -o foobar foobar.c
This will produce an optimized version of
foobar
.
The following three flags will force cc
to check that your code complies to the relevant international
standard, often referred to as the ANSI
standard, though strictly speaking it is an
ISO standard.
-Wall
Enable all the warnings which the authors of
cc
believe are worthwhile. Despite the
name, it will not enable all the warnings
cc
is capable of.
-ansi
Turn off most, but not all, of the
non-ANSI C features provided by
cc
. Despite the name, it does not
guarantee strictly that your code will comply to the
standard.
-pedantic
Turn off all
cc
's non-ANSI C
features.
Without these flags, cc
will allow you to
use some of its non-standard extensions to the standard. Some
of these are very useful, but will not work with other
compilers—in fact, one of the main aims of the standard is
to allow people to write code that will work with any compiler
on any system. This is known as portable
code.
Generally, you should try to make your code as portable as possible, as otherwise you may have to completely rewrite the program later to get it to work somewhere else—and who knows what you may be using in a few years time?
%
cc -Wall -ansi -pedantic -o foobar foobar.c
This will produce an executable foobar
after checking foobar.c
for standard
compliance.
-llibrary
Specify a function library to be used at link time.
The most common example of this is when compiling a program that uses some of the mathematical functions in C. Unlike most other platforms, these are in a separate library from the standard C one and you have to tell the compiler to add it.
The rule is that if the library is called
lib
,
you give something
.acc
the argument
-l
.
For example, the math library is
something
libm.a
, so you give
cc
the argument -lm
.
A common “gotcha” with the math library is
that it has to be the last library on the command
line.
%
cc -o foobar foobar.c -lm
This will link the math library functions into
foobar
.
If you are compiling C++ code, use
c++
. c++
can also be
invoked as clang++
on FreeBSD.
%
c++ -o foobar foobar.cc
This will both produce an executable
foobar
from the C++ source file
foobar.cc
.
2.4.1.1. | I am trying to write a program which uses the
/var/tmp/cc0143941.o: Undefined symbol `_sin' referenced from text segment |
When using mathematical functions like
| |
2.4.1.2. | All right, I wrote this simple program to practice
using #include <stdio.h> int main() { float f; f = pow(2.1, 6); printf("2.1 ^ 6 = %f\n", f); return 0; } and I compiled it as:
like you said I should, but I get this when I run it:
This is not the right answer! What is going on? |
When the compiler sees you call a function, it checks if it has already seen a prototype for it. If it has not, it assumes the function returns an int, which is definitely not what you want here. | |
2.4.1.3. | So how do I fix this? |
The prototypes for the mathematical functions are in
#include <math.h> #include <stdio.h> int main() { ... After recompiling it as you did before, run it:
If you are using any of the mathematical functions,
always include
| |
2.4.1.4. | I compiled a file called
|
Remember,
| |
2.4.1.5. | OK, I have an executable called
|
Unlike MS-DOS®, UNIX® does not
look in the current directory when it is trying to find
out which executable you want it to run, unless you tell
it to. Type | |
2.4.1.6. | I called my executable |
Most UNIX® systems have a program called
or choose a better name for your program! | |
2.4.1.7. | I compiled my program and it seemed to run all right at first, then there was an error and it said something about core dumped. What does that mean? |
The name core dump dates back
to the very early days of UNIX®, when the machines used
core memory for storing data. Basically, if the program
failed under certain conditions, the system would write
the contents of core memory to disk in a file called
| |
2.4.1.8. | Fascinating stuff, but what I am supposed to do now? |
Use | |
2.4.1.9. | When my program dumped core, it said something about a segmentation fault. What is that? |
This basically means that your program tried to perform some sort of illegal operation on memory; UNIX® is designed to protect the operating system and other programs from rogue programs. Common causes for this are:
Making one of these mistakes will not always lead to an error, but they are always bad practice. Some systems and compilers are more tolerant than others, which is why programs that ran well on one system can crash when you try them on an another. | |
2.4.1.10. | Sometimes when I get a core dump it says bus error. It says in my UNIX® book that this means a hardware problem, but the computer still seems to be working. Is this true? |
No, fortunately not (unless of course you really do have a hardware problem…). This is usually another way of saying that you accessed memory in a way you should not have. | |
2.4.1.11. | This dumping core business sounds as though it could be quite useful, if I can make it happen when I want to. Can I do this, or do I have to wait until there is an error? |
Yes, just go to another console or xterm, do
to find out the process ID of your program, and do
where
This is useful if your program has got stuck in an infinite loop, for instance. If your program happens to trap SIGABRT, there are several other signals which have a similar effect. Alternatively, you can create a core dump from
inside your program, by calling the
If you want to create a core dump from outside your
program, but do not want the process to terminate, you
can use the |
[2] In case you did not know, a binary sort is an efficient way of sorting things into order and a bubble sort is not.
[3] The reasons for this are buried in the mists of history.
[4] Note, we did not use the -o
flag
to specify the executable name, so we will get an
executable called a.out
.
Producing a debug version called
foobar
is left as an exercise for
the reader!
All FreeBSD documents are available for download at http://ftp.FreeBSD.org/pub/FreeBSD/doc/
Questions that are not answered by the
documentation may be
sent to <[email protected]>.
Send questions about this document to <[email protected]>.