2

I wrote a simple printf C code and made a simple makefile. When I run make with CFLAGS, CPPFLAGS and LDFLAGS, the values of the variables goes into a cc execution, followed by a gcc execution without those values, like this:

$ CFLAGS="-I." CPPFLAGS="-D TESTEDEFINE" CXXFLAGS="TESTECXXFLAGS" LDFLAGS="-L." LFLAGS="TESTELFLAGS" make
cc -I. -D TESTEDEFINE -L.  teste.c   -o teste
gcc -o teste teste.c

When I run the built program, the define isn't defined since it gives me the printf of the not defined #else.

teste.c

#include <stdio.h>

int main()
{
#if defined(TESTEDEFINE)
  printf("TESTEDEFINE!!!");
#else
  printf("!!!");
#endif
  return 0;
}

Makefile

all: teste
  gcc -o teste teste.c
  • 1
    If you tell it to run `gcc -o teste teste.c`, that's what it will run. If you removed that line, you might find the macros work. Or use `${CC} -o $@ ${CPPFLAGS} ${CFLAGS} ${LDFLAGS} ${LDLIBS}` instead as the recipe line. – Jonathan Leffler Sep 14 '17 at 14:12
  • So the flags only work if I use them explicit in the Makefile or if I omit the command for the rule? – Marcos Taranta Sep 14 '17 at 14:40
  • might be helpful to see your makefile in its entirety ... or is that what you've shown? If so, you weren't kidding about "simple makefile" – yano Sep 14 '17 at 14:41
  • it is the entire Makefile, I was just testing to understand how the environment variables for flags actually work since the GNU website doesn't explain. – Marcos Taranta Sep 14 '17 at 14:45
  • 1
    That Makefile tells make that `all` depends on the build of `teste`, so make uses its built-in rules to build `teste` before executing the recipe. That's why `cc` is invoked. – rici Sep 14 '17 at 14:56
  • I'm certainly no makefile expert, but I've always defined those variables inside the makefile. That's what you're going to want to do anyway, unless you get joy out of extra typing. But as already mentioned those variables are no where to be found in the recipe. If you want to use them, then use them! – yano Sep 14 '17 at 14:56
  • 1
    Dud you try this part of the Gnu website: https://www.gnu.org/software/make/manual – rici Sep 14 '17 at 14:58
  • Specifically [section on environment variables](https://www.gnu.org/software/make/manual/html_node/Environment.html#Environment) but read the introduction first. – rici Sep 14 '17 at 15:01
  • The 'flags' as you refer to them are simply Make macros set on the command line. If the rules don't use those macros, then the macros are defined but unused. The built-in rules use those macros. Your makefile did not. – Jonathan Leffler Sep 14 '17 at 15:13
  • Delete your `Makefile` and just run `make teste` and it will work – Sean Bright Sep 14 '17 at 15:18

1 Answers1

4

The variables are for consistency, readability, and ease of use. Neither your compile nor your makefile reference them. The compiler does not automatically reference those variables.

Try this instead:

$ export CFLAGS="-I." CPPFLAGS="-D TESTEDEFINE" CXXFLAGS="TESTECXXFLAGS" LDFLAGS="-L." LFLAGS="TESTELFLAGS"
$ gcc $CFLAGS $CPPFLAGS $CXXFLAGS $LDFLAGS $LFLAGS -o teste teste.c

You would also need to define them in your makefile and reference them in the compiler line.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
John T Cox
  • 81
  • 8