Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

on gcc, make and CFLAGS

223 views
Skip to first unread message

Meredith Montgomery

unread,
Nov 19, 2021, 4:25:34 PM11/19/21
to
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.

Ben Bacarisse

unread,
Nov 19, 2021, 7:26:12 PM11/19/21
to
Meredith Montgomery <mmont...@levado.to> writes:

> 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.

You could make sure by introducing a mistake that -Wall would catch:

$ cat h.c
int main(void) { int x; }
$ gcc -Wall -c h.c
h.c: In function ‘main’:
h.c:1:22: warning: unused variable ‘x’ [-Wunused-variable]
1 | int main(void) { int x; }
| ^
$ gcc -c h.c
$ echo $CFLAGS
-Wall

> Does gcc care at all about CFLAGS?

No.

> 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.

GNU make's default rule (for this case) uses $(CC) $(CPPFLAGS) $(CFLAGS) -c

> 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.

Yup. And if you just define CFLAGS in the makefile, the default rule
will use it, but you can also ask that the value from the environment be
used instead with the -e flag to make.

--
Ben.

James Kuyper

unread,
Nov 20, 2021, 12:16:39 AM11/20/21
to
On 11/19/21 4:23 PM, Meredith Montgomery wrote:
> It seems to me that gcc by default doesn't read CFLAGS.

Assuming you're using a unix-like system, run "man gcc" and search for
"ENVIRONMENT". It lists all of the environment variables that gcc pays
attention to, and there's quite a few of them, but CFLAGS isn't one of
them.

> But it seems that make does look at CFLAGS by default.

make automatically creates a make macro corresponding to each
environment variable that is set at the time it is run, with the same
value, unless make is invoked with a makefile, in which case make macros
that are explicitly defined by the makefile take precedence over the
values of corresponding environment variables.

> ... I have no
> written Makefile (at this moment), then I say:
>
> %make hello
> cc -Wall hello.c -o hello


Even with no makefile present, make has a fairly large number of default
rules. The set of default rules is, in general, configurable by whoever
installed make. You can execute "make -p -f /dev/null" to find out what
they are. However, a certain minimum set of rules is mandatory according
to POSIX, and those include the following:

CC=c99
CFLAGS=-O 1
LDFLAGS=

.c:
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $<

.c.o:
$(CC) $(CFLAGS) -c $<

Knowing what the default rules are can greatly simplify your use of make.

Jorgen Grahn

unread,
Nov 20, 2021, 4:32:56 PM11/20/21
to
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 .

Scott Lurndal

unread,
Nov 21, 2021, 10:44:03 AM11/21/21
to
Meredith Montgomery <mmont...@levado.to> writes:
>It seems to me that gcc by default doesn't read CFLAGS.

gcc knows nothing about CFLAGS, nor does it generally
use environment variables.

CFLAGS is a make variable that is part of the default rule
for compling C source files.

$ make -p

will print the default rules. The default rule for C is:

CC = cc
COMPILE.c = $(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c

%.o: %.c
# recipe to execute (built-in):
$(COMPILE.c) $(OUTPUT_OPTION) $<

One can always override such rules in the Makefile, for example:

%.o $(OBJDIR)/%.o: %.c
@$(QUIET) || echo " COMPILE $<"
$(HUSHCOMPILE)$(CC) $(CFLAGS) -MMD -MP -o $@ -c $<


QUIET can be either "true" or "false".
HUSHCOMPILE can be not present or set to "@".

Keith Thompson

unread,
Nov 21, 2021, 4:40:38 PM11/21/21
to
sc...@slp53.sl.home (Scott Lurndal) writes:
> Meredith Montgomery <mmont...@levado.to> writes:
>>It seems to me that gcc by default doesn't read CFLAGS.
>
> gcc knows nothing about CFLAGS,

True.

> nor does it generally
> use environment variables.

Not quite true.

https://gcc.gnu.org/onlinedocs/gcc-11.2.0/gcc/Environment-Variables.html

[...]

--
Keith Thompson (The_Other_Keith) Keith.S.T...@gmail.com
Working, but not speaking, for Philips
void Void(void) { Void(); } /* The recursive call of the void */
0 new messages