Menu

#6 Honour pre-set CFLAGS and LDFLAGS.

Unstable_(example)
open
None
5
2023-05-08
2023-05-06
Drei Eck
No

Current Makefile hard-sets $CFLAGS and $LDFLAGS.

Feature request: Do not re-set the flags, but just append options.

The following patch, which does this, is taken from the asap Arch Linux package:

--- a/Makefile  2021-10-07 21:48:58.204086012 -0700
+++ b/Makefile  2021-10-07 22:14:39.290565502 -0700
@@ -3,9 +3,9 @@
 bindir = $(prefix)/bin
 libdir = $(prefix)/lib
 CC = gcc
-CFLAGS = -O2 -Wall
+CFLAGS += -O2 -Wall
 CPPFLAGS =
-LDFLAGS = -s
+LDFLAGS += -s
 AR = ar
 ARFLAGS = rc
 DO_CC = $(DO)$(CC) $(CFLAGS) $(CPPFLAGS) -o $@ $(if $(filter %.so,$@),-shared -fPIC) $(INCLUDEOPTS) $(filter %.c,$^) $(LDFLAGS)
1 Attachments

Discussion

  • Drei Eck

    Drei Eck - 2023-05-06

    (Use even -O3 and not -O2 to achieve even more optimisation. Or not set this at all, so the user can influence this via $CFLAGS, e.g. can also set -Og for debug optimisation.)

     
  • Piotr Fusik

    Piotr Fusik - 2023-05-07
    • assigned_to: Piotr Fusik
     
  • Piotr Fusik

    Piotr Fusik - 2023-05-07

    I've seen CFLAGS = a lot. Even GNU Coding Standards promotes it. It doesn't stop from overriding the flags on the command line:

    C:\0\a8\asap\git>make CFLAGS=-g V=1 asapconv
    mkdir -p . && gcc -g  -o asapconv  -I. asapconv.c asap.c -s
    

    Why is += superior? I understand it enables environment variables, but why would we want to set flags via an environment variable and not the make command line?

     
    • Drei Eck

      Drei Eck - 2023-05-08

      Ahoj,

      It doesn't stop from overriding the flags on the command
      line:

      C:\0\a8\asap\git>make CFLAGS=-g V=1 asapconv
      

      Ah, OK, I was not aware of that!

      Why is += superior? I understand it enables environment variables,
      but why would we want to set flags via an environment variable and
      not the make command line?

      In your use case, you can still specify specific CFLAGS that are needed
      to build the project correctly (I suppose mainly when you forcefully
      need to switch off some optimisations).

      In the use case I am referring to, it is helpful with Arch Linux
      packaging:

      There you can define in a global configuration file standard CFLAGS,
      which get passed to the build scripts via environment variable.

      This way, they are automatically honoured if the software's build
      system honours the envoronment variable.

      OK, if this patch is not applied, I can add in the package manually the
      inclusion of $CFLAGS, or just keep the patch around.

      Regards!

       

      Last edit: Drei Eck 2023-05-08

Log in to post a comment.