On Fri, 2021-11-19, Meredith Montgomery wrote:
It seems to me that gcc by default doesn't read CFLAGS.
%echo $CFLAGS
-Wall
%cat hello.c
int main(void) {}
%gcc -c hello.c
%
Maybe it did read CFLAGS, maybe it didn't. I don't know. Does gcc care
at all about CFLAGS?
But it seems that make does look at CFLAGS by default. I have no
written Makefile (at this moment), then I say:
%make hello
cc -Wall hello.c -o hello
%
It did read CFLAGS. Nice.
Now let me write a Makefile.
%cat Makefile
hello: hello.o
gcc -o hello hello.o
hello.o: hello.c
gcc -c hello.c
%make hello
gcc -c hello.c
gcc -o hello hello.o
%
So my conclusion I must explicitly write $(CFLAGS) in the Makefile.
This makes sense --- if I tell make that the command is ``gcc -c
hello.c'', it should respect me and not add anything else.
It's a bit unclear what you want to do, if you need to use old make implementations or can stick to GNU Make, and if you've read the
documentation.
What I always do is put something like this in the Makefile:
CFLAGS=-W -Wall -pedantic -ansi -g -Os
CXXFLAGS=-W -Wall -pedantic -std=c++14 -g -Os
CPPFLAGS=-Isome/where -Dsomething
With the builtin GNU Make rules, that's enough to compile C and C++
to object code. Note that I split cpp[1] stuff into CPPFLAGS.
If I want to override things and I don't want to edit the Makefile --
and this happens very rarely -- I tend not to use the environment, but
this syntax:
% make CFLAGS=-std=c99 foo.o
Oddly, I cannot find that in the GNU Make manual. Not where it should
be, anyway.
I know there are rules for when GNU Make picks stuff from the
environment, and when it doesn't. Those /are/ documented.
I never could figure out how to use the builtin rules for linking,
so I write that rule explicitly.
Lastly, I actually override the whole .o: .c rule, since I enable
automatic dependency generation with the help of gcc. But that's
orthogonal to the CFLAGS stuff.
There's an example here (the code is mostly C++ but that doesn't
matter):
https://github.com/kjgrahn/gavia
/Jorgen
[1] That's the C preprocessor, not an alternative name for C++.
--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)