1.makefile初体验
Makfile : 管理工程的编译
基本组成
目标:依赖
命令
含义:生成目标,依赖那些文件,命令则为生成目标的方式
make 调用 Makfile 进行编译,它找的是Makefile第一个目标
目标是否需要重新生成,看依赖是否比目标 “新”
目标:
命令
注意:此时 make 目标,调用Makefile文件,找到Makefile文件中目标,无条件执行命令
c source:
#include <stdio.h>
int main(int argc, const char *argv[])
{
printf("hello word1");
return 0;
}
makefile:
test:test.c
gcc test.c -o test
clean:
rm -rf test
maxie@seil:~/workspace/study/Makefile/makefile1$ make
gcc test.c -o test
maxie@seil:~/workspace/study/Makefile/makefile1$ ./test
hello word1maxie@seil:~/workspace/study/Makefile/makefile1$
makefile:
test:test.o
gcc test.o -o test
test.i:test.c
gcc -E test.c -o test.i
test.s:test.i
gcc -S test.i -o test.s
test.o:test.s
gcc -c test.s -o test.o
clean:
rm -rf test *.o *.s *.i
runtime:
maxie@seil:~/workspace/study/Makefile/makefile3$ make
gcc -E test.c -o test.i
gcc -S test.i -o test.s
gcc -c test.s -o test.o
gcc test.o -o test
maxie@seil:~/workspace/study/Makefile/makefile3$ lsls
lsls: command not found
maxie@seil:~/workspace/study/Makefile/makefile3$ ls
core Makefile note.txt test test.c test.i test.o test.s
maxie@seil:~/workspace/study/Makefile/makefile3$ ./test
hello word1maxie@seil:~/workspace/study/Makefile/makefile3$
2.Makefile 变量
(1)用户自定义的变量
VAR= value
引用变量的方法:$(VAR)
变量的赋值:
VAR = “hello”
//不会立即展开 $(VAR)
,在引用VAR1的时候展开$(VAR)
VAR1 = $(VAR)
//立即展开$(VAR)
VAR1 := $(VAR)
//如果VAR1没有赋值,则赋值,没有,则不会赋值
VAR1 ?= $(VAR)
makefile:
VAR = "hello"
VAR1 = $(VAR)
VAR2 := $(VAR)
#此时VAR 不会被赋值
#VAR ?= "word"
VAR = "word"
test:
echo "VAR1 = $(VAR1)"
echo "VAR2 = $(VAR2)"
c source:
#include <stdio.h>
int main(int argc, const char *argv[])
{
printf("hello word1");
return 0;
}
runtime:
maxie@seil:~/workspace/study/Makefile/makefile2$ make
echo "VAR1 = "word" "
VAR1 = word
echo "VAR2 = "hello""
VAR2 = hello
maxie@seil:~/workspace/study/Makefile/makefile2$ ls
Makefile note.txt test.c
maxie@seil:~/workspace/study/Makefile/makefile2$
3.特殊变量
(2)特殊变量
$@ : 完整的目标名称
$< : 第一个依赖文件名称
$^ : 所有的依赖文件名称
1.
main.c
#include <stdio.h>
extern int add(int a,int b);
extern void test_function();
int main(int argc, const char *argv[])
{
int a = 10;
int b = 20;
test_function();
printf("a + b = %d.\n",add(a,b));
return 0;
}
test.c
#include <stdio.h>
void test_function()
{
printf("test_function !.\n");
return;
}
add.c
#include <stdio.h>
int add(int a,int b)
{
return a + b;
}
makefile:
OBJS=main.o test.o add.o
CC=gcc
main:$(OBJS)
$(CC) $^ -o $@
main.o:main.c
$(CC) -c $< -o $@
test.o:test.c
$(CC) -c $< -o $@
add.o:add.c
$(CC) -c $< -o $@
clean:
rm -rf *.o main
runtime:
maxie@seil:~/workspace/study/Makefile/makefile4$ make
gcc -c main.c -o main.o
gcc -c test.c -o test.o
gcc -c add.c -o add.o
gcc main.o test.o add.o -o main
maxie@seil:~/workspace/study/Makefile/makefile4$ ./main
test_function !.
a + b = 30.
2.
main.c
#include <stdio.h>
extern int add(int a,int b);
extern void test_function();
int main(int argc, const char *argv[])
{
int a = 10;
int b = 20;
test_function();
printf("a + b = %d.\n",add(a,b));
printf("a - b = %d.\n",sub(a,b));
return 0;
}
test.c
#include <stdio.h>
void test_function()
{
printf("test_function !.\n");
return;
}
add.c
#include <stdio.h>
int add(int a,int b)
{
return a + b;
}
sub.c
int sub(int a,int b)
{
return a - b;
}
makefile:
OBJS =main.o test.o add.o
OBJS += sub.o
CC=gcc
main:$(OBJS)
$(CC) $^ -o $@
clean:
rm -rf *.o main
runtime:
maxie@seil:~/workspace/study/Makefile/makefile5$ make
gcc -c -o main.o main.c
gcc -c -o test.o test.c
gcc -c -o add.o add.c
gcc -c -o sub.o sub.c
gcc main.o test.o add.o sub.o -o main
maxie@seil:~/workspace/study/Makefile/makefile5$ vi Makefile
maxie@seil:~/workspace/study/Makefile/makefile5$ ls
add.c add.o main main.c main.o Makefile note.txt sub.c sub.o test.c test.o
maxie@seil:~/workspace/study/Makefile/makefile5$ ./main
test_function !.
a + b = 30.
a - b = -10.
maxie@seil:~/workspace/study/Makefile/makefile5$
3.
main.c
#include <stdio.h>
extern int add(int a,int b);
extern void test_function();
int main(int argc, const char *argv[])
{
int a = 10;
int b = 20;
test_function();
printf("a + b = %d.\n",add(a,b));
printf("a - b = %d.\n",sub(a,b));
return 0;
}
test.c
#include <stdio.h>
void test_function()
{
printf("test_function !.\n");
return;
}
add.c
#include <stdio.h>
int add(int a,int b)
{
return a + b;
}
sub.c
int sub(int a,int b)
{
return a - b;
}
makefile:
OBJS =main.o test.o add.o
OBJS += sub.o
CC=gcc
CFLAGS=-g -c
main:$(OBJS)
$(CC) $^ -o $@
指定变量规则
%.o:%.c
$(CC) $(CFLAGS) $< -o $@
clean:
rm -rf *.o main
runtime:
maxie@seil:~/workspace/study/Makefile/makefile6$ make
gcc -g -c main.c -o main.o
gcc -g -c test.c -o test.o
gcc -g -c add.c -o add.o
gcc -g -c sub.c -o sub.o
gcc main.o test.o add.o sub.o -o main
maxie@seil:~/workspace/study/Makefile/makefile6$ ls
add.c add.o main main.c main.o Makefile note.txt sub.c sub.o test.c test.o
maxie@seil:~/workspace/study/Makefile/makefile6$ ./main
test_function !.
a + b = 30.
a - b = -10.
maxie@seil:~/workspace/study/Makefile/makefile6$
4.
makefile7目录:
main.c:
#include <stdio.h>
extern int add(int a,int b);
extern void test_function();
int main(int argc, const char *argv[])
{
int a = 10;
int b = 20;
test_function();
printf("a + b = %d.\n",add(a,b));
printf("a - b = %d.\n",sub(a,b));
return 0;
}
makefile:
OBJS =main.o
CC=gcc
CFLAGS=-g -c
main:$(OBJS)
#调用其他的Makefile生成add.o sub.o test.o
make -C ./calc all #到指定的目录下取找Makefile
make -C ./test all
$(CC) *.o -o $@
#指定变量规则
%.o:%.c
$(CC) $(CFLAGS) $< -o $@
clean:
rm -rf *.o main
#不仅仅删除当前.o文件,还把子目录下的.o删除
distclean:
make -C ./calc all #到指定的目录下取找Makefile
make -C ./test all
rm -rf *.o main
makefile7/calc:
add.c
#include <stdio.h>
int add(int a,int b)
{
return a + b;
}
sub.c
int sub(int a,int b)
{
return a - b;
}
makefile:
OBJS +=add.o
OBJS +=sub.o
CC=gcc
CFLAGS=-g -c
all:$(OBJS)
cp $(OBJS) ../
rm *.o
#指定编译规则
%.o:%.c
$(CC) $(CFLAGS) $< -o $@
makefile7/test目录:
test.c
#include <stdio.h>
void test_function()
{
printf("test_function !.\n");
return;
}
makefile:
OBJS +=test.o
CC=gcc
CFLAGS=-g -c
all:$(OBJS)
cp $(OBJS) ../
rm *.o
#指定编译规则
%.o:%.c
$(CC) $(CFLAGS) $< -o $@
runtime:
maxie@seil:~/workspace/study/Makefile/makefile7$ make
gcc -g -c main.c -o main.o
#调用其他的Makefile生成add.o sub.o test.o
make -C ./calc all #到指定的目录下取找Makefile
make[1]: Entering directory `/home/maxie/workspace/study/Makefile/makefile7/calc'
gcc -g -c add.c -o add.o
gcc -g -c sub.c -o sub.o
cp add.o sub.o ../
rm *.o
make[1]: Leaving directory `/home/maxie/workspace/study/Makefile/makefile7/calc'
make -C ./test all
make[1]: Entering directory `/home/maxie/workspace/study/Makefile/makefile7/test'
gcc -g -c test.c -o test.o
cp test.o ../
rm *.o
make[1]: Leaving directory `/home/maxie/workspace/study/Makefile/makefile7/test'
gcc *.o -o main
maxie@seil:~/workspace/study/Makefile/makefile7$ make
make: `main' is up to date.
maxie@seil:~/workspace/study/Makefile/makefile7$ ./main
test_function !.
a + b = 30.
a - b = -10.
maxie@seil:~/workspace/study/Makefile/makefile7$