如何写makefile?
TherearetwoCsourcefilesinyourdirectory.compute.cincludethemainfunctiontogettheuserinp...
There are two C source files in your directory. compute.c include the main function to get the user input, and compute the square of the user input. mymath.c include square function. The dependency tree is as following:
compute
|
-------------
| |
compute.o mymath.o
| |
compute.c mymath.c
Write a makefile to create the executable file compute.
=====================================================
compute.c
#include "mymath.h"
#include <stdio.h>
int main() { int num;
printf("input a number: ");
scanf("%d", &num);
printf("the power of %d is %d\n", num, square(num));
return 0; }
=====================================================
mymath.c
int square(int num)
{ return num * num; } 展开
compute
|
-------------
| |
compute.o mymath.o
| |
compute.c mymath.c
Write a makefile to create the executable file compute.
=====================================================
compute.c
#include "mymath.h"
#include <stdio.h>
int main() { int num;
printf("input a number: ");
scanf("%d", &num);
printf("the power of %d is %d\n", num, square(num));
return 0; }
=====================================================
mymath.c
int square(int num)
{ return num * num; } 展开
展开全部
CC=gcc
TARGET=compute
DEPEND=compute.o mymath.o
$(TARGET):$(DEPEND)
(这前面是一个TAB键,不要有空格)$(CC) -o $@ $^ -g -Wall
.c.o:
(这前面是一个TAB键,不要有空格) $(CC) -c $^ -g
.PHONY:clean
clean:
(这前面是一个TAB键,不要有空格) rm $(DEPEND)
#下面为说明
#Makefile的格式为Target(目标):depend(依赖)
# (TAB)command(生成目标的命令)
#其中:%@ 为目标,$^ 为所有的依赖文件,$< 为第一个依赖文件
#.c.o:此格式表名所有的.o都依赖于同名的.c
#.PHONY:clean
#clean:
# rm$(DEPEND)
#.PHONY:clean 表明clean为虚目标
//compute.c
#include "compute.h"
int main(int argc, char** argv)
{
int num;
printf("input a number: ");
scanf("%d", &num);
printf("the power of %d is %d\n", num, square(num));
return 0;
}
=====================================================
//compute.h
#ifndef __COMPUTE_H__
#define __COMPUTE_H__
#include "mymath.h"
#endif
=====================================================
//mymath.c
#include "mymath.h"
int square(int num)
{
return num * num;
}
============================================
//mymath.h
#ifndef __MYMATH_H__
#define __MYMATH_H__
int square(int num);
#endif
//这样写就规范了
=====================================
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询