编写多个文件的程序,并使用Makefile文件进行编译。
正确答案:
(1)在/work目录先新建一个hello2目录,并在该目录下用vi命令创建文件hello1.c、hello2.c、hello.h和Makefile。分别输入如下内容。
文件hello1.c
//hello1.c
#include
intmain()
{
printf("WelcomeEmdoor!/n");
test2();
return1;
}
文件hello2.c
//hello2.c
#include"hello2.h"
#include
voidtest2(void)
{
printf("WelcomeEmdoor!–hello2/n");
}
文件hello2.h
//hello2.h
voidtest2(void);
文件makefile
#makefiletestformultifilesprogram
CC=gcc
OFLAGS=
OBJS=hello1.ohello2.o
all:hello
hello:$(OBJS)
$(CC)$(CFLAGS)$^-o$@
hello1.o:hello1.c
$(CC)$(CFLAGS)-c$<-o$@
hello2.o:hello2.c
$(CC)$(CFLAGS)-c$<-o$@
clean:
rm-rfhello*.o
(2) 先后执行如下命令
[root@local]$make
[root@local]$./hello
文件hello1.c
//hello1.c
#include
intmain()
{
printf("WelcomeEmdoor!/n");
test2();
return1;
}
文件hello2.c
//hello2.c
#include"hello2.h"
#include
voidtest2(void)
{
printf("WelcomeEmdoor!–hello2/n");
}
文件hello2.h
//hello2.h
voidtest2(void);
文件makefile
#makefiletestformultifilesprogram
CC=gcc
OFLAGS=
OBJS=hello1.ohello2.o
all:hello
hello:$(OBJS)
$(CC)$(CFLAGS)$^-o$@
hello1.o:hello1.c
$(CC)$(CFLAGS)-c$<-o$@
hello2.o:hello2.c
$(CC)$(CFLAGS)-c$<-o$@
clean:
rm-rfhello*.o
(2) 先后执行如下命令
[root@local]$make
[root@local]$./hello
答案解析:有

微信扫一扫手机做题