01.09.2014 Views

The Linux Development Platform Configuring, Using, and ... - Classes

The Linux Development Platform Configuring, Using, and ... - Classes

The Linux Development Platform Configuring, Using, and ... - Classes

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CH03.fm Page 59 Monday, October 7, 2002 8:33 PM<br />

Compiling a Program 59<br />

is used. <strong>The</strong>se Makefiles contain information about how the compiler will be invoked <strong>and</strong> what<br />

comm<strong>and</strong> line switches will be used. Information about GNU make <strong>and</strong> Makefiles is presented<br />

in Chapter 5.<br />

3.3.1 Simple Compilation<br />

Consider the following C source code file, which is named hello.c. We shall frequently<br />

refer to this program in this as well as coming chapters.<br />

#include <br />

main ()<br />

{<br />

printf("Hello world\n");<br />

}<br />

To compile this file, you just need to run the following comm<strong>and</strong>.<br />

[rr@conformix 4]$ gcc hello.c<br />

[rr@conformix 4]$<br />

By default, this comm<strong>and</strong> will generate an output file named a.out, which can be executed<br />

on the comm<strong>and</strong> line as follows:<br />

[rr@conformix 4]$ ./a.out<br />

Hello world<br />

[rr@conformix 4]$<br />

Note that regardless of the name of your source code file, the name of the output file is<br />

always a.out. You may actually know what an ordinary a.out file is, but this isn't one of<br />

them. It is an elf file, despite its name. If you want to create an output file with a different<br />

name, you have to use the –o comm<strong>and</strong> line option. <strong>The</strong> following comm<strong>and</strong> creates an output<br />

file with name hello.<br />

gcc hello.c -o hello<br />

As you may have noted, the above comm<strong>and</strong>s do both the compiling <strong>and</strong> linking processes<br />

in a single step. If you don’t want to link the program, then simple compilation of hello.c file<br />

can be done with the following comm<strong>and</strong>. <strong>The</strong> output file will be hello.o <strong>and</strong> will contain<br />

object code.<br />

gcc –c hello.c<br />

Note that both –c <strong>and</strong> –o comm<strong>and</strong> line switches can be used simultaneously. <strong>The</strong> following<br />

comm<strong>and</strong> compiles hello.c <strong>and</strong> produces an output file test.o which is not yet linked.<br />

gcc –c hello.c -o test.o<br />

Usually when you compile many files in a project, you don’t create a.out files. Either<br />

you compile many files into object files <strong>and</strong> then link them together into an application or make<br />

executables with the same name as the source code file.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!