12

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • In `gcc filename.c –o filename`, "–" is an [en dash](https://en.wiktionary.org/wiki/en_dash#Noun), not [ASCII](https://en.wikipedia.org/wiki/ASCII#Printable_characters) 0x2D ("-"). (Is in all revisions.) – Peter Mortensen Apr 03 '22 at 15:16

3 Answers3

17

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
David Ranieri
  • 39,972
  • 7
  • 52
  • 94
  • 1
    Thanks Alter Mann that solved my issue I'll gladly enjoy the coding in the terminal now.Just another question even if it solve it do you know why this doesn't work .make filename cc filename.c -o filename is it just not supported on MacOs or is some configuration issue or it just not the way to do it ? –  Oct 16 '14 at 18:24
  • 1
    @bigpop That command-line syntax is not supported by any `make`, anywhere, ever, on any system. Don't know where you got it from but wherever you did, you should avoid using that as a reference in the future. Also, I don't understand the statement in the answer _[there must be a] blank line before_ the variable assignment: there's no requirement for that that I know of. – MadScientist Oct 17 '14 at 00:10
  • @MadScientist Yes you are right I won't use it I was just a bit confused with this :s . Anyway thanks for the explanation appreciate it :) . –  Oct 17 '14 at 15:35
  • 1
    Why not before `rm -f *.o appname` as well? Why would it be different? – Peter Mortensen Apr 03 '22 at 15:21
5

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
kly
  • 89
  • 1
  • 2
1

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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • Please review *[Why not upload images of code/errors when asking a question?](https://meta.stackoverflow.com/questions/285551/)* (e.g., *"Images should only be used to illustrate problems that* ***can't be made clear in any other way***, *such as to provide screenshots of a user interface.*) and take the appropriate [action](https://stackoverflow.com/posts/66101455/edit) (it covers answers as well). Thanks in advance. – Peter Mortensen Apr 03 '22 at 15:38