7

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?

3
  • 1
    If your dependencies are set up correctly, the make program should compile again if any header or source file is modified. Jun 22, 2016 at 19:21
  • 1
    @Thomas Matthews Obviously, dependencies are not configured correctly in this case. Jun 22, 2016 at 19:41
  • If you interpret the question as the OP wants to force a rebuild if a header file is changed, then it becomes a more interesting (or challenging) issue. Jun 22, 2016 at 19:45

5 Answers 5

6

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.

2
  • Wouldn't just removing the executable and not the object files it is comprised of, just force a re-link, not a re-compile? Thus not solving the problem. Jun 22, 2016 at 19:39
  • I was assuming a simpler compile as in my example, without intermediate object files. Of course, yes, if only object files are in the dependency list, then it will just re-link. Jun 22, 2016 at 19:44
3

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.

3
  • 2
    This should be a comment. It does not answer the question.
    – AndyG
    Jun 22, 2016 at 19:06
  • Thanks! I agree that OPs problem would likely be solved if they fixed their dependencies, however the core of the question is actually kind of interesting: If a .h file is changed, force a rebuild. This is something I don't know off-hand how to do, and would be interested in seeing someone answer. forcing a "make clean" before building is kind of a sledgehammer approach.
    – AndyG
    Jun 22, 2016 at 19:12
  • 1
    everything (my answer included) is a sledgehammer approach. If dependencies aren't tracked properly, you basically have no option but to do a full rebuild. Jun 22, 2016 at 19:41
2

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.

2

There is a simpler way than the accepted answer. Simply add -MD to your compiler flags in your Makefile, and -include myfile.d at the end of the Makefile (listing all source files with a *.d extension instead). This will, respectively, generate and reference additional *.d dependency files in your build folder (wherever your *.o files go) when you make, so you do not need to explicitly add every single header file to your makefile dependencies.

This is useful for projects with a long list of header files. Furthermore, this way, you know that you can't forget to include a header file in your Makefile dependencies, preventing troubleshooting time lost later when you think your binary updated when you changed a header file, but it actually didn't because you forgot to put it in the Makefile.

For example, use gcc -MD -I. -c myfile.cpp -o obj/myfile.o, and you can keep your Makefile dependencies as just foo: myfile.cpp without myfile.h.

A shortcut way to do this so you only need to list all files once is something like the following:

# Beginning of Makefile etc. etc.

# Only need to list all files once, right here.
SRCS = myfile.cpp myfile2.cpp

OBJS = $(SRCS:%.cpp=%.o)

# put .o and .d files in ./obj/
# (Assumes 'obj' directory exists)
FULLOBJS = $(addprefix obj/,$(OBJS))

# rule to make object (*.o) files
$(FULLOBJS): obj/%.o:%.cpp
    gcc -MD -I. -c %< -o $@

# rule to make binary
foo: $(FULLOBJS)
    g++ -o $@ $(FULLOBJS)

# rule to clean (Note that it also deletes *.d files)
.PHONY: clean
clean:
    rm -rf obj/*.o obj/*.d foo

# include dependency files (*.d) if available
-include $(FULLOBJS:%.o=%.d)
2
  • 1
    Or you could do SRCS = $(shell find . -name '*.cpp') to avoid manually listing the files. Mar 1, 2018 at 11:20
  • 1
    Works as long as your source files are all in the same folder/subfolders, AND you don't have any extra ones you don't necessarily want to compile. Often not the case for my projects, but would be better for some cases.
    – Keith M
    Mar 12, 2018 at 18:43
0

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

Yes ... but you probably want to improve your make (tool).

What I do is to force the most recent compile of the file in question, where the command generated by make shows.

Example:

# ... noise 

g++ -O3 -ggdb -std=c++14 -Wall -Wextra -Wshadow -Wnon-virtual-dtor -pedantic -Wcast-align -Wcast-qual -Wconversion -Wpointer-arith -Wunused -Woverloaded-virtual   -O0  lmbm101_11.cc -o lmbm101_11 -L../../bag -lbag_i686 -lnet_i686 -lposix_i686 -lzlib_i686 -lrt -pthread

# ...  more noise.

To force a build, I highlight the "command" make created (starts with "g++", and resides between noise and more noise), and invoke it instead of make.

This is trivial using emacs on Linux. Might not be so easy on other systems.

You might consider copying this command into the file, for future use.

( i.e. I bypass make until I choose to fix my make file. )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.