Reputation: 37
New guy learning programming with C in ubuntu Using a hello.c for example From some video tutorial I saw the teacher can use "make hello" to compile the hello.c without setting up a Makefile in current directory And the command goes like
bash$ make hello
gcc -g -Wall hello.c -o hello
I tried to use make on my own ubuntu 14.04 I installed build-essential and tried out the same hello.c
bash$ make hello
cc hello.c -o hello
It goes like above How can I make changes to get -g, -Wall or any other flags?
Upvotes: 0
Views: 492
Reputation: 1193
The way make
works is that it produces targets out of sources through known recipes.
When you write your own Makefile, you provide the recipes for it, however, make
has internal recipes, which can be used. You can list them by make -p
.
One of the recipes tells make
how to produce <something> out of <something>.c. When you run make hello
, make
checks how to produce hello, finds that there is a file hello.c and that it knows how to produce hello from hello.c -- using that internal rule.
Now the rule looks like this.
%: %.c
# recipe to execute (built-in):
$(LINK.c) $^ $(LOADLIBES) $(LDLIBS) -o $@
and the important part, $(LINK.c)
looks like this
LINK.c = $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH)
You don't need to understand the syntax at this point, the important thing is, that your make hello
will be transformed into (some unused variables omitted)
$(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) hello.c -o hello
Now CC, CFLAGS, CPPFLAGS and LDFLAGS may be set by make
(in your case, CC is set to "cc" and the rest is not set), but you can override this with your environment variables. What you want is having
CC=gcc
CFLAGS="-g -Wall"
To do that for one command only run:
CC=gcc CFLAGS="-g -Wall" make hello
To do that for one session (until you close your terminal) run
export CC=gcc
export CFLAGS="-g -Wall"
and then just make hello
or make whatever
as long as you want.
To do that permanently, set these variables in your .profile
file (open ~/.profile
(create it if it doesn't exist) and add
export CC=gcc
export CFLAGS="-g -Wall"
to it. Or just run
echo 'export CC=gcc' >> ~/.profile
echo 'export CFLAGS="-g -Wall"' >> ~/.profile
In both cases, you need to source ~/.profile
or start a new terminal. It will work happily ever after.
Upvotes: 1
Reputation: 70253
Make has a number of implicit rules, which are used in the absence of a Makefile.
The one regarding .c
files is:
n.o is made automatically from n.c with a recipe of the form
$(CC) $(CPPFLAGS) $(CFLAGS) -c
This means you can set the environment variables:
CC
to set the compiler used;CPPFLAGS
to set the preprocessor flags used (the same would be used e.g. for C++ .cpp
or Fortran .F
sources);CFLAGS
to set the compiler flags used.The implicit rule turns the .c
source file into a .o
object file, which is then linked to an executable according to another implicit rule:
n is made automatically from n.o by running the linker (usually called ld) via the C compiler. The precise recipe used is ‘$(CC) $(LDFLAGS) n.o $(LOADLIBES) $(LDLIBS)’.
Again, you see the environment variables used.
Upvotes: 0