func1.h
#ifndef _FUNC1_H_
#define _FUNC1_H_
void func1print();
#endif
func1.c
#include<stdio.h>
#include "func1.h"
void func1print(){
printf("This is func1 print!\n");
}
func2.h
#ifndef _FUNC2_H_
#define _FUNC2_H_
void func2print();
#endif
func2.c
#include<stdio.h>
#include "func1.h"
#include "func2.h"
void func2print(){
printf("In func2, first, call func1:");
func1print();
printf("This is func2 print!\n");
}
main.c
#include "func1.h"
#include "func2.h"
int main(){
func1print();
func2print();
return 0;
}
makefile
#Makefile
.PHONY:clean
myapp: main.o func1.o func2.o
gcc -o myapp main.o func1.o func2.o
main.o: main.c func1.h func2.h
gcc -c main.c
func1.o:func1.c func1.h
gcc -c func1.c
func2.o:func2.c func1.h func2.h
gcc -c func2.c
clean:
$(RM) *.o myapp
執行
>make
gcc -c main.c
gcc -c func1.c
gcc -c func2.c
gcc -o myapp main.o func1.o func2.o
驗證
>./myapp
This is func1 print!
In func2, first, call func1:This is func1 print!
This is func2 print!
解除make
>make clean
rm -f *.o myapp