Prev | Current Page 1407 | Next

Christopher Negus

"Linux Bible, 2008 Edition: Boot up to Ubuntu, Fedora, KNOPPIX, Debian, openSUSE, and 11 Other Distributions"

h - header for msg.c
*/
#ifndef MSG_H_
#define MSG_H_
void prmsg(char *msg);
#endif /* MSG_H_ */
783
Programming Tools and Utilities 29
LISTING 29-3
Definitions for newhello Helper Function
/*
* msg.c - function declared in msg.h
*/
#include
#include "msg.h"
void prmsg(char *msg)
{
printf("%s\n", msg);
}
The command to compile these programs to create newhello is
$ gcc msg.c main.c -o newhello
The gcc command finds the header file msg.h in the current directory and automatically includes
that file during the preprocessing stage. The stdio.h file resides in a location known to the gcc
command, so this file also gets included. You can add directories to search for such files, called
include files, with the -I command-line option.
To create the object files individually, you might use the following commands:
$ gcc -c msg.c
$ gcc -c main.c
Then, to create newhello from the object files, use the following command:
$ gcc msg.o main.o -o newhello
When you run this program, the output is:
$ ./newhello
Hi there, programmer!
Goodbye, programmer!
Before it creates the newhello binary, gcc creates object files for each source file. Typing long
commands such as this does become tedious, however. The section ???Automating Builds with
make??? later in this chapter shows you how to avoid having to type long, involved command lines.


Pages:
1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419