C ++-编译多个文件 [英] C++ - Compiling multiple files
问题描述
我正在学习看起来很强大的makefile工具,并且由于我有4个不同的mains和其中3个mains的通用类而试图弄清楚如何使其工作.
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.
我想要得到的是:
线性: g ++ -o linear linear.cpp
Linear5: g ++ -o linear5 linear5.cpp Contenidor.cpp Contenidor.h
对数: g ++ -o logarithmic logarithmic.cpp Contenidor.cpp Contenidor.h
常量: g ++ -o常量constant.cpp Contenidor.cpp Contenidor.h
使用以下Makefile代码:
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
但是当我尝试执行make命令时会弹出错误消息:
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
推荐答案
问题在于执行两步编译的方式:您应该更改的每个实例
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
进入:
file.o: file.cpp
g++ -c -o file.o file.cpp
这样,您告诉g ++仅编译( -c
)而不链接文件;输出将是一个目标文件,尽管如此,您仍然必须使用 -o
指定其名称.
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.
这篇关于C ++-编译多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!