允许用户覆盖CFLAGS,CXXFLAGS和朋友 [英] Allowing users to override CFLAGS, CXXFLAGS and friends

查看:120
本文介绍了允许用户覆盖CFLAGS,CXXFLAGS和朋友的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

典型的makefile通常使用内置变量CFLAGSCXXFLAGSCPPFLAGS 1 来设置传递给C,C ++或其他编译器/工具的标志.原则上,由于各种内置规则都使用这些标志,因此有时甚至甚至可以避免编写完整的编写配方.

Typical makefiles often use the built-in variables CFLAGS, CXXFLAGS, CPPFLAGS and so on1 to set the flags passed to the C, C++ or other compilers/tools. In principle, this sometimes even lets you avoid writing a compilation recipe entirely since the various built-in rules use these flags.

通常,makefile可能会将内容添加到需要编译的代码的FLAGS变量中,例如包含目录,指示使用哪种语言标准的参数等.这些变量还可能包含可选"或默认"参数,例如优化级别,警告级别以及可能有效更改或删除的其他设置.

In general, a makefile might add things to the FLAGS variables that are required for the code to compile, such as include directories, arguments indicating which language standard to use and so on. The variables might also include "optional" or "default" arguments, such as optimization level, warning level and other settings that might validly be altered or removed.

由于CFLAGS和字段是众所周知的"变量,因此它们显然也是最终用户的配置点.例如,如果默认情况下项目编译时没有调试信息,则预期make命令行上的CFLAGS=-g会导致-g被添加到$(CC)编译器命令行中,从而导致调试信息被添加到$(CC)编译器命令行中.生产的.同样,对于最终用户可能希望控制的其他选项,例如优化级别,gcc上的-march设置,等等.

Since CFLAGS and fields are "well known" variables, they are also apparently a configuration point for end users. For example, if a project compiles without debug information by default, it is expected that CFLAGS=-g on the make command line causes -g to be added to the $(CC) compiler command line and hence cause debug info to be produced. Similarly for other options the end user might want to control, such as the optimization level, the -march setting on gcc, and so on.

但是,这两种用法似乎与我不兼容.如果用户覆盖$(CFLAGS),则他们将删除如上所述的任何内部必需"标志,并且该项目可能无法编译或编译不正确.

However, these two uses seem incompatible to me. If the user overrides $(CFLAGS) they will obliterate any internal "required" flags as described above, and the project either may not compile or may compile incorrectly.

是否有处理此问题的最佳实践?对于像$(CC)这样的单值"变量,实际上不会出现相同的问题,因为它们通常只有一个值:在本示例中,使用的是C编译器.如果用户覆盖它,则使用其值.诸如$(CFLAGS)之类的东西原则上是一个值列表,其中一些值是内部值,不应覆盖,而其他值则用户可能希望覆盖.

Is there a best practice for handling this? The same problem doesn't really arise for "single value" variables like $(CC) since they generally have exactly one value: in this example, the C compiler to use. If the user overrides it, you use their value. Things like $(CFLAGS) are in principle a list of values, some of which are internal and shouldn't be overridden, an others which a user may want to override.

直觉上,一种解决方案似乎是将$(CFLAGS)和朋友保留为空且未在makefile中使用它们,而更喜欢说CFLAGS_INTERNAL作为makefile中的参数,然后将两者都放在命令行中.但是,我很好奇,是否有最佳实践,或者是否缺少明显的东西.

Intuitively, a solution seems to be to leave $(CFLAGS) and friends empty and unused in your makefile, preferring say CFLAGS_INTERNAL for in-makefile arguments, and then put both on the command line. I'm curious, however, if there is a best practice around this or if I'm missing something obvious.

1 对于此问题的其余部分,我通常会简单地参考$(CFLAGS),但要理解,这只是方便地代表了整个众所周知的编译器标志变量家族,例如$(CXXFLAGS)等.

1 For the rest of this question I will often simply refer to $(CFLAGS) with the understanding that this is simply a convenient representative of the whole family of well known compiler flag variables such as $(CPPFLAGS), $(CXXFLAGS) and so on.

推荐答案

在使用debuginfo软件包构建RPM时,偶然发现了相同的问题.

Just stumbled upon the same question while building an RPM with debuginfo package.

生成debuginfo的要求是在CFLAGS中传递-g,同时保留软件在Makefile中具有的所有CFLAGS.

The requirement for debuginfo generation is to pass -g in CFLAGS while preserving whatever CFLAGS the software has in its Makefile.

因此,如果要添加一些额外的位到CFLAGS,而不会覆盖Makefile中存在的那些,则可以简单地将CFLAGS用作环境变量.但是,只要有问题的Makefile使用CFLAGS += ...表示法即可.

So if you want to add some extra bits to CFLAGS, without overwriting the ones present in Makefile, you can simply use CFLAGS as an environment variable. But only as long as the Makefile in question uses CFLAGS += ... notation.

例如,假设您的软件具有Makefile的功能:

Example, suppose that you have software with Makefile having:

CFLAGS += $(ARCH) -O3 -std=gnu11 -Wall ...

要使用所有这些标志 -g构建它,您将执行以下操作:

To have it build with all those flags and -g, you will do:

CFLAGS='-g' make 

请注意,将其作为make的参数传递是无效的,例如:make CFLAGS='-g'是错误的,因为它将覆盖内部CFLAGS.

Note that passing it as an argument to make won't work, as in: make CFLAGS='-g' is wrong, because it will overwrite internal CFLAGS.

有关通过-g的解决方案的更多信息,以构建debuginfo软件包正确

More on the solution to pass -g for building debuginfo packages properly

有关 make:附加到变量的参考.

这篇关于允许用户覆盖CFLAGS,CXXFLAGS和朋友的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆