Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

makefile auto removing .o files

Tags:

c++

makefile

I am taking a C++ course in college and they want us to manually type in all of the test files... I know, however, that there is a way to do it with out, which is how I ended up with the current(http://pastebin.com/6d9UtKM4) makefile. My question is, why is this makefile automatically removing all the .o files it uses for compiling when it is done? It's not killing me, but I would like to preserve the .o files. I have pasted the makefile here(http://pastebin.com/6d9UtKM4). I have also pasted the current result of running "make tests" here(http://pastebin.com/h3Ny3dib). (Note the part at the bottom of that page that removes all the .o files automatically.)

I would also like to be able to make it generate it like this:

  • 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

In other words, I want it to compile everything, then run everything. I hope this isn't too much to ask!

Edit: Additional Information: (This is the code that does "make tests")

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 $@ $<

EDIT: -----------------------------------------

Resolved via comments:

Adding this line fixed it: .PRECIOUS $(compileDir)%.o

like image 477
teynon Avatar asked Nov 06 '11 00:11

teynon


People also ask

What is a .o file Makefile?

.o denote "object-files", these are files compiled from source but not yet linked into an executable or a library. In your make-file, i.e. main.o : main.cpp. says that main.o will be created from main.

How to write a clean in Makefile?

The Cleanup Rule clean: rm *.o prog3 This is an optional rule. It allows you to type 'make clean' at the command line to get rid of your object and executable files. Sometimes the compiler will link or compile files incorrectly and the only way to get a fresh start is to remove all the object and executable files.

Can you run .O files?

An object file ( .o ) is not executable. You want to be running ./demo_fully_homomorphic (e.g. the file without extension). Make sure you have execute permissions ( chmod a+x demo_fully_homomorphic ).

What is a .O and .D file?

The ".o" files are likely intermediate files from which the actual executable program should have been created. The ". d" files are likely internal state used by the makefile, only important if you are making changes to the source code and then rebuilding "incrementally".

Can I delete a .o file?

Yes, you can remove it. BUT, if you program consists of multiple files and you change one of them, lack of .o s would make CB recompile the entire program (once) instead of just the changed file.


1 Answers

You might add

.PRECIOUS: %.o

which should be implicit, but perhaps you've got a weird setup.

like image 168
Ernest Friedman-Hill Avatar answered Sep 22 '22 03:09

Ernest Friedman-Hill