GNU/Linux 编程指南之二:make

这篇博客详细讲解了GNU/Linux环境下使用make进行编程时的关键概念,包括环境变量、自动变量、预定义变量及其默认值,如$@、$<、$^等。还介绍了隐含规则和模式规则的概念,以及如何自定义规则。此外,文章涵盖了makefile中的注释、命令行选项,常见的错误消息以及有用的make目标,如install、uninstall和test。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • Environment, Automatic, and Predefined Variables

Automatic Variables

***************************************

$@     The filename of a rule's target

$<      The name of the first dependency in a rule

$^      Space-delimited list of all the dependencies in a rule

$?       Space-delimited list of all the dependencies in a rule that are newer than the target

$(@D) The directory part of a target filename, if the target is  in a subdirectory

$(@F) The filename part of a target filename, if the target is in a subdirectory

****************************************

Predefined Varialbes For Program Names and Flags

************************************************

AR                     Archive-maintenance programs; default value = ar

AS                     Program to do assembly; default value = as

CC                    Program for compliling C programs; default value = cc

CPP                  C Preprocessor program; default value = cpp

RM                    Program to remove files; default value = "rm -f"

ARFLAGS           Flags for the archive-maintenance program; default = rv

ASFLAGS           Flags for the assembler program; no default

CFLAGS            Flags for the C compiler; no default

LDFLAGS           Flags for the linker(ld); no default

****************************************************

If you want, you can redefine these variables in the makefile. In most cases, their default values are reasonable.

 

 

  •  Implicit Rules

Feature : special purpose and limited usage

如果makefile中没有相应的.o文件的生成命令,make 会自动寻找.c然后生成相应的.o文件

 

  • Pattern Rules

除了make自己定义的一些隐含规则外,用户也可以自己定义规则,成为pattern rules.  The target contains exactly one character(%)that matches any nonempty string. The dependencies of such a rule also use % in order to mach the target.

eg.

****************************************

%.o : %.c

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

****************************************

It defines a rule that makes any file x.o from x.c.

 

 

  • Comments

#This file is used to ...

 

 

  • Additional make Command-line option

********************************************************

-f file                                Specify an alternatively-named makefile file

-Idirname                        Specify dirname as a directory in which make search for included makefiles.

 

-k                                     If one target fails to build, continue to build other targets. Normally, make terminates if a target fails to build successfully.

********************************************************

 

  • Common make Error Messages

************************************************************

1   No rule to make target 'target'. Stop

2   'target' is up to date

3   Target 'target' not remade because of errors

4   command: Command not found

5   Illegal option -option

************************************************************

 

 

  • Usefule Makefile Targets

***************************************************

install                      uninstall                           dist

test                         check

installtest                   installcheck

***************************************************

 

 

 

总结:这次笔记介绍了make的作用,规则, 变量的定义, 命令行参数等等。一些更详细的说明等用到的时候可以查询使用。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值