c++ - 在 C++ 中使用 -o 和 -c 的 Makefile 语法

标签 c++ makefile

我已经有一段时间没有制作 makefile 了。我继承了一些在 makefile 中使用以下行构建的代码

$(CC) $(FLAGS) -c -o $*.o $*.cpp

为什么要在同一行中使用-c-o-c 不是让您无需链接即可构建对象吗?

编辑 这是完整的 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

$*.cpp 不应该找到我当前路径中的所有 .cpp 文件吗(它们就在那里)

最佳答案

如您所说,-c 表示无需链接即可生成目标文件。

-o 表示您要覆盖默认输出文件名并指定您自己的文件名。所以 -o $.o 意味着输出文件名将与输入文件名相同,但末尾带有 .o。

如果您计划让此 Makefile 规则可用于许多不同的编译器,您可能会这样做,其中一些编译器可能具有不同的目标文件默认输出文件名。

gcc 的手册页这样描述 -o:

-o file

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. 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.

关于c++ - 在 C++ 中使用 -o 和 -c 的 Makefile 语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24987328/

相关文章:

c++ - 存储内部排序的 map 元素的最快方法

shell - 当子命令抛出时终止 makefile 命令?

c++ - 主 Makefile 中 undefined reference

makefile - makefile 中的命令前面有反斜杠

c++ - 我使用什么函数来在 cin 语句中存储单个字符,以便我可以单独处理每个字符?

c++ - 关于无锁编程的一些疑惑

c++ - 谷歌 SketchUp API

c++ - 无法进行嵌套循环

compilation - 链接 lapack 问题

c++ - 如何使用 GNU Make 为每个目标运行前后配方?