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?

  • 2
    What does your Makefile contain? – Stephen Kitt Jan 5 '17 at 16:46
  • Erm... where would that be? Or must I create one at the project root? Is there a system wide Makefile? – Dave Lawrence Jan 5 '17 at 16:48
  • Wherever you are when you type make and it runs cc x.c -o x. (If there is none, that's useful to know too.) – Stephen Kitt Jan 5 '17 at 16:52
  • there is none in that directory anyway - big surprise. Reading up on makefile now – Dave Lawrence Jan 5 '17 at 16:56

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


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.

| improve this answer | |
  • Brilliant! :) that worked – Dave Lawrence Jan 5 '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 5 '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 6 '17 at 9:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.