Bug #8762

CFLAGS and LDFLAGS are not set properly in Makefile when they are already set as environment variables

Added by Charlie Somerville over 3 years ago. Updated about 3 years ago.

Target version:
ruby -v:


When the environment variable CFLAGS does not exist, running the ./configure script outputs this to the Makefile:

# ./configure && grep ^CFLAGS Makefile
...configure output...
CFLAGS = ${cflags} $(ARCH_FLAG)

However, setting the environment variable CFLAGS to anything (even the empty string) causes ${cflags} to not be included in the CFLAGS written to the Makefile:

# CFLAGS= ./configure && grep ^CFLAGS Makefile
...configure output...

The same happens with LDFLAGS. I believe this is what is breaking trunk compilation for both myself and spastorino in #8759.


#1 [ruby-core:56492] Updated by Nobuyoshi Nakada over 3 years ago

  • Status changed from Open to Rejected
  • Priority changed from 5 to 3

No, it's not the culprit.

#2 [ruby-core:58936] Updated by Aman Gupta about 3 years ago

  • Status changed from Rejected to Open
  • Assignee set to Nobuyoshi Nakada
  • Priority changed from 3 to Normal
  • Target version set to 2.1.0

I believe this is still a bug.

If I compile ruby with a custom CFLAGS, the build will lose all optimization and warning flags:

$ CFLAGS="-I/opt/include " ./configure >/dev/null && make -n gc.o
gcc -I/opt/include -D_FORTIFY_SOURCE=2 -fstack-protector -fno-strict-overflow -fvisibility=hidden -DRUBY_EXPORT -fPIE -I. -I.ext/include/x86_64-linux -I./include -I. -o gc.o -c gc.c

$ ./configure >/dev/null && make -n gc.o
gcc -O3 -fno-fast-math -ggdb3 -Wall -Wextra -Wno-unused-parameter -Wno-parentheses -Wno-long-long -Wno-missing-field-initializers -Wunused-variable -Werror=pointer-arith -Werror=write-strings -Werror=declaration-after-statement -Werror=implicit-function-declaration -ansi -std=iso9899:199409 -D_FORTIFY_SOURCE=2 -fstack-protector -fno-strict-overflow -fvisibility=hidden -DRUBY_EXPORT -fPIE -I. -I.ext/include/x86_64-linux -I./include -I. -o gc.o -c gc.c

This issue is resolved in my environment with the following patch:

diff --git a/ b/
index 98749de..e6b82df 100644
--- a/
+++ b/
@@ -57,7 +57,7 @@ CC_VERSION = @CC_VERSION@
+CFLAGS = $(cflags) @CFLAGS@ $(ARCH_FLAG)
cflags = @cflags@
optflags = @optflags@
debugflags = @debugflags@
diff --git a/ b/
index 88d24ee..99d78e4 100644
--- a/
+++ b/
@@ -852,7 +852,7 @@ if test "$GCC" = yes; then

-test -z "${ac_env_CFLAGS_set}" -a -n "${cflags+set}" && eval CFLAGS="\"$cflags $ARCH_FLAG\""
+test -z "${ac_env_CFLAGS_set}" -a -n "${ARCH_FLAG+set}" && eval CFLAGS="\"$ARCH_FLAG\""
test -z "${ac_env_CXXFLAGS_set}" -a -n "${cxxflags+set}" && eval CXXFLAGS="\"$cxxflags $ARCH_FLAG\""


#3 [ruby-core:58941] Updated by Nobuyoshi Nakada about 3 years ago

  • Status changed from Open to Rejected

If you set CFLAGS, that is you say it must be used.

#4 [ruby-core:58942] Updated by Aman Gupta about 3 years ago

With my patch, provided CFLAGS are appended to the built-in flags. This follows the behavior most projects use with ./configure.

On trunk now, what is the correct way to add a CFLAG for build? (i.e. -Iextra/include/dir)?

Also available in: Atom PDF