Makefile creation problem

I am having trouble generating the Makefile. Write now, I collect everything all the time. Although the professor is fine with this, I want it to run faster and to avoid unnecessary compilation.

Here's what I have.

FILES= p6.cpp SetIter.cpp Node.cpp Set.cpp
CFLAGS= -ansi -pendantic -Wall -Wextra
CC= g++

MakePg6: p6.cpp SetIter.cpp Node.cpp Set.cpp
   $(CC) $(CFLAGS) $(FILES) -o pg6

      

Node.cpp - node class

Set.cpp - uses nodes. Friend of Node.

SetIter.cpp - gets a set and uses a pointer to an iterator via

I am confused with some of the dependencies coming from the friends thing and the point lib.o is included in the Makefile like some sites do.

Any help is appreciated. Thanks in advance.

+2


a source to share


3 answers


You can simplify the creation of the file, but using $(FILES)

depending on MakePg6, this way:

MakePg6: $(FILES)
   $(CC) $(CFLAGS) $(FILES) -o pg6

      



The friendly part is allowed by every C ++ compiled independently, and the header files define all the information needed to compile that one file. The compiler applies the friends' rules. The compiler concatenates the resulting object together to make the resulting executable file.

It is included in lib.o because people are using the standard code provided by the base library. If you are missing, you will receive a communication error message.

+1


a source


How about separating the creation of the files so that they only rebuild when needed.

OBJECTS= p6.o SetIter.o Set.o Node.o

.cpp.o: $*.cpp
     $(CC) $(CFLAGS) -cpp $*.cpp
p6.o: p6.cpp SetIter.cpp // any file that it depends on
SetIter.o: SetIter.cpp Set.cpp //etc
Set.o: Set.cpp Node.cpp
Node.o: Node.cpp
p6: $(OBJECTS)
    $(CC) $(CFLAGS) $(OBJECTS) -o
MakePg6: p6

      



That being said, it will only create each object file if the .o file is older than the .cpp file and I think you are trying to execute.

+1


a source


If your Makefile works as written, then you must properly configure your header files (eg Node.h). This way, different files can be compiled separately and linked, but whether you do it or not, you have to consider the dependencies on the header files.

OBJECTS= p6.o SetIter.o Node.o Set.o
CFLAGS= -ansi -pendantic -Wall -Wextra
CC= g++

# This will compile any object file (something.o) that is called for
# Note that I am using automatic variables-- "$<" means the first prerequisite
# (the .cpp file), "$^" means all the prerequisites, and "$@" means the target.
%.o: %.cpp
    $(CC) $(CFLAGS) -c $< -o $@

# If A.cpp (or A.h) includes B.h, then if you change B.h you must recompile A.o,
# So A.o depends on B.h.
# I will make some educated guesses about how you include header files:

SetIter.o Node.o Set.o: Node.h
Set.o: Set.h
SetIter.o p6.o: Set.h SetIter.h

# This will link the object files together to make pg6.
# Notice that the target is the name of thing actually made, a good practice.
pg6: $(OBJECTS)
    $(CC) $(CFLAGS) $^ -o $@

      

You should solve these .h dependencies by looking at the code. There are ways to have g ++ and do it automatically, but this is an advanced technique.

0


a source







All Articles