makefile自动删除.o文件

tey*_*non 8 c++ makefile

我正在一个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.o -c -Wall src/assembler.cpp
  • g ++ -o compileDir/string.o -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/string.o compileDir/assembler.o
  • g ++ -c -Wall -o compileDir/test_bracket.o testSrc/test_bracket.cpp
  • g ++ -o testDir/test_bracket compileDir/test_bracket.o compileDir/string.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 $@ $<
Run Code Online (Sandbox Code Playgroud)

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

通过评论解决:

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

Ern*_*ill 12

你可以补充一下

.PRECIOUS: %.o
Run Code Online (Sandbox Code Playgroud)

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


小智 5

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