使用Makefile在C中的程序时出现错误 [英] Getting an error while a program in c using makefile

查看:41
本文介绍了使用Makefile在C中的程序时出现错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用makefile运行我的c程序时遇到错误.当我直接在终端上运行以下命令时,程序正常运行

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是通过编译程序生成的可执行文件,test1.c和test2.c是命令行参数)

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

但是当我在makefile中编写以下内容时:

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:目标运行"的配方失败
生成:*** [运行]错误45

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

推荐答案

除了已经给出的有关 run 如何依赖 a.out 和<实际上,应该将code> compile 更改为 a.out ,或者将其变成依赖于 a.out 的假冒的确切原因是因为您的程序返回的非零错误值,特别是45.

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.

当配方中的任何命令返回非零值时,配方将失败(假设Linux和大多数其他系统(其中0表示成功)).因此,您的 run 目标失败,因为 ./a.out $(INPUT)返回的不是0.这当然是假定您已包含整个错误消息.错误号,Makefile和错误消息不是来自 run 的依赖项.一种简单的检查方法是将 ./a.out $(INPUT)替换为 true .如果一切正常,那么它就是您的程序本身.

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.

请注意,该程序似乎可以正确运行,但仍返回错误值.一个可能的原因是您没有在 main 函数中包含 return 语句.假设您正在使用bash,则可以通过 echo $?查看最新程序的返回值.

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 $?.

这篇关于使用Makefile在C中的程序时出现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆