[英]Makefile syntax using -o and -c in c++
It's been a while since I've done a makefile. 自从我制作了一个makefile以来已经有一段时间了。 I have inherited some code that is built with the following line in a makefile 我继承了在makefile中使用以下行构建的一些代码
$(CC) $(FLAGS) -c -o $*.o $*.cpp
Why would you use -c
and -o
in the same line. 为什么要在同一行中使用-c
和-o
。 Doesn't the -c
make it so that you build the objects without linking? -c
不能使您无需链接即可构建对象吗?
EDIT Here is the complete makefile, now I get an error saying cpp.cpp No such file or directory
编辑这是完整的makefile,现在我得到一个错误,说cpp.cpp No such file or directory
.SUFFIXES: .o .cpp
CC=g++
MAIN_OBJS = \
main.o \
f1.o \
f2.o \
all:
$(CC) -c -o $*.o $*.cpp
$(CC) $(MAIN_OBJS) -o final
Shouldn't the $*.cpp
find all the .cpp files in my current path (and they are there) $*.cpp
应该不应该找到我当前路径中的所有.cpp文件(它们在那里)
As you say, -c
means make object files without linking. 如您所说, -c
表示不链接而生成目标文件。
-o
means you want to override the default output file name and specify your own. -o
表示您要覆盖默认输出文件名并指定自己的文件名。 So -o $.o
means the output file name would be the same as the input file name but with .o on the end. 所以-o $.o
表示输出文件名将与输入文件名相同,但末尾带有.o。
You might do this if you were planning to have this Makefile rule usable with a number of different compilers, some of which might have a different default output file name for object files. 如果您打算让此Makefile规则可用于许多不同的编译器,则可以执行此操作,其中某些编译器可能具有与目标文件不同的默认输出文件名。
The man page for gcc describes -o
like this: gcc的手册页描述了-o
如下所示:
-o file -o文件
Place output in file file. 将输出放置在文件文件中。 This applies regardless to whatever sort of output is being produced , whether it be an executable file, an object file, an assembler file or preprocessed C code. 这适用于无论生成何种输出 ,无论是可执行文件,目标文件,汇编文件还是预处理的C代码。 If -o is not specified, the default is to put an executable file in a.out, the object file for source.suffix in source.o, its assembler file in source.s, a precompiled header file in source.suffix.gch, and all preprocessed C source on standard output. 如果未指定-o,则默认值为将可执行文件放入a.out中,将source.suffix的目标文件放入source.o中,将其汇编文件放入source.s中,将预编译的头文件放入source.suffix.gch中,以及所有标准输出上的预处理C源代码。
Yes, what you said makes sense. 是的,你说的很有道理。
As we know, -c will compile the source files and produce the assembly files. 众所周知, -c将编译源文件并生成程序集文件。
After that, we normally use -o to link all those assembly files and produce a executable file. 之后,我们通常使用-o链接所有这些程序集文件并生成一个可执行文件。
What follows -o is the name of the output executable file. -o之后是输出可执行文件的名称。
And if the -c and -o are used in the same line, it means the output assembly file produced by -c is named what follows -o . 而且,如果-c和-o在同一行中使用,则意味着-c生成的输出程序集文件的名称与-o相同 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.