Common programs like gcc, rm, cp etc. seem to follow a pattern in reporting their errors like this:
program: error
For instance:
$ gc开发者_如何学编程c
gcc: no input files
Is there some standard function or macro I should call from within my own C programs to report errors in a standard fashion?
There are no macros (that I know of). But, here's the relevant standard for formatting error output.
If you're not aware of perror
and strerror
(etc.), they're worth knowing about. They're not universal by any means, but can be helpful under at least some circumstances.
It dates back to the earliest days of Unix programming. It was always a recommendation that error messages be prefixed by the name of the command generating them, not least so that if errors occur in a pipeline of commands, you can tell which of the various commands generated the error message. You can see the style in K&R 1st Edition, in some of the example programs (not quite all), and also in 'Software Tools' by Kernighan & Plauger, and in 'The UNIX Programming Environment' by Kernighan & Pike.
There are no completely standard tools - functions or macros - for such error messages. (I have an extensive and comprehensive set of error reporting functions that do print the program name ahead of the message.)
精彩评论