简体   繁体   English

C ++ - 编译多个文件

[英]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. 我正在学习makefile工具,它似乎非常强大,并试图找出如何使它工作,因为我有4个不同的主电源和3个共同的类。

What I'd like to get is: 我想得到的是:

Linear: g++ -o linear linear.cpp 线性: g++ -o linear linear.cpp

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

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

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

With the following Makefile code: 使用以下Makefile代码:

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: 但是当我尝试执行make命令时会弹出一个错误:

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

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; 这样,你告诉g ++只是编译( -c )而不是链接你的文件; the output will be an object file, you still have to specify its name with -o though. 输出将是一个目标文件,您仍然必须使用-o指定其名称。

Then, the object file can be used in later steps. 然后,目标文件可以在以后的步骤中使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM