1

I created a makefile for my project and it looks like this:

CC = g++
LDFLAGS = -lpthread
CFLAGS = -Wall -pedantic -Wno-long-long -O0 -ggdb
SOURCES = main.o List.o ProcessCommands.o HandleTransfers.o

all:    $(SOURCES)

%.o: %.cpp
    $(CC) $< -o $@ -c $(CFLAGS) $(LDFLAGS)

compile:
    $(CC) $(SOURCES) -o executable/ftp $(LDFLAGS)

run:
    ./executable/ftp

clean:
    rm -rf *o ftp 

doc:
    doxygen ftpDocConf

The main issue is that when I modify any header file, that is included in one of my source files, the "make all" command does not notice any changes and so returns "make: Nothing to be done for `all'." I have searched the internet and still wasn't able to get it to work. I also have another "problem" with the makefile. The project this makefile is used for is an ftp server and when I type "make run" to run the executable, do something with the server and the exit the program the makefile returns "make: * [run] Interrupt". Is there any way to make this message not appear? I guess it appears because I exit the program using ctrl+c but is there a way to do it differently so I won't get any "error"?

Thanks in advance for any answers

2 Answers 2

1

The reason your object files don't recompile when you change your header files is that you've not declared any header files to be prerequisites of the object files. You have to do this explicitly, or else arrange for that information to be auto-generated (typically by your compiler). Your pattern rule %.o : %.cpp tells make that if a .cpp file changes, the associated .o file must be rebuilt. You need to add some xxx.o : xxx.h ... rules so that make knows about the header files as well. Or, as I mentioned, arrange for them to be auto-generated. There are lots of solutions out there for this, for example: GNU make: Generating automatic dependencies with generated header files

For your second question, there's no way to do that. When you press ^C at your terminal it sends the interrupt signal not just to the ftp program, but also to make itself. There's no way to convince make to ignore that signal that I'm aware of. The only way to do it would be to write a wrapper around make that changed the process group ID so it didn't receive those signals... but then I don't know how the ftp program would join the process group again.

I recommend you find a less destructive way to exit the program, if this is a serious problem. Perhaps you could send a SIGHUP or SIGTERM to the process directly, using kill or similar.

Then if you want make to ignore the error, change your rule to something like this:

run:
        ./executable/ftp || true
1
  • Thanks, I see it it not that hard to solve :). As for the second problem, I solved it by catching the ^c signal. Jun 3, 2013 at 17:40
1

MadScientist explains your problem with headers well. I can add these cheap and dirty fixes:

# header files added as dependencies of .o files below
%.o: %.cpp header1.h header2.h header3.h
    $(CC) $< -o $@ -c $(CFLAGS) $(LDFLAGS)

To get around your make run problem, you can change your run target to execute in an xterm -- this way the xterm catches the signal, rather than make:

run:
    xterm -e ./executable/ftp
2
  • Thanks for the help. It works of course but I don't have all the headers included in every source. With this code, when I modify one header, every source will be compiled right? Jun 3, 2013 at 17:45
  • Yes, as I said, quick and dirty. If you don't want that, you should use @MadScientist's suggestion of adding finer grained dependencies ... xxx.o: xxx.cpp xxx.h ...
    – Markku K.
    Jun 3, 2013 at 17:50

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.