学习makefile [英] Learning makefiles
问题描述
我正在尝试学习Makefile.我必须为我的Unix管理/编程类做一个分配,该分配涉及为三个文件创建一个makefile:Savings.h Savings.cpp和SavingsTest.cpp.我的老师对Makefile的解释并不多.我看了我的书后,就了解了总体思路,但是语法上却遇到了麻烦.我按照我的书中的示例进行操作,但出现错误.到目前为止,这是我的makefile代码:
I am trying to learn makefiles. I have to do an assignment for my Unix admin/programming class that involves creating a makefile for three files: savings.h savings.cpp and savingsTest.cpp. My teacher didn't really explain much to us about makefiles. I looked in my book and I understand the general idea but I am having trouble with the syntax. I followed the example my book has but I get an error. Here is my code for the makefile so far:
savingsTest.0: savingsTest.cpp savings.cpp savings.h
cc -c savingsTest.cpp
savings.o: savings.cpp savings.h
cc -c savings.cpp
这是我得到的错误:
cc -c savingsTest.cpp
cc: No input file specified, no output generated
*** Error code 1
make: Fatal error: Command failed for target `savingsTest'
v245-2% emacs makefile.txt
有人可以帮助我理解语法,以便确定我做错了什么.谢谢任何响应我们的人.
Can somebody please help me understand the syntax so I can determine what I am doing wrong. Thank you to anybody that responds to help.
推荐答案
您需要整理出C ++编译器的名称.我要假设它是CC
,但是还有很多其他名称(例如g++
).
You need to sort out the name of your C++ compiler. I'm about to assume it is CC
, but there are lots of other names (g++
for instance).
您的程序savingsTest.0
依赖于两个目标文件:savings.o
和savingsTest.o
. savings.o
文件取决于savings.cpp
和savings.h
. savingsTest.o
文件取决于savingsTest.cpp
和savings.h
.
Your program savingsTest.0
depends on two object files: savings.o
and savingsTest.o
. The savings.o
file depends on savings.cpp
and savings.h
. The savingsTest.o
file depends on savingsTest.cpp
and savings.h
.
全部写为:
savingsTest.0: savings.o savingsTest.o
CC -o $@ savings.o savingsTest.o
savingsTest.o: savingsTest.cpp savings.h
CC -c savingsTest.cpp
savings.o: savings.cpp savings.h
CC -c savings.cpp
您可能会省略最后两个命令行; make
知道如何将.cpp
编译为.o
.
You could probably leave out the last two command lines; make
knows how to compile a .cpp
to .o
.
您将学习如何在makefile
中使用宏. $@
是要构建的程序或文件的名称(因此它是savingsTest.0
的简写).有理由使用它-尤其是,不要重复自己.
You'll learn to use macros in your makefile
. $@
is the name of the program or file being built (so it is a shorthand for savingsTest.0
). There are reasons to use it — notably, don't repeat yourself.
通常应将宏用于命令和命令参数,库以及文件列表.
You should normally use macros for commands and command arguments, for libraries, and for lists of files.
这篇关于学习makefile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!