I have a problem with Makefiles, i understand the point behind them but they are
ID: 3790240 • Letter: I
Question
I have a problem with Makefiles, i understand the point behind them but they are very hard to understand -
the programming in itself is not difficut, but making them work in Linux through makefiles is extremely difficult
Can somebody please explain this to me? and please input the changes!
Thankyou very much in advance
SOURCES=main.cpp part1.cpp part2.cpp
OBJECTS=$(SOURCES :.cpp=.o)
DEPS=$(SOURCES :.cpp=.d)
EXE=prog
CXXFLAGS=-I.
# Making for host
# > make ARCH=host
ifeq (${ARCH},host)
CXX=g++
BUILD_DIR=build/host
endif
# Making for target
# > make ARCH=target
ifeq (${ARCH},target)
CXX=arm -devkit -g++
BUILD_DIR=build/target
endif
$(EXE): $(DEPS) $(OBJECTS) # << Check the $(DEPS) new dependency
$(CXX) $(CXXFLAGS) -o $@ $(OBJECTS)
# Rule that describes how a .d (dependency) file is created from a .cpp file
# Similar to the assigment that you just completed %.cpp -> %.o
${BUILD_DIR }/%.d: %.cpp
$(CXX) -MT${BUILD_DIR }/$(@:.d=.o) -MM $(CXXFLAGS) $^ > $@
ifneq ($(MAKECMDGOALS),clean)
-include $(DEPS)
endif
SOURCES=main.cpp part1.cpp part2.cpp
OBJECTS=$(SOURCES :.cpp=.o)
DEPS=$(SOURCES :.cpp=.d)
EXE=prog
CXXFLAGS=-I.
# Making for host
# > make ARCH=host
ifeq (${ARCH},host)
CXX=g++
BUILD_DIR=build/host
endif
# Making for target
# > make ARCH=target
ifeq (${ARCH},target)
CXX=arm -devkit -g++
BUILD_DIR=build/target
endif
$(EXE): $(DEPS) $(OBJECTS) # << Check the $(DEPS) new dependency
$(CXX) $(CXXFLAGS) -o $@ $(OBJECTS)
# Rule that describes how a .d (dependency) file is created from a .cpp file
# Similar to the assigment that you just completed %.cpp -> %.o
${BUILD_DIR }/%.d: %.cpp
$(CXX) -MT${BUILD_DIR }/$(@:.d=.o) -MM $(CXXFLAGS) $^ > $@
ifneq ($(MAKECMDGOALS),clean)
-include $(DEPS)
endif
Explanation / Answer
Makefiles are a simple way to organize code compilation.
gcc -o (filename) -I
(The -I. is included so that gcc will look in the current directory for your file)
This code will compile the files
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.