Makefile - 常用标准变量

CFLAGS and EXTRA_CFLAGS

在用于编译软件的 Makefile 中, EXTRA_CFLAGS 通常是一个变量, 用于指定在编译过程中应包含的附加编译器标志。这些标志会附加到默认编译器标志之后。

下面是详细说明:

  • CFLAGS: 这是 Makefiles 中的一个标准变量,用于指定编译 C 源文件的编译器标志。

  • EXTRA_CFLAGS: 这是标准 CFLAGS 变量的扩展。它允许添加额外的编译器标志,而不覆盖 CFLAGS 中指定的现有标志。

例如,请考虑以下情况:

CFLAGS := -Wall -O2

EXTRA_CFLAGS := -DDEBUG -g

myprogram: myprogram.c

    gcc $(CFLAGS) $(EXTRA_CFLAGS) -o myprogram myprogram.c

In a Makefile used for compiling software, EXTRA_CFLAGS is typically a variable used to specify additional compiler flags that should be included during the compilation process. These flags are appended to the default compiler flags.

Here's a breakdown:

  • CFLAGS: This is a standard variable in Makefiles used to specify the compiler flags for compiling C source files.

  • EXTRA_CFLAGS: This is an extension to the standard CFLAGS variable. It allows additional compiler flags to be added without overriding the existing flags specified in CFLAGS.

For example, consider the following scenario:

在本例中:

  • CFLAGS 指定常用的编译器标志,如启用警告 (-Wall) 和优化级别 2 (-O2)。

  • EXTRA_CFLAGS 指定额外的标志,如定义调试宏 (-DDEBUG) 和启用调试信息 (-g)。

  • 编译 myprogram.c 以创建 myprogram 可执行文件时,会同时使用 CFLAGS 和 EXTRA_CFLAGS。

因此,EXTRA_CFLAGS 是一种在编译过程中动态添加编译器标志的方法,而无需直接修改标准 CFLAGS 变量。这对于向编译器传递额外选项(如宏、优化设置或调试标志)非常有用。

In this example:

  • CFLAGS specifies common compiler flags like enabling warnings (-Wall) and optimization level 2 (-O2).

  • EXTRA_CFLAGS specifies additional flags such as defining a macro for debugging (-DDEBUG) and enabling debugging information (-g).

  • When compiling myprogram.c to create the myprogram executable, both CFLAGS and EXTRA_CFLAGS are used.

So, EXTRA_CFLAGS is a way to dynamically add compiler flags to the build process without directly modifying the standard CFLAGS variable. This can be useful for passing additional options, such as macros, optimization settings, or debugging flags, to the compiler.

注意,在EXTRA_CFLAGS中使用 -D 选项时,以下格式均可:

-DTEST

-D TEST

-DTEST=1

-D TEST=1

CXXFLAGS

在 Makefile 中编译 C++ 程序时,通常使用 CXXFLAGS 变量来指定 C++ 专用的编译器标志。

下面是一个典型的细分:

  • CXXFLAGS: 该变量用于指定编译 C++ 源文件的编译器标志。其功能类似于 C 源文件的 CFLAGS。

下面是一个如何在 Makefile 中使用 CXXFLAGS 的示例:

CXXFLAGS := -Wall -O2 -std=c++11

LDFLAGS := -lm

myprogram: myprogram.cpp

    g++ $(CXXFLAGS) -o myprogram myprogram.cpp $(LDFLAGS)

In a Makefile, when compiling C++ programs, you typically use the CXXFLAGS variable to specify compiler flags specific to C++.

Here's a typical breakdown:

  • CXXFLAGS: This is the variable used to specify the compiler flags for compiling C++ source files. It's similar in function to CFLAGS for C source files.

Here's an example of how you might use CXXFLAGS in a Makefile:

在本例中:

  • CXXFLAGS 指定常用的编译器标志,如启用警告 (-Wall)、优化级别 2 (-O2),以及使用 C++11 标准 (-std=c++11)。

  • LDFLAGS 指定链接器标志(非 C++ 特有),如链接数学库 (-lm)。

  • 编译 myprogram.cpp 以创建 myprogram 可执行文件时,会使用 CXXFLAGS 和 LDFLAGS。

通过这种设置,您可以将特定于 C++ 的编译器标志与 C 编译器标志分开指定,从而为您的 Makefile 提供灵活性和清晰度。

In this example:

  • CXXFLAGS specifies common compiler flags like enabling warnings (-Wall), optimization level 2 (-O2), and using the C++11 standard (-std=c++11).

  • LDFLAGS specifies linker flags (not specific to C++), such as linking against the math library (-lm).

  • When compiling myprogram.cpp to create the myprogram executable, CXXFLAGS and LDFLAGS are used.

This setup allows you to specify C++-specific compiler flags separately from C compiler flags, providing flexibility and clarity in your Makefile.

EXTRA_CXXFLAGS

在标准的 GNU Makefile 中, 并没有一个预定义的 EXTRA_CXXFLAGS 变量, 就像 C 程序的 EXTRA_CFLAGS 变量一样。不过, 如果您希望为 C++ 源文件提供额外的标志, 可以在 Makefile 中创建并使用 EXTRA_CXXFLAGS。

下面是如何在 Makefile 中定义和使用 EXTRA_CXXFLAGS:

CXXFLAGS := -Wall -O2 -std=c++11

EXTRA_CXXFLAGS := -fno-exceptions

myprogram: myprogram.cpp

    g++ $(CXXFLAGS) $(EXTRA_CXXFLAGS) -o myprogram myprogram.cpp

In the context of a standard GNU Makefile, there isn't a predefined EXTRA_CXXFLAGS variable, like there is with EXTRA_CFLAGS for C programs. However, you can create and use EXTRA_CXXFLAGS in your Makefile if you wish to provide additional flags specifically for C++ source files.

Here's how you might define and use EXTRA_CXXFLAGS in a Makefile:

在本例中:

  • CXXFLAGS 指定用于编译 C++ 源文件的常用编译器标志。

  • EXTRA_CXXFLAGS 为 C++ 源文件指定额外的编译器标志,如 -fno-exceptions。

  • 在编译 myprogram.cpp 时,会同时使用 CXXFLAGS 和 EXTRA_CXXFLAGS。

虽然 EXTRA_CXXFLAGS 并不像 CFLAGS 或 CXXFLAGS 那样是一个标准变量, 但您可以在 Makefile 中定义并使用它, 以专门为 C++ 程序提供额外的编译器标志, 就像为 C 程序提供 EXTRA_CFLAGS 一样。

In this example:

  • CXXFLAGS specifies common compiler flags for compiling C++ source files.

  • EXTRA_CXXFLAGS specifies additional compiler flags that you want to include for C++ source files, such as -fno-exceptions.

  • During the compilation of myprogram.cpp, both CXXFLAGS and EXTRA_CXXFLAGS are used.

While EXTRA_CXXFLAGS is not a standard variable like CFLAGS or CXXFLAGS, you can define and use it in your Makefile to provide additional compiler flags specifically for C++ programs, just like you would with EXTRA_CFLAGS for C programs.

LDFLAGS

LDFLAGS 是一个标准的 makefile 变量,用于为链接器指定标志。在将对象文件链接到可执行文件或库时,它允许您向链接器传递额外的选项。

下面是在 Makefile 中使用 LDFLAGS 的典型方法:

LDFLAGS := -L/path/to/library -lmylibrary

myprogram: myprogram.o

    $(CC) $(LDFLAGS) -o myprogram myprogram.o

LDFLAGS is a standard makefile variable used to specify flags for the linker. It allows you to pass additional options to the linker when linking object files into an executable or a library.

Here's how you typically use LDFLAGS in a Makefile:

在本例中:

  • LDFLAGS 指定链接器标志,例如 -L 用于指定包含库的目录,-l 用于指定要链接的库。

  • 在链接 myprogram.o 以创建 myprogram 可执行文件时,LDFLAGS 用于向链接器传递附加选项。

LDFLAGS 通常用于 Makefile 中,以控制链接器的行为,并与特定的库进行链接,或提供额外的链接器选项。

In this example:

  • LDFLAGS specifies linker flags, such as -L to specify a directory containing libraries and -l to specify a library to link against.

  • When linking myprogram.o to create the myprogram executable, LDFLAGS is used to pass additional options to the linker.

LDFLAGS is commonly used in Makefiles to control the linker behavior and link against specific libraries or provide additional linker options.

CFLAGS_MODULE

在编译 Linux 内核模块时,CFLAGS_MODULE 是一个变量,用于指定编译器标志,专门用于编译内核模块。这些标志在编译单个内核模块时使用。

下面是一个典型的使用场景:

obj-m += mymodule.o

CFLAGS_MODULE += -fno-pic

all:

    $(MAKE) -C $(KERNEL_SRC) M=$(PWD) modules

clean:

    $(MAKE) -C $(KERNEL_SRC) M=$(PWD) clean

In the context of building Linux kernel modules, CFLAGS_MODULE is a variable used to specify compiler flags specifically for compiling kernel modules. These flags are used when building individual kernel modules.

Here's a typical usage scenario:

在本例中

  • obj-m += mymodule.o 表示 mymodule.o 是作为内核模块构建的目标。

  • CFLAGS_MODULE += -fno-pic 将 -fno-pic 标志附加到 CFLAGS_MODULE 中。该标记会告诉编译器不要生成位置无关代码(PIC),PIC 通常用于共享库,但内核模块不需要。

  • $(MAKE) -C $(KERNEL_SRC) M=$(PWD) modules 是用来编译内核模块的命令。

In this example:

  • obj-m += mymodule.o indicates that mymodule.o is a target to be built as a kernel module.

  • CFLAGS_MODULE += -fno-pic appends the -fno-pic flag to CFLAGS_MODULE. This flag tells the compiler not to generate position-independent code (PIC), which is typically used for shared libraries but is unnecessary for kernel modules.

  • $(MAKE) -C $(KERNEL_SRC) M=$(PWD) modules is the command used to build the kernel modules.

So, CFLAGS_MODULE allows you to specify compiler flags specifically for building kernel modules, which may differ from the flags used for other parts of the kernel or for user-space programs.

Variables Used by Implicit Rules (隐式规则使用的变量 )

内置隐式规则中的配方会大量使用某些预定义变量。你可以在 makefile、make 参数或环境中改变这些变量的值,从而改变隐式规则的工作方式,而无需重新定义规则本身。你可以使用"-R "或"--no-builtin-variables "选项取消隐式规则使用的所有变量。

例如,用于编译 C 源文件的口诀实际上是"$(CC) -c $(CFLAGS) $(CPPFLAGS)"。所使用变量的默认值是 "cc "和无,因此命令是 "cc -c"。通过将 "CC "重新定义为 "ncc",可以使 "ncc "用于隐式规则执行的所有 C 语言编译。将 "CFLAGS "重新定义为"-g",就可以在每次编译时传递"-g "选项。所有进行 C 语言编译的隐式规则都使用"$(CC) "来获取编译器的程序名,并在给编译器的参数中包含"$(CFLAGS)"。

隐式规则中使用的变量分为两类:一类是程序名称(如 CC),另一类是包含程序参数的变量(如 CFLAGS)("程序名称 "也可能包含一些命令参数,但必须以实际的可执行程序名称开头)。如果变量值包含多个参数,请用空格分隔。

下表介绍了一些常用的预定义变量。该列表并不详尽,此处显示的默认值可能不是 make 为你的环境选择的值。要查看 GNU make 实例的完整预定义变量列表,可以在没有 makefile 的目录下运行 "make -p"。

The recipes in built-in implicit rules make liberal use of certain predefined variables. You can alter the values of these variables in the makefile, with arguments to make, or in the environment to alter how the implicit rules work without redefining the rules themselves. You can cancel all variables used by implicit rules with the ‘-R’ or ‘--no-builtin-variables’ option.

For example, the recipe used to compile a C source file actually says ‘$(CC) -c $(CFLAGS) $(CPPFLAGS)’. The default values of the variables used are ‘cc’ and nothing, resulting in the command ‘cc -c’. By redefining ‘CC’ to ‘ncc’, you could cause ‘ncc’ to be used for all C compilations performed by the implicit rule. By redefining ‘CFLAGS’ to be ‘-g’, you could pass the ‘-g’ option to each compilation. All implicit rules that do C compilation use ‘$(CC)’ to get the program name for the compiler and all include ‘$(CFLAGS)’ among the arguments given to the compiler.

The variables used in implicit rules fall into two classes: those that are names of programs (like CC) and those that contain arguments for the programs (like CFLAGS). (The “name of a program” may also contain some command arguments, but it must start with an actual executable program name.) If a variable value contains more than one argument, separate them with spaces.

The following tables describe of some of the more commonly-used predefined variables. This list is not exhaustive, and the default values shown here may not be what make selects for your environment. To see the complete list of predefined variables for your instance of GNU make you can run ‘make -p’ in a directory with no makefiles.

Makefile常用内置标准变量列表

下面列出了 Makefile 中一些常用的标准变量:

1. CC:指定要使用的 C 编译器。

2. CXX: 指定要使用的 C++ 编译器。

3. CFLAGS: C 编译器的标志。

4. CXXFLAGS:C++ 编译器的标志: 用于 C++ 编译器的标志。

5. CPPFLAGS: 用于 C 预处理器的标志。

6. LDFLAGS:链接器的标志: 链接器的标志。

7. LDLIBS: 要链接的库。

8. AR:指定存档程序(用于创建库)。

9. AS:指定汇编程序。

10. RM: 指定删除文件的命令(如 rm -f)。

11. MAKE:make 程序的名称(默认为 make)。

12. MAKEFLAGS: 传递给子 make 的标志(如 -j 用于并行编译)。

13. OBJDIR:指定存放对象文件的目录。

14. VPATH:指定搜索源文件的其他目录。

15. SHELL:指定要使用的 shell 程序(默认为 /bin/sh)。

16. CPP: 运行 C 预处理器的程序,将结果输出到标准输出;默认为"$(CC) -E"。

这些变量通常用于 Makefile,以控制编译和链接过程。根据项目的复杂程度和具体要求,您可以使用其他变量或进一步定制这些变量。

其中一些变量的默认值是空字符串,比如一些FLAGS。

Here's a list of some commonly used standard variables in Makefiles:

1. CC: Specifies the C compiler to be used. default ‘cc’.

2. CXX: Specifies the C++ compiler to be used. default ‘g++’.

3. CFLAGS: Flags for the C compiler.

4. CXXFLAGS: Flags for the C++ compiler.

5. CPPFLAGS: Flags for the C preprocessor.

6. LDFLAGS: Flags for the linker.

7. LDLIBS: Libraries to link against.

8. AR: Specifies the archiver program (used for creating libraries). default ‘ar’.

9. AS: Specifies the assembler program. default ‘as’.

10. RM: Specifies the command to remove files (e.g., rm -f).

11. MAKE: The name of the make program (default is make).

12. MAKEFLAGS: Flags passed to sub-makes (e.g., -j for parallel builds).

13. OBJDIR: Specifies the directory where object files are stored.

14. VPATH: Specifies additional directories to search for source files.

15. SHELL: Specifies the shell program to be used (default is /bin/sh).

16. CPP: Program for running the C preprocessor, with results to standard output; default ‘$(CC) -E’.

17. ARFLAGS: Flags to give the archive-maintaining program; default ‘rv’.

18. ASFLAGS: Extra flags to give to the assembler (when explicitly invoked on a ‘.s’ or ‘.S’ file).

These variables are commonly used in Makefiles to control the compilation and linking process. Depending on the complexity of your project and the specific requirements, you may use additional variables or customize these variables further.

参考:

Implicit Variables (GNU make)

  • 14
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夜流冰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值