Smallest example of non separate compilation, the alone file m.c
containing :
int main() {}
compilation : gcc m.c
producing the executable a.out
In C we are free to put all in one file or in several files and also to compile in one or several steps, if this is your question
Example with 2 functions in the alone file m.c
:
#include <stdio.h>
void hw()
{
puts("hello world");
}
int main()
{
hw();
}
Compilation, we are also free to do in one or several steps
gcc m.c
producing the executable a.out
- or
gcc -c m.c
producing m.o
then gcc m.o
producing executable a.out
Same program using several files, hw.c
containing
#include <stdio.h>
#include "hw.h" /* not mandatory in current case but recommended to check signature compatibility */
void hw()
{
puts("hello world");
}
and hw.h
containing
#ifndef _HW_H
#define _Hw_H
extern void hw();
#endif
and m.c
containing
#include "hw.h"
int main()
{
hw();
}
Compilation, using one or several steps :
gcc m.c hw.c
producing executable a.out
- or
gcc -c m.c hw.c
producing objects m.o
and hw.o
, then gcc m.o hw.o
to produce executable a.out
- or
gcc -c m.c
producing objects m.o
then gcc -c hw.c
producing object hw.o
then gcc m.o hw.o
to produce executable a.out
It is also possible to create and use a library, here a static library containing only hw.o
:
gcc -c m.c hw.c
producing objects m.o
and hw.o
(or two gcc commands, one by object)
ar rcs hw.a hw.o
to make the static library containing hw.o
gcc m.o hw.a
to produce the executable a.out