Jacob Jacob - 5 months ago 27
C Question

When compiling C code in the linux terminal, what is the difference between make <filename> and cc <filename>?

I'm new to C, and I understand that both of those commands accomplish the same thing, but does one of them do something different than the other along the way?

sps sps

First of all, if you are using make then for hello.c you will call make as make hello and not make hello.c. Also, note that make is most of the times used with a Makefile. Nonetheless, you can build executable binary from single source file using make as you have shown.

make will run the build commands only if it thinks that the source code has changed after last build, whereas cc will always do the build.

For example:

$ make hello
cc     hello.c   -o hello
$ make hello
make: 'hello' is up to date.   # Make does not think source file has changed
$ touch hello.c                # Update the timestamp of hello.c
$ make hello                   
cc     hello.c   -o hello      # Make thinks source file changed. Builds again

However, cc will not check if the source has changed or not. It will always do the required build.

$ cc hello.c -o hello
$ ls -l hello | cut -d ' ' -f '8-'    
12:18 hello
$ cc hello.c -o hello                # Build again without changing source
$ ls -l hello | cut -d ' ' -f '8-'
12:21 hello                          # hello was built again

Above description was for GNU make and GNU cc. Not sure about other implementations.