1290 1290 - 23 days ago 4
C Question

Makefile Executable Error

I have the following Makefile:

all: test.c test1.c
gcc -o test test.c -lm
gcc -o test1 test1.c
./test 1000 input.txt


I am getting an error like
./test 1000 input.txt make: *** [run] Error 255
. Is the Makefile correct?

Answer

If you want to run a command after a successful build, use separate prerequisites as follows:

all: test test1

run_test: test
    ./test 1000 input.txt

test: test.c
    gcc -o test test.c -lm

test1: test1.c
    gcc -o test1 test1.c

Your Makefile doesn't check if the build was successful. So it is possible that the test executable is not available when you are running ./test 1000 input.txt.

Another way is to chain the commands using the shell AND operator:

all: test.c test1.c
    gcc -o test test.c -lm && ./test 1000 input.txt
    gcc -o test1 test1.c

But the preferred way is definitely to use separate prerequisites (targets).

P.S.

As mentioned, you should always use -Wall compiler option!

Comments