簡體   English   中英

C ++ - 編譯多個文件

[英]C++ - Compiling multiple files

我正在學習makefile工具,它似乎非常強大,並試圖找出如何使它工作,因為我有4個不同的主電源和3個共同的類。

我想得到的是:

線性: 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 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

問題在於您執行兩步編譯的方式:您應該更改每個實例

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

成:

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

這樣,你告訴g ++只是編譯( -c )而不是鏈接你的文件; 輸出將是一個目標文件,您仍然必須使用-o指定其名稱。

然后,目標文件可以在以后的步驟中使用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM