网上关于gcc -I 的教程好少,试左成晚终于试左出来。
假如系目录 ~/testmake/下面有2个c文件同1个makefile: file1.c file2.c Makefile
系另个目录 ~/testmake/headfiles/ 有1个头文件: file2.h
其中file2.h内容为:
#ifndef __FILE2_H_
#define __FILE2_H_
void File2Print();
#endif
即系头文件同埋c文件唔系同1个目录, 有两种写法:
===============================================================================================
第一种写法:
//file1.c
#include <stdio.h>
#include "
./headfiles/
file2.h" //注明路径
int main(){
printf("print file1$$$$$$\n");
File2Print();
return 0;
}
//file2.c
#include <stdio.h>
#include "
./headfiles/
file2.h" //注明路径
void File2Print(){
printf("Print file2**********\n");
}
//Makefile
helloworld: file1.o file2.o
gcc file1.o file2.o -o helloworld
file1.o: file1.c ./headfiles/file2.h
gcc -c file1.c -o file1.o
file2.o: file2.c ./headfiles/file2.h
gcc -c file2.c -o file2.o
clean:
rm *.o
===============================================================================================
第二种写法:
//file1.c
#include <stdio.h>
#include "
file2.h" //<file2.h> 也可
int main(){
printf("print file1$$$$$$\n");
File2Print();
return 0;
}
//file2.c
#include <stdio.h>
#include
"
file2.h"
//
<file2.h> 也可
void File2Print(){
printf("Print file2**********\n");
}
//Makefile
helloworld: file1.o file2.o
gcc file1.o file2.o -o helloworld
file1.o: file1.c //不需要指明依赖的file2.h 前提是file2.h这个文件存在于./headfiles/ ,而不能是其他名字.
gcc -c file1.c -I ./headfiles/ -o file1.o //加上-I 选项
file2.o: file2.c
gcc -c file2.c -I ./headfiles/ -o file2.o
clean:
rm *.o