2.4. Building software from Natively Compiled Code
This section shows how to build the cello.c
program written in the C language into an executable.
cello.c
#include <stdio.h> int main(void) { printf("Hello World\n"); return 0; }
2.4.1. Manual building
If you want to build the cello.c
program manually, use this procedure:
Procédure
Invoke the C compiler from the GNU Compiler Collection to compile the source code into binary:
gcc -g -o cello cello.c
Execute the resulting output binary
cello
:$ ./cello Hello World
2.4.2. Automated building
Large-scale software commonly uses automated building that is done by creating the Makefile
file and then running the GNU make
utility.
If you want to use the automated building to build the cello.c
program, use this procedure:
Procédure
To set up automated building, create the
Makefile
file with the following content in the same directory ascello.c
.Makefile
cello: gcc -g -o cello cello.c clean: rm cello
Note that the lines under
cello:
andclean:
must begin with a tab space.To build the software, run the
make
command:$ make make: 'cello' is up to date.
Since there is already a build available, run the
make clean
command, and after run themake
command again:$ make clean rm cello $ make gcc -g -o cello cello.c
NoteTrying to build the program after another build has no effect.
$ make make: 'cello' is up to date.
Execute the program:
$ ./cello Hello World
You have now compiled a program both manually and using a build tool.