1

I am facing an error while running my c program using makefile. When I run the following command directly on the terminal,the program runs correctly

$ ./a.out test1.c test2.c

(a.out is the executable generated by compiling the program,test1.c and test2.c are the command line arguments)

But when I write the below in makefile:

all : compile run

compile :
    gcc ConsonantVowelCount15.c
run :
    ./a.out $(INPUT)

and run the following command on terminal

$ make INPUT=test1.c\ test2.c

It gives the output followed by following error

makefile:6: recipe for target 'run' failed
make: *** [run] Error 45

Mohit Jain
  • 30,259
  • 8
  • 73
  • 100
Abhishek Gangwar
  • 1,697
  • 3
  • 17
  • 29
  • 1
    Make compile dependent on run and retry by writing `run : compile` instead of `run :` – Mohit Jain Jul 29 '15 at 15:04
  • Rename `compile` to `a.out` and use `run: a.out` to have a makefile that actually means something useful. The use `all: run`. You can also add `compile: a.out` if you still want to be able to type `make compile`. That being said all that may have nothing to do with the problem since you haven't actually told us what the problem is. The actual error is **above** the snippet of error you pasted. – Etan Reisner Jul 29 '15 at 15:10

1 Answers1

2

Besides the comments you have already been given about how run should depend on a.out and compile should really be changed to a.out or be made a phony that relies on a.out the exact reason you are getting this error message is because your program is returning a non-zero error value, specifically 45.

A recipe fails when any of the commands in that recipe return a non-zero value (assuming Linux and most other systems where 0 indicates a success). Therefore your run target is failing because ./a.out $(INPUT) is returning something other than 0. This is of course assuming that you have included the entire error message and the error number and Makefile and the error message is not from a dependency of run. A simple way to check would be to replace ./a.out $(INPUT) with true. If everything works then it is your program itself.

Note that the program may seem to run correctly but still return an error value. One possible reason for this is you have not included a return statement in your main function. Assuming you are using bash you can view the return value of the latest program via echo $?.

missimer
  • 4,022
  • 1
  • 19
  • 33
  • It worked I did not return 0 in the main. But some of my programs are working without returning 0 – Abhishek Gangwar Jul 29 '15 at 16:20
  • It is not very difficult to always `return 0` from `main` or `exit` with a meaningful error status. The fact that your compiler is both lenient about missing return statements or erroneous `main` prototypes, and non conforming is no excuse for sloppiness. – chqrlie Jul 29 '15 at 17:03
  • 1
    @AbhishekGangwar If you do not do a return in your main function or call exit your program will most likely compile depending on your compiler and settings. However just because it compiles does not mean everything is good. In this case you will have undefined behavior as main is expected to return an integer. You might want to check out this SO post: http://stackoverflow.com/questions/204476/what-should-main-return-in-c-and-c. – missimer Jul 29 '15 at 18:46
  • 1
    @AbhishekGangwar Also if my answer solved your problem (it is a little hard to tell from your comment) you can accept the answer and we both get reputation. More importantly though, future visitors to this page will know that this solved your problem so if they have a similar problem it is can indicator that it might help. Again only if this solved your problem. – missimer Jul 29 '15 at 18:48
  • @AbhishekGangwar - Its always a cordial gesture to return the favour by accepting the answer, if the answer helped you – Amrith Krishna Aug 02 '15 at 08:22