Makefile error: No such file or directory

Someone please tell me why this Makefile is getting an error.
The Makefile, error message, and directory listing follow.
Thank you.

Makefile:
1
2
3
4
5
6
7
8
9
10
11
12
13
OBJS = obj/main.o obj/file1.o obj/file2.o

main.exe : $(OBJS)
	g++ -o main.exe $(OBJS)

obj/main.o : main.cpp
	g++ -c $@ $<

obj/file1.o : file1.cpp file1.h
	g++ -c $@ $<

obj/file2.o : ../lib/file2.cpp ../lib/file2.h
	g++ -c $@ $<


g++ make error message:
1
2
3
4
D:\wolf\Documents\teensy\demo_make\src>make
g++ -c obj/file1.o file1.cpp
g++: error: obj/file1.o: No such file or directory
make: *** [obj/file1.o] Error 1


directory structure (Makefile is in /src)
1
2
3
4
5
6
7
8
9
10
11
12
D:\wolf\Documents\teensy\demo_make>ls -R
.:
a.exe  lib  src

./lib:
file2.cpp  file2.cpp~  file2.h  file2.h~

./src:
Makefile         Makefile~  file1.cpp~  file1.h~  main.cpp~  obj
Makefile - Copy  file1.cpp  file1.h     main.cpp  main.exe

./src/obj:
1
2
//g++ -c obj/file1.o file1.cpp
g++ -c file1.cpp -o obj/file1.o
You have your compile rule incorrect or the wrong way round for the compiler.
Change each:
g++ -c $@ $<
to:
g++ -c $< -o $@

The way you have it, you're asking the compiler to compile the object file and the source file.

The meaning of the automatic variables $@, $< etc are explained here:
http://www.gnu.org/software/make/manual/html_node/Automatic-Variables.html
Last edited on
Thank you ne555 and ShodanHo.

That worked, and I learned about the -o option:

3.2 Options Controlling the Kind of Output
-o file
Place output in file file.
http://gcc.gnu.org/onlinedocs/gcc-4.3.3/gcc/Overall-Options.html#Overall-Options
Topic archived. No new replies allowed.