Reputation: 61
I'm trying to make a makefile for a simple calculator for a college project. I need it done, and I've searched the web for tutorials and I eventually found this code:
IDIR =include
CC=gcc
CFLAGS=-I$(IDIR)
ODIR=obj
LDIR=lib
SDIR=src
LIBS=-lm
_DEPS = calc.h
DEPS = $(patsubst %,$(IDIR)/%,$(_DEPS))
_OBJ = calc.o libcalc.o
OBJ = $(patsubst %,$(ODIR)/%,$(_OBJ))
$(ODIR)/%.o: $(SDIR)/%.c $(DEPS)
$(CC) -c -o $@ $< $(CFLAGS)
calc: $(OBJ)
gcc -o $@ $^ $(CFLAGS) $(LIBS)
.PHONY: clean
clean:
rm -f $(ODIR)/*.o *~ core $(INCDIR)/*~
The output of the make command is
gcc -c -o obj/calc.o calc.c -I../include
Assembler messages:
Fatal error: can't create obj/calc.o: No such file or directory
make: *** [obj/calc.o] Error 1
My professor gave us no lead nor instruction to this, so I'm really lost.
My project is divided in three folders: lib
, src
and include
. src
contains the source for the project, named calc.c
, include
contains the header named calc.h
, and lib
contains the library named libcalc.c
.
EDIT: I figured out this error, and it was that the obj
directory under src
wasn't created. However, it's still not working. The new error message is:
make: *** No rule to make target `obj/libcalc.o', needed by `calc'. Stop.
EDIT 2: I just moved the libcalc.c from lib to obj and it worked fine. Not ideal, but it does what I need.
Upvotes: 5
Views: 26904
Reputation: 435
The obj
directory doesn't exist, so gcc
cannot write the output file there.
You could create it automatically from the Makefile, but depending on what you want to do, a simple mkdir obj
might be all you need.
However, it looks like you are running your Makefile from the src
directory. Chances are you want to run it from your project's root directory and specify src/
where needed. You'd want to add an SDIR
variable (or similar) for that; make sure to remove the ../
on LDIR
, IDIR
and ODIR
in that case.
For example, where you currently use %.c
, use $(SDIR)/%.c
instead, and move your Makefile appropriately.
Upvotes: 8
Reputation: 6333
according to your cli command, you are running your makefile under src/
, which is fine, but gcc won't create missing folder for you. here is the code runs in my linux:
$ tree
.
└── test2.c
0 directories, 1 file
$ gcc -c -o obj/test2.o test2.c
Assembler messages:
Fatal error: can't create obj/test2.o: No such file or directory
$ mkdir obj
$ gcc -c -o obj/test2.o test2.c
$ tree
.
├── obj
│ └── test2.o
└── test2.c
1 directory, 2 files
Upvotes: 0