0

I am trying to make a simple Makefile for a hello world program in c currently, I compile with:

gcc -Wall -std=c99 helloWorld.c

In the Makefile what I have so far is:

CC = gcc
CFLAGS = -Wall -std=c99

helloWorld.exe: helloWorld.c
    $(CC) $(CFLAGS) -o helloWorld.exe

Also, that is a tab and not spaces in the second line
When I enter either make or make --trace I get:

make: *** No targets specified and no makefile found. Stop.

rsorki
  • 1
  • 5
  • 1
    There are some issues with your Makefile, but they are addressed in the answers below. I just wanted to make sure you know: the whitespace in front of `$(CC)` is a character. It's an easy thing to miss if you're new to `make`...and some editors silently replace tabs with spaces. – lockcmpxchg8b Nov 25 '17 at 08:59
  • Edit your question to show the output of `make --trace` and/or `make` – Basile Starynkevitch Nov 25 '17 at 22:24
  • What is the actual name of your make file? Are you in that directory when you run make? – paxdiablo Nov 26 '17 at 01:17
  • Name of the make file is Makefile, it is in the same directory as the c file that I am compiling and yes I am also in that directory – rsorki Nov 26 '17 at 06:17
  • On what operating system are you doing this? I hope you use Linux or some POSIX system – Basile Starynkevitch Nov 26 '17 at 08:13
  • I'm on Windows but I use cygwin for the terminal – rsorki Nov 28 '17 at 00:44

1 Answers1

2

Read documentation of make. Beware that TAB characters are significant in Makefile-s (most other build automation systems, e.g. ninja, don't have this historical misfeature). Your question and my answer don't show these tabs but you need to be sure they are there (each code line starting here with several spaces should really start with a single tab character).

helloWorld.exe: helloWorld.c
    $(CC) $(CFLAGS) -o helloWorld.exe

is wrong. You don't pass helloWorld.c to gcc.

You want at least

 helloWorld.exe: helloWorld.c
     $(CC) $(CFLAGS) -o helloWorld.exe helloWorld.c

and you could (and actually should) use the $@ and $^ special make automatic variables. Be also aware of builtin rules (try make -p). Perhaps on your system having just helloWorld.exe: helloWorld.c without explicit actions might be enough. I recommend having the usual clean: target with the appropriate rule.

You probably want to explicitly set in your Makefile, if invoking GCC:

CFLAGS= -Wall -g

because you should ask for all warnings and debug info (since gcc won't produce them if not asked to).

To debug your Makefile: try first make -n. Then make --trace and perhaps use remake with -x.

You'll find many examples of Makefile, e.g. this.

Be sure to clean your file tree (either with a good - but still missing - clean target and then make clean, or manually by removing any object files and any executable) after each edition of Makefile.

When I enter either make or make --trace I get:
make: *** No targets specified and no makefile found.

This means that you are running that make command in the wrong directory. Before running make check with pwd or /bin/pwd your working directory. It probably is not what you believe it is. List also files in it (with ls on Linux and POSIX, with DIR on MSDOS & Windows); you should see both your Makefile and your helloWorld.c at least.

Check also that your PATH variable is correct (e.g. with echo $PATH on POSIX systems).

PS. I am surprised of the .exe suffix. On my Linux system I don't use and don't need it.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • So I added helloWorld.c after the helloWorld.exe on the second line but it still does not work. I'm on windows using cygwin if that makes any difference. – rsorki Nov 25 '17 at 16:25
  • Then edit your question to show output of `make --trace` at least, and check that your `Makefile` does contain the tab character appropriately. – Basile Starynkevitch Nov 25 '17 at 22:14