0

Currently when I go invoke make I see the following being executed

cc x.c -o x

What I would like to see is the following

cc -ggdb3 -O0 -std=c99 -Wall -Werror x.c -l<lib> -lm -o x

or maybe

clang -ggdb3 -O0 -std=c99 -Wall -Werror x.c -l<lib> -lm -o x

Obviously I could type that every time but I'd prefer when I use make to ultimately use cc with those params.

Is there a make config file somewhere?

Rui F Ribeiro
  • 55,929
  • 26
  • 146
  • 227

1 Answers1

2

make generally uses variables for this; in particular:

  • CC specifies the C compiler to use;
  • CFLAGS specifies C compiler flags to pass the compiler;
  • LDLIBS specifies libraries to add (although in general you should really write a Makefile which defines everything your program needs to build, which typically includes libraries).

So you'd run

make CFLAGS="-ggdb3 -O0 -std=c99 -Wall -Werror" LDLIBS="-lm" x

to get

cc -ggdb3 -O0 -std=c99 -Wall -Werror x.c -lm -o x

and

make CC="clang" CFLAGS="-ggdb3 -O0 -std=c99 -Wall -Werror" LDLIBS="-lm" x

to get

clang -ggdb3 -O0 -std=c99 -Wall -Werror x.c -lm -o x

You can also set the variables in your environment (which allows defaults to be set in your shell startup scripts):

export CFLAGS="-ggdb3 -O0 -std=c99 -Wall -Werror"
make LDLIBS="-lm" x

Given the absence of a Makefile in your case, you'll probably find How does this Makefile makes C program without even specifying a compiler? interesting! The GNU Make manual will also come in handy.

Stephen Kitt
  • 411,918
  • 54
  • 1,065
  • 1,164
  • Brilliant! :) that worked – Dave Lawrence Jan 05 '17 at 17:23
  • You're welcome! Be careful if you set defaults though, it can wreak havoc with some builds (so if you download some software, try to build it and it fails, try again without your defaults). – Stephen Kitt Jan 05 '17 at 17:27
  • Good point. While env variables are fine for the moment I intend on putting together my own makefiles. That way I can restrict any changes to where they should be applied. Thanks again. – Dave Lawrence Jan 06 '17 at 09:49