I am trying to compile the code structure with src and test folders. The aim is to have main application as well as test application. However, I am facing a problem when I create main.o inside $(OBJDIR). main.o inside src folder works fine. What is the difference between two? I am trying to understand what is wrong in my Makefile.
SRCDIR = src
TESTDIR = test
OBJDIR = obj
LIBDIR = lib
CXXFLAGS = -std=c++11 -fPIC -g
LDFLAGS = -shared
# Get Only the Internal Structure of Directories from SRCDIR
STRUCTURE := $(shell find $(SRCDIR) -type d)
#Filter-out hidden directories
STRUCTURE := $(filter-out $(shell find $(SRCDIR)/.* -type d),$(STRUCTURE))
# Get All Files From STRUCTURE
CODEFILES := $(addsuffix /*,$(STRUCTURE))
CODEFILES := $(wildcard $(CODEFILES))
TESTCODEFILES := $(wildcard $(TESTDIR)/*.cpp)
## Filter Only Specific Files
SRCFILES := $(filter %.cpp,$(CODEFILES))
HDRFILES := $(filter %.h,$(CODEFILES))
OBJFILES := $(subst $(SRCDIR),$(OBJDIR),$(SRCFILES:%.cpp=%.o))
# Filter Out Function main for Libraries
LIBDEPS := $(filter-out $(OBJDIR)/main.o,$(OBJFILES))
TESTOBJFILES := $(TESTCODEFILES:%.cpp=%.o)
all: libeasy_math.so easy_math_test main
libeasy_math.so: $(LIBDEPS)
$(CXX) $(CXXFLAGS) $(LIBDEPS) -o $@ $(LDFLAGS)
easy_math_test: $(TESTOBJFILES)
$(CXX) $(CXXFLAGS) $(TESTOBJFILES) -o $@ -L./ -leasy_math -lgtest -lgtest_main
main: $(OBJFILES)
$(CXX) $(CXXFLAGS) -o $@ $(OBJFILES)
$(OBJDIR)/%.o: $(addprefix $(SRCDIR)/,%.cpp %.h)
@echo " Compiling: $<"
$(CXX) $(CXXFLAGS) -c $< -o $@
clean:
-rm -r $(OBJDIR)/*
-rm -r main
-rm -r libeasy_math.so
-rm -r easy_math_test
output:
Compiling: src/easy_math.cpp
c++ -std=c++11 -fPIC -g -c src/easy_math.cpp -o obj/easy_math.o
c++ -std=c++11 -fPIC -g obj/easy_math.o -o libeasy_math.so -shared
c++ -std=c++11 -fPIC -g test/test.o -o easy_math_test -L./ -leasy_math -lgtest -lgtest_main
make: *** No rule to make target `obj/main.o', needed by `main'. Stop
but when written like this main application is getting created
SRCDIR = src
TESTDIR = test
OBJDIR = obj
LIBDIR = lib
CXXFLAGS = -std=c++11 -fPIC -g
LDFLAGS = -shared
# Get Only the Internal Structure of Directories from SRCDIR
STRUCTURE := $(shell find $(SRCDIR) -type d)
#Filter-out hidden directories
STRUCTURE := $(filter-out $(shell find $(SRCDIR)/.* -type d),$(STRUCTURE))
# Get All Files From STRUCTURE
CODEFILES := $(addsuffix /*,$(STRUCTURE))
CODEFILES := $(wildcard $(CODEFILES))
TESTCODEFILES := $(wildcard $(TESTDIR)/*.cpp)
## Filter Only Specific Files
SRCFILES := $(filter %.cpp,$(CODEFILES))
HDRFILES := $(filter %.h,$(CODEFILES))
OBJFILES := $(SRCFILES:%.cpp=%.o)
# Filter Out Function main for Libraries
LIBDEPS := $(filter-out $(SRCDIR)/main.o,$(OBJFILES))
TESTOBJFILES := $(TESTCODEFILES:%.cpp=%.o)
all: libeasy_math.so easy_math_test main
libeasy_math.so: $(LIBDEPS)
$(CXX) $(CXXFLAGS) $(LIBDEPS) -o $@ $(LDFLAGS)
easy_math_test: $(TESTOBJFILES)
$(CXX) $(CXXFLAGS) $(TESTOBJFILES) -o $@ -L./ -leasy_math -lgtest -lgtest_main
main: $(OBJFILES)
$(CXX) $(CXXFLAGS) -o main $(OBJFILES)
%.o: $(addprefix $(SRCDIR)/,%.cpp %.h)
$(CXX) $(CXXFLAGS) -c $< -o $@
clean:
-rm -r $(OBJDIR)/*
-rm -r main
-rm -r libeasy_math.so
-rm -r easy_math_test
output:
c++ -std=c++11 -fPIC -g -c -o src/easy_math.o src/easy_math.cpp
c++ -std=c++11 -fPIC -g src/easy_math.o -o libeasy_math.so -shared
c++ -std=c++11 -fPIC -g -c -o test/test.o test/test.cpp
c++ -std=c++11 -fPIC -g test/test.o -o easy_math_test -L./ -leasy_math -lgtest -lgtest_main
c++ -std=c++11 -fPIC -g -c -o src/main.o src/main.cpp
c++ -std=c++11 -fPIC -g -o main src/easy_math.o src/main.o
Aucun commentaire:
Enregistrer un commentaire