Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
797 views
in Technique[技术] by (71.8m points)

makefile - C++ - Compiling multiple files

I was learning about the makefile tool which seems very powerful, and was trying to figure out how to make it work since I have 4 different mains and a common class for 3 of them.

What I'd like to get is:

Linear: g++ -o linear linear.cpp

Linear5: g++ -o linear5 linear5.cpp Contenidor.cpp Contenidor.h

Logarithmic: g++ -o logarithmic logarithmic.cpp Contenidor.cpp Contenidor.h

Constant: g++ -o constant constant.cpp Contenidor.cpp Contenidor.h

With the following Makefile code:

all: linear5 linear logarithmic constant

linear5: linear5.o
    g++ -o linear5 linear5.o

linear5.o: linear5.cpp
    g++ -cpp linear5.cpp

Contenidor.o: Contenidor.cpp
    g++ -cpp Contenidor.cpp

linear: linear.o Contenidor.o
    g++ -o linear linear.o Contenidor.o

linear.o: linear.cpp
    g++ -cpp linear.cpp

logarithmic: logarithmic.o Contenidor.o
    g++ -o logarithmic logarithmic.o Contenidor.o

logarithmic.o: logarithmic.cpp
    g++ -cpp logarithmic.cpp

constant: constant.o Contenidor.o
    g++ -std=gnu++0x -o constant constant.o Contenidor.o

constant.o: constant.cpp
    g++ -cpp constant.cpp

clean:
    rm *.o

But an error pops out when I try execute the make command:

g++ -cpp linear5.cpp
g++ -o linear5 linear5.o
g++: linear5.o: No such file or directory
g++: no input files
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The problem is in the way you perform two-step compilation: you should change every instance of

file.o: file.cpp
    g++ -cpp file.cpp

into:

file.o: file.cpp
    g++ -c -o file.o file.cpp

This way, you tell g++ to just compile (-c) and not link your file; the output will be an object file, you still have to specify its name with -o though.

Then, the object file can be used in later steps.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...