Reputation: 14670
I am dumping all the object files of my compilation into a separate directory called
objfiles
While compiling I want to check for the existence of the directory objfiles and if it does not exist (indicating re-compilation of the whole project) I want to create it.
Here is my Makefile and an attempt at the problem
SHELL := /bin/zsh
#All object files are dumped into OBJDIR
OBJDIR=objfiles
OBJECTS=$(OBJDIR)/main.o \
$(OBJDIR)/printdata.o \
#...
#...
COMPILER=nvcc -arch=sm_20
exec: objdir_exist $(OBJECTS)
$(COMPILER) $(OBJECTS) -o exec
@printf "\nExecutable updated!\n\n"
$(OBJDIR)/main.o: main.cu octree.h
$(COMPILER) -c $< -o $@
$(OBJDIR)/printdata.o: printdata.cu
$(COMPILER) -c $< -o $@
...
#Clean-up executables and object files
.PHONY=clean objdir_exist
clean:
rm exec
rm -r $(OBJDIR)
# Check for the existence of object files.
# If it does not exist, then create another object_file directory to dump data to.
# If it exists do nothing
objdir_exist:
if [ ! -d "$(OBJDIR)" ] then mkdir $(OBJDIR) fi
I get the following error.
if [ ! -d "objfiles" ] then mkdir objfiles fi
zsh:[:1: ']' expected
nvcc -arch=sm_20 -c main.cu -o objfiles/main.o
Assembler messages:
Fatal error: can't create objfiles/main.o: No such file or directory
make: *** [objfiles/main.o] Error 1
Where am I going wrong?
Upvotes: 0
Views: 7084
Reputation: 454
The solution I found goes something like this
BIN=exec
OBJSDIR=objs
all: $(OBJSDIR) $(BIN)
$(OBJSDIR):
if [ ! -d $(OBJSDIR) ]; then mkdir $(OBJSDIR); fi
$(OBJSDIR)/obj1.o: obj1.c header1.h
# $< expands only to first prerequisite
$(COMPILER) -c $< -o $@
$(OBJSDIR)/obj2.o: obj2.c header2.h
$(COMPILER) -c $< -o $@
$(BIN): $(OBJSDIR)/obj1.o $(OBJSDIR)/obj2.o
# $^ expands to all prerequisites
$(COMPILER) $^ -o $@
all depends on both $(OBJSDIR) and $(BIN). If $(OBJSDIR) doesn't exist, it creates it. Then it goes on to check $(BIN) and builds that if it doesn't exist or its prerequisites have changed.
Upvotes: 0
Reputation: 15214
The problem is because when you write if
in one line you should use ;
as line-delimiter.
So, replace
if [ ! -d "$(OBJDIR)" ] then mkdir $(OBJDIR) fi
by
if [ ! -d "$(OBJDIR)" ]; then mkdir $(OBJDIR); fi
Upvotes: 5