Im using this code in the makefile, all is going good but the last line. My code folder looks like this:
Project/bin -> For executable files
Project/build -> For .o files
Project/include -> For .hpp files
Project/src -> For .cpp files
Makefile path: Project/Makefile
# Compiler #
CC = g++
DEBUG = -g
LFLAGS =
CFLAGS = -Wall
# Directories #
SRCDIR = src/
INCDIR = include/
BUILDDIR = build/
BINDIR = bin/
# Objects #
OBJ_NAMES = main.o dfa.o dfaException.o state.o
OBJS = $(addprefix $(BUILDDIR), $(OBJ_NAMES))
# Output #
TARGET = $(BINDIR)pract3
$(TARGET): $(OBJS)
$(CC) $(CCFLAGS) $(LFLAGS) $(OBJS) -o $(TARGET)
$(BUILDDIR)%.o: $(SRCDIR)%.cpp
$(CC) $(CCFLAGS) $(LFLAGS) -c $< -o $(BUILDDIR)$($(notdir $<):.cpp=.o)
The problem is in "$(BUILDDIR)$($(notdir $<):.cpp=.o)
". What Im trying to do here is:
- $< contains "
src/mysrc.cpp
" so with$(notdir $<)
I get "mysrc.cpp
" Now, I want to change the file extension to
.o
, so I use$($(notdir $<):.cpp=.o)
And I should get "mysrc.o
". But this part is not working:g++ -c src/main.cpp -o build/
Assembler messages:
Fatal error: can't create build/: Permission denied
Makefile:25: recipe for target 'build/main.o' failed
make: *** [build/main.o] Error 1
I use
$(BUILDDIR)
To get "build/mysrc.o
"
Why does $($(notdir $<):.cpp=.o)
somehow delete the file name ?
And now that Im here. I've learned to use make some days ago. Is there something that I should improve here ?