【问题标题】:C++ - Compiling multiple filesC++ - 编译多个文件
【发布时间】:2016-12-09 18:11:12
【问题描述】:

我正在学习看起来非常强大的 makefile 工具,并试图弄清楚如何使它工作,因为我有 4 个不同的 main 和一个用于其中 3 个的通用类。

我想得到的是:

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

线性5:g++ -o linear5 linear5.cpp Contenidor.cpp Contenidor.h

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

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

使用以下 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

但是当我尝试执行make命令时会弹出一个错误:

g++ -cpp linear5.cpp
g++ -o linear5 linear5.o
g++: linear5.o: No such file or directory
g++: no input files

【问题讨论】:

    标签: c++ makefile gnu


    【解决方案1】:

    问题在于您执行两步编译的方式:您应该更改每个实例

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

    进入:

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

    这样,你告诉 g++ 只编译 (-c) 而不是链接你的文件;输出将是一个目标文件,但您仍然必须使用 -o 指定其名称。

    然后,目标文件可以在后面的步骤中使用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-29
      • 2011-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多