user1869741
user1869741

Reputation:

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

I recently bought a MacBook Pro and I wanted to do some coding in C with the terminal.

I'm able to compile the code with this command:

gcc filename.c –o filename

But I want to compile it with the make command, because I know it is best practice and I want to follow best practice.

make filename cc filename.c -o filename

This command is giving me the following output:

make: Nothing to be done for `ex01'.
make: *** No rule to make target `cc'.  Stop.

Note that I have installed Xcode and Xcode developer command-line tools and in the folder /usr/bin I see the make and makefile properties.

What should I do to be able to compile with a makefile and a cc argument?

Upvotes: 12

Views: 74611

Answers (3)

David Ranieri
David Ranieri

Reputation: 41046

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

Upvotes: 17

kly
kly

Reputation: 89

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.

Upvotes: 5

Anup Panchal
Anup Panchal

Reputation: 11

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

Upvotes: 1

Related Questions