在Linux 下 新建 Test.c func.h func.c 源文件,内容分别为:
-----------func.h-----------
#include <stdio.h>
void ShowHello();
----------func.c-----------
#include "func.h"
void ShowHello()
{
printf("show hello func called \n");
}
-----------Test.c------------
#include <stdio.h>
#include "func.h"
int main()
{
printf("hello world\n");
ShowHello();
return 0;
}
编写makefile如下:
Test1: Test.o func.o
gcc -o Test1 Test.o func.o
Test.o: func.h Test.c
gcc -c Test.c
func.o: func.h func.c
gcc -c func.c
clean:
rm *.o
只需要一个make命令既可生成目标文件 具体makefile教程 参考
www.gddhx.com/post/32.html
第一次编译遇到两个问题
1. undefined reference to main 解决办法 gcc -o Test1 Test.o func.o 要写明要生成的目标文件名
2. makefile:2: *** missing separator. Stop. 解决办法 : gcc命令前使用tab而不是空格