Makefile: Getting started

linux开发Makefile文件范本:

 

OBJ:= a.o b.o …

 

CC+= gcc

CFLAGS+= -g -Wall…

INCLUDE+= -I$MYHEADFILEPATH

libLIBS+=-lsctp   -lpthread   -lrt (在自己的库函数中使用)

SLIBS+= -L$(staticlibpath)   -lstatic  

DLIBS+= -L$(dynamiclibpath)   -ldynamic   -Wl,-rpath=$(dynamiclibpath)

 

target:$(OBJ)

   $(CC)  $(CFLAGS)   -o   target  $(OBJ)   $(SLIBS/DLIBS)

 

%.o:%.c

   $(CC)  $(CFLAGS)   $(INCLUDE)   -c  -o   $@   $<  

 

clean:

    rm -f *.o ...

-----------------------------------------------------------------------------------------------------------------

makefile的使用

 

make和makefile的作用:

1.     控制源代码的编译

2.     手册页的编写

3.     将应用程序安装到目标目录

 

.h文件为头文件用来放函数和变量的声明。

.c或者.cpp文件为源文件用来放函数或变量的定义。

源文件编译之后生成目标文件(.o文件)

将目标文件链接之后生成可执行文件

将众多中间目标文件(.o文件)打包叫库文件(.a文件.so文件)

 

编译:编译器只检查语法错误,函数和变量是否声明。

链接:连接器将函数和全局变量链接,检查函数和变量的定义。

 

makefile使用规则:

1.如果工程没有编译过,需要编译和链接所有c文件和目标文件。

2.如果工程里的某几个c文件被修改,只需要编译修改的c文件,并链接所有目标文件。

3.如果头文件被修改,只需要编译引用了被修改的头文件的c文件,并链接所有目标文件。

 

Make命令:

-k:让make命令在发现错误时继续执行

-n:输出将要执行的操作步骤,而不真正执行这些操作

-ffilename:指定makefile文件

make

makeall

makeinstall

makeclean

 

Makefile文件查找顺序:

当前目录找makefile

当前目录查找Makefile(推荐用这个)

当前目录找GNUmakefile

 

--------------------------------------------------

makefile的格式:

 

target:(空格或制表符) prerequisites . . .

(制表符)command

(制表符)...

(制表符)...

#note

 

target:可以是.o文件、可执行文件、标签(lable),多个文件用空格分开。 target=all表示默认生成all需要的依赖。

prerequisites:生成target所需要的文件或目标(目标文件或头文件等),多个文件用空格或制表符分开。

command:制表符tab开头,利用prerequisites生成target的shell命令(make需要执行的命令)

#  用来注释一行

 

可以在makefile文件的结尾定义一些标签,比如打包,备份等:

clean:

       rm   elf   a.o   b.o  c.o   d.o

在执行make clean命令后就会删除所有的目标文件。

 

可以使用续行符\

 

make命令:执行makefile文件,会比较target和prerequisites的时间戳,如果prerequisites比target新,或者target不存在就会执行command,否则会跳过command。

 

--------------------------------------------------

makefile中使用变量

 

如果prerequisites包含的文件很多或者会重复用到,那么将这些文件当作一个字符串,给他取个别名:

定义:

obj  = a.o   b.o   c.o  d.o

使用:

elf: $(obj)

       gcc  -o    target   $(obj)

clean:

       rm  elf   $(obj)

 

变量相当于宏:

定义:

name=value(不能有空格)

引用:

$(name)

${name}

$name

 

定义编译器

CC=$(CROSS_COMPILE)gcc

CC=gcc

$(CC) 

 

编译器参数                                               

CFLAGS=-g –Wall –ansi -pedantic

$(CFLAGS)

 

C语言预处理器参数                                   

$(CPPFLAGS)  

 

目标机器的结构定义                                

$(TARGET_ARCH)                           

 

指定头文件搜索路径

INCLUDE=.

$(INCLUDE)  

 

指定安装路径

INSTDIR=/usr/local/bin

$(INSTDIR)

                                    

自动化变量

$<             第一个依赖文件

$^             所有的依赖文件

$@             目标文件

 

$*             不包含扩展名的目标文件名称

$+             所有的依赖文件,用空格分开

$?             所有时间戳比目标文件晚的依赖文件,以空格分开

$%             如果目标是归档成员,则表示目标的归档或成员

 

--------------------------------------------------

让make自动推导

 

make的自动推导功能:只要make看到.o文件,就会自动关联.c文件,同时command中的gcc也会自动关联:

a.o   :  a.c   a.h

       gcc  -c   a.c   a.h

等价于:

a.o   :  a.h

 

对于简单的.c文件:

make  filename.c  

make能自动编译生成filename可执行文件:cc filename.c –o filename

在linux系统cc是gcc的别名

 

make   -p    可以打印出gun  make的内置规则

 

--------------------------------------------------

共用的头文件可以简化

 

a.o   :  a.h   b.h   c.h

b.o   :  b.h   c.h   d.h

c.0   :  c.h   d.h

d.o: d.h

等价于:

$(obj)   :  d.h

a.o   b.o  c.o   :   c.h

a.o   b.o  :   b.h

a.o   :  a.h 

 

--------------------------------------------------

清空目标文件的规则

 

makefile文件中加清空文件便于重新编译:

clean:

       -rm  -f  elf   $(obj)

更好的写法是:

.PHONY   :  clean

clean:

       -rm  elf   $(obj)

用.PHONY来表示clean是一个伪目标。

在rm前面加-表示忽略错误,继续向后执行。

 

--------------------------------------------------

一个makefile组成

all:elf

obj= a.o b.o c.o d.o

CC= gcc

elf: $(obj)

       ${CC}    -o      elf      $(obj)

$(obj)   :  d.h

a.o   b.o  c.o   :   c.h

a.o   b.o  :   b.h

a.o   :  a.h 

 

.PHONY   :  clean

clean:

       -rm  elf   $(obj)

 

install:myapp

       @if [ -d $(INSTDIR) ];\

      then\

              cp myapp $(INSTDIR);\

              chmod a+x $(INSTDIR)/myapp;\

              chmod og-w $(INSTDIR)/myapp;\

       else\

              echo “sorry, $(INSTDIR) does notexist”;\

       fi\

 

Makefile中有哪些内容:

显式规则;

隐晦规则;

变量定义;

文件指示:makefile中的引用和根据条件指定有效部分。

注释:使用#进行行注释。

 

在commands前面加@就会取消在终端显示执行编译等命令的信息。

@command   不要将操作显示在stdout

-command   忽略错误

--------------------------------------------------

Make的工作顺序:

1.      读入所有makefilea

2.      读入include的其他makefile

3.      初始化变量

4.      推到隐晦规则,分析所有规则

5.      为所有目标文件创建以来关系链

6.      依据依赖关系,决定哪些目标需要重新生成

7.      执行生成的命令


未完待续......

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Getting Started with Angular - Second Edition by Minko Gechev English | 6 Apr. 2017 | ISBN: 1787125270 | 308 Pages | EPUB/PDF (conv) | 7.7 MB Key Features Get up to date with the final changes to Angular 2, including the improvements to directives, change detection, dependency injection, router, and more Understand Angular 2's new component-based architecture Start using TypeScript to supercharge your Angular 2 applications Book Description Angular is the modern framework you need to build performant and robust web applications. This book is the quickest way to get to grips with Angular 2 and will help you transition to the brave new world of Angular 2. It starts with an overview putting the changes of the framework in context with version 1.x. After that, you will be taken on a TypeScript crash-course so you can take advantage of Angular 2 in its native, statically-typed environment. We'll look at the new change-detection method in detail, how Directives and Components have changed how you create websites with Angular, the new Angular 2 router, and much more. By the end of the book, you'll be ready to start building quick and efficient Angular 2 applications that take advantage of all the new features on offer. What you will learn Understand the changes made from Angular 1.x with side-by-side code samples to help demystify the Angular 2 learning curve Start working with Angular 2's new method of implementing directives Use TypeScript to write modern, powerful Angular 2 applications Dig in to the change-detection method, and other architectural changes to make sure you know what's going on under the hood of Angular 2 Get to work with the new router in Angular 2 Use the new features of Angular 2 including pipes, or the updated features such as forms, services, and the DI module Master server-side rendering in Angular 2 to keep your new applications SEO-friendly

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值