Makefile自动删除.o文件

makefile auto removing .o files

本文关键字:文件 删除 Makefile      更新时间:2023-10-16

我在大学里修了一门c++课程,他们想让我们手动输入所有的测试文件…但是,我知道有一种方法可以做到这一点,这就是我最终使用当前(http://pastebin.com/6d9UtKM4) makefile的原因。我的问题是,为什么这个makefile自动删除它用于编译时使用的所有.o文件?我不会死,但我想保留。o文件。我在这里粘贴了makefile (http://pastebin.com/6d9UtKM4)。我还在这里粘贴了运行"make tests"的当前结果(http://pastebin.com/h3Ny3dib)。(请注意该页底部自动删除所有.o文件的部分。)

我还希望能够使它像这样生成:

  • g++ -o compileDir/assembler。0 -c -Wall src/assembly .cpp
  • g++ -o compileDir/string。0 -c -Wall src/string.cpp
  • g++ -c -Wall -o compileDir/test_assignment。o testSrc/test_assignment.cpp
  • g++ -o testDir/test_assignment compileDir/test_assignment。o compileDir/字符串。o compileDir/assembler.o
  • g++ -c -Wall -o compileDir/test_括号。o testSrc/test_bracket.cpp
  • g++ -o testDir/test_bracket compileDir/test_bracket。o compileDir/字符串。o compileDir/assembler.o
  • testDir/test_bracket
  • testDir/test_assignment

换句话说,我希望它先编译所有内容,然后再运行所有内容。我希望这个要求不会太过分!

编辑:附加信息:(这是"进行测试"的代码)

tests: assembler.o string.o $(test_output) $(test_stringOutput)
        @echo '--- Testing complete ---'
$(testDir)%: $(compileDir)%.o string.o
        g++ -o $@ $< $(compileDir)string.o $(compileDir)assembler.o
        $@
        @echo ''
$(compileDir)%.o: $(testSourceDir)%.cpp
        g++ -c -Wall -o $@ $<
$(compileDir)%.o: $(testStringSrc)%.cpp
        g++ -c -Wall -o $@ $<

编辑 : -----------------------------------------

通过注释解析:

添加这一行修复它:.PRECIOUS $ (compileDir) % . o

你可以添加

.PRECIOUS: %.o

应该是隐式的,但也许你有一个奇怪的设置

Make将您的.o文件视为中间文件并删除它们。您可以通过将它们添加到特殊.SECONDARY目标的依赖项来防止它们被自动删除。有关更多细节,请参阅隐式规则链。好运!