开发者

Why don't environment variables override variables set in makefiles by default?

I am compiling packages and I see that oftentimes Makefile authors write set CFLAGS in the makefile, with such and such options. I, on the other hand, would like t开发者_开发问答o try out some compiler optimizations and would like to propagate the compiler switches to make with as little trouble as possible. Not always is this doable though. For instance when a makefile specifies CFLAGS and I want all C compiler invocations to use -fomit-frame-pointer, short of having to explicitly write something like CFLAGS=-fomit-frame-pointer make, what are my options that are not hackish. From what I can see there is the above, then there is the same but different make "CFLAGS=-fomit-frame-pointer" and I can also do what I consider to be the best solution and the reason for this question:

export CFLAGS=-fomit-frame-pointer
make -e

I consider this the best one, because frankly even thought potentially dangerous flag, I don't debug software that much, and when I need to I can recompile a particular piece on demand, with debugging info and all. Otherwise I like to work with release software without debugging bells and whistles at all, especially if the package is not authored by me. So I guess what I ask here specifically is: why make does not automatically prefer environment variables to makefile's own? After all environment knows best what is what, and in case make author really needs to have something their way there is the 'override' syntax, right?


Make will override the variables if you put them on the make command line.

Try this: make CFLAGS=-fomit-frame-pointer

One word of warning. Most Makefiles don't expect their variables to be overridden. That means that if the Makefile uses CFLAGS to specify -I for include files, -O2 for optimization, or other flags, you must add them to your CFLAGS override or the make will probably fail.


That is arguable: "After all environment knows best what is what"

It's like saying "mother knows best" to an all-grown up child and stopping them from going to study law because you wanted to see what kind of plumber they'd be. It's their life. Let go and let them make their choice for themselves.

Oh wait, you were asking about environment and make. Right.

Well, make is a new process that starts after you've modified your environment and in the general case, the environment does not know what the make goals are and how it works. So, whatever make explicitly sets should trump whatever might happen to be in the environment.

Update: There's an argument I missed originally in my answer - predictability. The only requirement should be the presence of standardized set of tools (same version of GCC, same libs), and given that a properly written make file should always produce the same result, no matter what the environment is, or what other tools are present.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜