简体   繁体   English

编译.o和.exe的C ++ makefile

[英]C++ makefile that compiles both .o and .exe

My professor didn't give us any information and I cant seem to find a solution with resources online. 我的教授没有给我们任何信息,我似乎也找不到在线资源的解决方案。

We need to create a makefile for a .cpp file. 我们需要为.cpp文件创建一个makefile。 It should compile a .o file, and an executable file. 它应该编译一个.o文件和一个可执行文件。 Also a command clean that removes both of these files. 同样,执行命令清除会删除这两个文件。 This is what I currently have, any help? 这是我目前所拥有的,有什么帮助吗?

List: List.cpp
    g++ List.cpp -o List

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

clean:
    rm List
    rm List.o

You're doing more work than you need to there. 您要做的工作比您要做的要多。

Usually the executable file is generated from the .o file. 通常, 可执行文件是从.o文件生成的。

1) Compile the .cpp file to make an .o file. 1) 编译 .cpp文件以创建.o文件。

2) Link the .o file to make an executable file. 2) 链接 .o文件以创建可执行文件。

# List.o depends on List.cpp
# If List.cpp changes, recompile List.o
List.o: List.cpp
    g++ -c -o List.o List.cpp

# List depends on List.o
# If List.o changes, relink List
List: List.o
    g++ -o List List.o

clean:
    rm List
    rm List.o

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

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