Reputation: 909
This is the first makefile of my life! I have a project in which I have the src folder (in which I keep my .cpp files), an include folder (in which I keep my .hpp files) and a build folder in which I would like to store my object files.
# define the C compiler to use
CCXX = g++ -std=c++11
# define any compile-time flags
CXXFLAGS = -g -Wall
# define any directories containing header files other than /usr/include
INCLUDES = -I./include
#define the directory for src files
SRCDIR = ./src/
#define the directive for object files
OBJDIR = ./build/
# define the C source files
SRCS = action.cpp conditionedBT.cpp control_flow_node.cpp execution_node.cpp main.cpp
# define the C object files
OBJS = $(OBJDIR)$(SRCS:.cpp=.o)
# define the executable file
MAIN = out
.PHONY: depend
all: $(MAIN)
@echo Program compiled
$(MAIN): $(OBJS)
$(CCXX) $(CXXFLAGS) $(INCLUDES) -o $(MAIN) $(OBJS)
$(OBJDIR)/%.o: ($SRCDIR)/%.c
$(CCXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $<
#.c.o:
# $(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@
#clean:
# $(RM) *.o *~ $(MAIN)
depend: $(addprefix $(SRCDIR),$(SRCS))
makedepend $(INCLUDES) $^
# DO NOT DELETE THIS LINE -- make depend needs it
Given the above, I get the following error when I try to execute make:
make: *** No rule to make target `build/action.o', needed by `out'. Stop.
Upvotes: 2
Views: 5795
Reputation: 48605
There are a few problems with your Makefile
.
1) You are using the .c
extension when your files use .cpp
.
2) Your substitution directive OBJS = $(SRCS:.c=.o)
doesn't take into account the subdirectories of your sources and objects.
3) Your general rule to make objects is not getting invoked for those reasons but also because you don't specify the subdirectory of the sources.
Because of that make
is making up its own rules to compile your object and ignoring the rule you made.
Also I would recommend using the correct implicit variables for C++
which would make the implicit rules work better.
They are detailed here: https://www.gnu.org/software/make/manual/html_node/Implicit-Variables.html
So I would recommend something more like this:
# define the C compiler to use
CXX = g++
# define any compile-time flags
CXXFLAGS = -std=c++11 -g -Wall
# define any directories containing header files other than /usr/include
CPPFLAGS = -I./include
#define the directive for object files
OBJDIR = ./build
SRCDIR = ./src
# define the C source files
SRCS = $(SRCDIR)/action.cpp $(SRCDIR)/main.cpp
# define the C object files
OBJS = $(patsubst $(SRCDIR)/%.cpp,$(OBJDIR)/%.o,$(SRCS))
# define the executable file
MAIN = out
.PHONY: depend
all: $(MAIN)
@echo Program compiled
$(MAIN): $(OBJS)
$(CXX) $(CXXFLAGS) $(CPPFLAGS) -o $(MAIN) $(OBJS)
$(OBJDIR)/%.o: $(SRCDIR)/%.cpp
@echo "Compiling: " $@
$(CXX) $(CXXFLAGS) $(CPPFLAGS) -c -o $@ $<
clean:
$(RM) $(OBJDIR)/*.o *~ $(MAIN)
depend: $(SRCS)
makedepend $(INCLUDES) $^
# DO NOT DELETE THIS LINE -- make depend needs it
Upvotes: 6