Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changed .h file in C++ does not need to be compiled again?

I have the following question. After a successful compilation, if I compile it again after I only change some content in one of the .h files, the computer says:

make: Nothing to be done for `all'.

Can I force the compiler to compile again even if I have only modified the .h files?

like image 443
Resorter Avatar asked Jun 22 '16 18:06

Resorter


3 Answers

If you want your output to be updated when header files change, then you have to add it to your dependency statement:

 myprogram: myprogram.cpp myprogam.h
      c++ -o myprogram myprogram.cpp

Typically we don't do it this way because the code that does things stays in the cpp file. If you are on unix and want to force a rebuild, just touch a cpp file to update its timestamp (forcing a rebuild) with "touch myprogram.cpp", for example, or delete your existing executable.

If you are using make without a Makefile, letting it infer dependencies, it will probably not infer the header dependencies. In this case, either blow away your executable or touch your source file.

like image 149
Ruan Caiman Avatar answered Nov 17 '22 15:11

Ruan Caiman


You can force make to rebuild everything using the --always-make command line option.

However, it sounds like you don't have your dependencies setup properly in your Makefile. If your code (.cpp files) actually include headers, then generally your target for compiling them should have a prerequisite on the header files that it includes.

like image 2
MuertoExcobito Avatar answered Nov 17 '22 14:11

MuertoExcobito


Sounds like your Makefile does not have dependencies configured correctly. That is what you should look into fixing.

If you really want to just force a rebuild rather than fix the underlying problem. Then you can do a make clean before your make all or, if the Makefile does not have a "clean" target, delete all the generated object files and libs/executables and then run make all again.

like image 3
Jesper Juhl Avatar answered Nov 17 '22 13:11

Jesper Juhl