How to properly make my makefile to compile and run?

If you're going to build and run over and over, You can use the history command to help with this:

# Run this once
make && ./foo

# Repeat last command
!!

You can simplify your 'run' target by having it depend on whether your program is up to date, and then simply run the program:

run:    ${PROGRAM_NAME}
        ./${PROGRAM} ${ARGS}

There's not much point in running make when you're already running make - at least, not in this context. Maybe for recursive operations (in different directories), but see 'Recursive Make Considered Harmful'.

Also, your makefile should normally provide a target 'all' and it should normally the first and therefore default target.


Running from the makefile is a bit unusual. Are you, perhaps, trying to duplicate the "Compile and Run" Menu item that some IDE provide? Make is not well equipped to do that.

All the stuff that happens in the target commands happens in sub-processes that are not attached directly to the terminal, which is why make receives your key stroke.

Another thing to look at: usually the object-file to executable stage (linking) uses a different set of flags (LDFLAGS and LIBS) then the compile stage. In this simple example you can get away with it, but if you copy this makefile for use in a more complicated case you'll run into trouble.