How to compile a C program with make on Mac OS X Terminal

I don't know what exactly you did, but I think the mistake was running the wrong command. You typed make filename cc filename.c -o filename, but the tutorial instructed us to use make filename, without the cc filename.c -o filename part. Maybe you read an old version?

And, make filename works fine. You don't need a Makefile.


FYI, here's how I ran into the problem and how I solved it:

  1. typed the code below, and saved it in a file named "ex1"

      int main(int argc, char *argv[])
      {
          puts("Hello, World!");
    
          return 0;
      }
    
  2. typed make ex1in terminal

  3. got error message make: Nothing to be done for 'ex1'.

As you can see, my mistake was that the file name of the source code should be ex1.c, not ex1.

And as I changed the file name to ex1.c, and executed make ex1, it worked.


Create a file called Makefile on the same path with this content:

CC = cc
CFLAGS = -std=c99 -pedantic -Wall
OBJECTS = filename.o

all: appname

filename.o: filename.c
    $(CC) $(CFLAGS) -c filename.c

appname: $(OBJECTS)
    $(CC) $(OBJECTS) -o appname

clean:
    rm -f *.o appname

Then run:

make

Of course, replace appname with the name of your program.

Note: There must be a "tab" (not spaces) before

$(CC) $(CFLAGS) -c filename.c

and

$(CC) $(OBJECTS) -o appname

and

rm -f *.o appname

Enter image description here

Use:

make [filename]

./[filename]

If the filename is test.c and saved one the desktop then the commands are:

cd Desktop

make test

./test