尧图网站建设 尧图网络
  • 首页
  • 关于我们
  • 服务项目
  • 案例展示
  • 建站流程
  • 资讯中心
  • 联系我们
首页/资讯中心/详情

[Mandatory] - Makefile中的变量名大小写策略 / Variables - 推荐结论:小写-内部变量用, 大写:显示规则,参数会被override变量 - ENGINEER

[Mandatory] - Makefile中的变量名大小写策略 / Variables - 推荐结论:小写-内部变量用, 大写:显示规则,参数会被override变量 - ENGINEER
📅 发布时间:2026/6/24 21:22:38

[Mandatory] - Makefile中的变量名大小写策略 / Variables - 推荐结论:小写-内部变量用, 大写:显示规则,参数会被override变量

A variable is a name defined in a makefile to represent a string of text, called the variable’s
value. These values are substituted by explicit request into targets, prerequisites, recipes,
and other parts of the makefile. (In some other versions of make, variables are called macros.)

Variables and functions in all parts of a makefile are expanded when read, except for
in recipes, the right-hand sides of variable definitions using ‘=’, and the bodies of variable
definitions using the define directive. The value a variable expands to is that of its most
recent definition at the time of expansion. In other words, variables are dynamically scoped.
Variables can represent lists of file names, options to pass to compilers, programs to run,
directories to look in for source files, directories to write output in, or anything else you can
imagine.
A variable name may be any sequence of characters not containing ‘:’, ‘#’, ‘=’, or whitespace.
However, variable names containing characters other than letters, numbers, and
underscores should be considered carefully, as in some shells they cannot be passed through
the environment to a sub-make (see Section 5.7.2 [Communicating Variables to a Sub-make],
page 57). Variable names beginning with ‘.’ and an uppercase letter may be given special
meaning in future versions of make.
Variable names are case-sensitive. The names ‘foo’, ‘FOO’, and ‘Foo’ all refer to different
variables.
It is traditional to use upper case letters in variable names, but we recommend using lower
case letters for variable names that serve internal purposes in the makefile, and reserving
upper case for parameters that control implicit rules or for parameters that the user should
override with command options (see Section 9.5 [Overriding Variables], page 113).
A few variables have names that are a single punctuation character or just a few characters.
These are the automatic variables, and they have particular specialized uses. See
Section 10.5.3 [Automatic Variables], page 130.

 

 

在 Makefile 中,变量名和规则名称是严格区分大小写的,这是其核心语法规则之一。以下是关于 Makefile 大小写使用的详细说明和最佳实践:


1. 变量名的大小写敏感性

  • 区分大小写:FOO、Foo和 foo是三个不同的变量,需严格区分。

    makefile
    CC = gcc
    Cc = clang  # 与 CC 无关
    all:$(CC) main.c  # 使用 gcc$(Cc) main.c  # 使用 clang
  • 传统习惯:传统 Makefile 多用全大写(如 CFLAGS),但推荐 混合大小写(如 MakeFlags)以避免与系统变量冲突 


2. 规则名称的大小写

  • 目标与依赖:规则中的目标(Target)和依赖(Dependency)名称区分大小写。

    makefile
    复制
    Program: main.o utils.o  # 正确
    program: main.o utils.o  # 错误,与目标名不匹配
  • 文件匹配:若文件系统区分大小写(如 Linux),main.c和 Main.c是不同文件。


3. 自动化变量的大小写

  • 预定义变量:如 $@(目标)、$<(第一个依赖)、$^(所有依赖)等,必须小写。

    makefile
    all: $(TARGET)$(CC) $^ -o $@  # 正确使用小写自动化变量

4. 常见错误与规避方法

错误示例

makefile
 
# 错误:变量名大小写不一致
CC = gcc
cc = clang
all:$(cc) main.c  # 实际使用 clang,而非预期 gcc

规避方法

  • 统一命名规范:全局变量用全大写,局部变量用混合大小写。

    makefile
    # 全局变量(全大写)
    CC = gcc
    CFLAGS = -Wall# 局部变量(混合大小写)
    lib_src = lib/utils.c
  • 避免覆盖系统变量:如 PATH、MAKE等,使用自定义前缀(如 MY_PROJECT_CFLAGS)


5. 最佳实践

  1. 变量命名规范:

    • 全局变量:全大写(如 PROJECT_ROOT)。

    • 局部变量:混合大小写(如 src_files)。

  2. 引用变量时加括号:

    makefile
    # 推荐
    $(CC) $(CFLAGS) main.c
    # 避免
    $CC $(CFLAGS) main.c  # 错误,$C 会被解释为变量
  3. 使用 ?=条件赋值:

    makefile
    CFLAGS ?= -O2  # 仅在未定义时设置默认值

6. 示例:混合大小写的变量使用

makefile
复制
 
 
# 定义混合大小写变量
SRC_DIR = src
OBJ_DIR = obj# 使用变量(严格匹配大小写)
$(OBJ_DIR)/main.o: $(SRC_DIR)/main.c$(CC) -c $< -o $@# 错误示例:变量名大小写不匹配
$(obj_dir)/main.o: $(src_dir)/main.c  # 无法找到目标文件

总结

  • 核心原则:Makefile 中所有标识符(变量、规则名)均区分大小写。

  • 推荐实践:通过统一命名规范(如全局全大写、局部混合大小写)和合理使用括号,减少错误。

  • 工具辅助:使用 make -n预检查变量展开,或通过静态分析工具(如 checkmake)验证语法

Time is like a fleeting show!

相关新闻

  • 2025年停车场洗地机实力厂家权威榜单:全自动洗地机/自走式洗地机/物业保洁洗地机源头厂家精选 - 品牌推荐官
  • Windows Server 2019 OVF (2025 年 12 月更新) - VMware 虚拟机模板
  • 2025水质分析仪推荐厂家排行榜权威排名,到底哪个水质分析仪品牌生产供应商受欢迎? - 品牌推荐大师1

最新新闻

  • 用ChatGPT做英语沉浸式训练:从pocket到语义网络的AI精练法
  • AI Agent服务化实战:从对话接口到商业分发平台
  • Pikachu靶场实战指南:从SQL注入到XSS的Web渗透入门
  • OpenClaw CN Windows原生部署全指南:从安装到服务化
  • 华为eNSP防火墙Web界面配置实战:从零搭建管理环境
  • HEIC转JPG实战指南:解码稳定性、色彩还原与隐私安全全解析

日新闻

  • 终极指南:如何用shadPS4在电脑上免费畅玩PS4游戏
  • 打造个性化Instagram Clone:主题定制与用户体验优化技巧
  • 未来展望:RoseTTAFold-All-Atom的发展路线图与社区支持资源汇总

周新闻

  • Visual C++运行库修复终极指南:5分钟快速解决Windows软件启动错误
  • 手把手教你构建统计局地区经济数据爬虫:从环境搭建到数据持久化全指南
  • 2026多Agent深度解析:用AI团队替代单一模型,四种架构实战落地

月新闻

  • 【总结】入门篇:50句话让你记住架构核心概念
  • WeChatMsg技术方案解析:实现Mac微信数据自主管理的完整解决方案
  • WeChatMsg:革新性微信数据备份方案,打造你的专属数字记忆库

关于尧图

  • 公司简介
  • 团队介绍
  • 企业文化
  • 荣誉资质

服务项目

  • 定制开发
  • 电商建站
  • UI 设计
  • 运维服务

快速链接

  • 案例展示
  • 建站流程
  • 常见问题
  • 资讯中心

联系方式

  • 📍北京市朝阳区互联网产业园 A 座 10 层
  • 📞400-888-8888
  • ✉️contact@rkmt.cn
  • 🕐周一至周日 9:00-21:00

© 2024 北京尧图网络科技有限公司 版权所有 | 京 ICP 备 XXXXXXXX 号