有没有办法为 c++ 制作一个 makefile,每次使用 make 命令时都会运行该程序?
Is there a way to make a makefile for c++ that will run the program every time you use the make command?
我有一个C++项目的makefile,它编译并创建该程序的可执行文件,然后运行该可执行文件。问题是,我希望每次使用 make 命令时都运行可执行文件,但如果对源代码没有需要重新编译的更改,make 将返回"make:无事可做"。有没有办法格式化makefile,以便在需要编译时编译并运行,否则只需运行可执行文件?
您可以简单地添加一个新目标来运行程序,并使该目标依赖于程序的创建。如果将该目标设置为默认目标 (all:( 的依赖项,它将始终在成功构建后运行程序。
喜欢这个:
SOURCES = ./src/main.cpp
OBJECTS = ./bin/main.o
# default target to build hello executable & run it if successful
all: hello run
hello: $(OBJECTS)
$(CXX) -o $@ $^
bin/%.o : src/%.cpp
$(CXX) -c $(CPPFLAGS) $(CXXFLAGS) -o $@ $< $(LDFLAGS)
run: hello
./hello
clean:
@rm -rf hello $(OBJECTS)
.PHONY: all run clean
注意:目标名称"run"没有什么特别之处。我本可以称之为"wibble"。
相关文章:
- VS Code "command":"make"与终端窗口中的命令行"make"不同
- make 命令如何避免重新编译未更改的源文件?
- make 命令创建 .file,但不创建应用程序文件
- 有没有办法为 c++ 制作一个 makefile,每次使用 make 命令时都会运行该程序?
- "无法确定要运行哪个"make"命令.检查构建配置中的"make"步骤
- 修复了安装 OSRM 时使用"make"命令时的构建问题
- 仅禁止显示实际的 make 命令和最终打印语句
- 如何在 kali Linux 的 make 命令中启用 c++11
- 使用 make 命令编译类文件
- make 命令给出不兼容的 i386 架构(i386x86-64)
- 是否可以使用C++中的一个make命令,用两个不同的gcc版本编译两个不同可执行文件
- 在 C++ 中使用 -s for make 命令时不会显示哪些消息?
- 基于使用 make 命令或生成文件运行预处理器
- MacOSx-使用make命令编译C项目
- 使用 MinGW 和 sip 安装 pyqt5。"make"命令在哪里?
- CMake:如何使用make命令创建文件
- 有没有办法在make命令中找到未使用的定义和结构
- 如何使用特定的gcc编译器来运行make命令
- 在ubuntu中使用make命令重新编译时出现问题
- 无法找到Make:命令