An Introduction To GCC - Compiling Multiple Source Files
An Introduction To GCC - Compiling Multiple Source Files
voidhello(constchar*name);
Thedefinitionofthefunctionhelloitselfiscontainedinthefile'hello_fn.c':
#include<stdio.h>
#include"hello.h"
void
hello(constchar*name)
{
printf("Hello,%s!\n",name);
}
Thisfunctionprintsthemessage"Hello,name!"usingitsargumentasthevalueofname.
Incidentally,thedifferencebetweenthetwoformsoftheincludestatement#include
"FILE.h"and#include<FILE.h>isthattheformersearchesfor'FILE.h'inthecurrent
directorybeforelookinginthesystemheaderfiledirectories.Theincludestatement#include
<FILE.h>searchesthesystemheaderfiles,butdoesnotlookinthecurrentdirectoryby
default.
Tocompilethesesourcefileswithgcc,usethefollowingcommand:
$gccWallmain.chello_fn.conewhello
Inthiscase,weusetheooptiontospecifyadifferentoutputfilefortheexecutable,
'newhello'.Notethattheheaderfile'hello.h'isnotspecifiedinthelistoffilesonthe
commandline.Thedirective#include"hello.h"inthesourcefilesinstructsthecompilerto
includeitautomaticallyattheappropriatepoints.
Toruntheprogram,typethepathnameoftheexecutable:
$./newhello
Hello,world!
Allthepartsoftheprogramhavebeencombinedintoasingleexecutablefile,which
producesthesameresultastheexecutablecreatedfromthesinglesourcefileusedearlier.
<<<previous tableofcontents next>>>
PublishedunderthetermsoftheGNUGeneralPublicLicense DesignbyInterspire