gpt4 book ai didi

c++ - makefile 自动删除 .o 文件

转载 作者:可可西里 更新时间:2023-11-01 17:23:31 26 4
gpt4 key购买 nike

我在大学里学习 C++ 类(class),他们希望我们手动输入所有测试文件...但是我知道有一种方法可以做到这一点,这就是我最终得到的结果current ( http://pastebin.com/6d9UtKM4 ) 生成文件。我的问题是,为什么这个 makefile 在完成后会自动删除它用于编译的所有 .o 文件?它不会杀死我,但我想保留 .o 文件。我已经粘贴了 makefile here (http://pastebin.com/6d9UtKM4)。我还粘贴了运行“make tests”的当前结果here (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 $@ $<

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

通过评论解决:

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

最佳答案

你可以添加

.PRECIOUS: %.o

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

关于c++ - makefile 自动删除 .o 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8024353/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com