• on gcc, make and CFLAGS

    From Meredith Montgomery@21:1/5 to All on Fri Nov 19 18:23:05 2021
    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ben Bacarisse@21:1/5 to Meredith Montgomery on Sat Nov 20 00:26:08 2021
    Meredith Montgomery <[email protected]> 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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From James Kuyper@21:1/5 to Meredith Montgomery on Sat Nov 20 00:16:34 2021
    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Jorgen Grahn@21:1/5 to Meredith Montgomery on Sat Nov 20 21:32:52 2021
    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)
  • From Scott Lurndal@21:1/5 to Meredith Montgomery on Sun Nov 21 15:43:59 2021
    Meredith Montgomery <[email protected]> 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 "@".

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Keith Thompson@21:1/5 to Scott Lurndal on Sun Nov 21 13:40:34 2021
    [email protected] (Scott Lurndal) writes:
    Meredith Montgomery <[email protected]> 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) [email protected]
    Working, but not speaking, for Philips
    void Void(void) { Void(); } /* The recursive call of the void */

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)