📄 standards.texi
字号:
\input texinfo @c -*-texinfo-*-@c %**start of header@setfilename standards.info@settitle GNU Coding Standards@c %**end of header@ifinfo@formatSTART-INFO-DIR-ENTRY* standards: (standards). GNU Project Coding StandardsEND-INFO-DIR-ENTRY@end format@end ifinfo@setchapternewpage off@ifinfoCopyright (C) 1992 Free Software FoundationPermission is granted to make and distribute verbatim copies ofthis manual provided the copyright notice and this permission noticeare preserved on all copies.@ignorePermission is granted to process this file through TeX and print theresults, provided the printed document carries copying permissionnotice identical to this one except for the removal of this paragraph(this paragraph not being relevant to the printed manual).@end ignorePermission is granted to copy and distribute modified versions of thismanual under the conditions for verbatim copying, provided that the entireresulting derived work is distributed under the terms of a permissionnotice identical to this one.Permission is granted to copy and distribute translations of this manualinto another language, under the above conditions for modified versions,except that this permission notice may be stated in a translation approvedby the Free Software Foundation.@end ifinfo@titlepage@sp 10@titlefont{GNU Coding Standards}@author{Richard Stallman}@author{last updated 16 Jul 1992}@c Note date also appears below.@page@vskip 0pt plus 1filllCopyright @copyright{} 1992 Free Software FoundationPermission is granted to make and distribute verbatim copies ofthis manual provided the copyright notice and this permission noticeare preserved on all copies.Permission is granted to copy and distribute modified versions of thismanual under the conditions for verbatim copying, provided that the entireresulting derived work is distributed under the terms of a permissionnotice identical to this one.Permission is granted to copy and distribute translations of this manualinto another language, under the above conditions for modified versions,except that this permission notice may be stated in a translation approvedby Free Software Foundation.@end titlepage@ifinfo@node Top, Reading Non-Free Code, (dir), (dir)@top VersionLast updated 16 Jul 1992.@c Note date also appears above.@end ifinfo@menu* Reading Non-Free Code:: Referring to Proprietary Programs* Contributions:: Accepting Contributions* Change Logs:: Recording Changes* Compatibility:: Compatibility with Other Implementations* Makefiles:: Makefile Conventions* Configuration:: How Configuration Should Work* Source Language:: Using Languages Other Than C* Formatting:: Formatting Your Source Code* Comments:: Commenting Your Work* Syntactic Conventions:: Clean Use of C Constructs* Names:: Naming Variables and Functions* Using Extensions:: Using Non-standard Features* Semantics:: Program Behaviour for All Programs* Errors:: Formatting Error Messages* Libraries:: Library Behaviour* Portability:: Portability As It Applies to GNU* User Interfaces:: Standards for Command Line Interfaces* Documentation:: Documenting Programs* Releases:: Making Releases@end menu@node Reading Non-Free Code@chapter Referring to Proprietary ProgramsDon't in any circumstances refer to Unix source code for or duringyour work on GNU! (Or to any other proprietary programs.)If you have a vague recollection of the internals of a Unix program,this does not absolutely mean you can't write an imitation of it, butdo try to organize the imitation internally along different lines,because this is likely to make the details of the Unix versionirrelevant and dissimilar to your results.For example, Unix utilities were generally optimized to minimizememory use; if you go for speed instead, your program will be verydifferent. You could keep the entire input file in core and scan itthere instead of using stdio. Use a smarter algorithm discovered morerecently than the Unix program. Eliminate use of temporary files. Doit in one pass instead of two (we did this in the assembler).Or, on the contrary, emphasize simplicity instead of speed. For someapplications, the speed of today's computers makes simpler algorithmsadequate.Or go for generality. For example, Unix programs often have statictables or fixed-size strings, which make for arbitrary limits; usedynamic allocation instead. Make sure your program handles NULs andother funny characters in the input files. Add a programming languagefor extensibility and write part of the program in that language.Or turn some parts of the program into independently usable libraries.Or use a simple garbage collector instead of tracking precisely whento free memory, or use a new GNU facility such as obstacks.@node Contributions@chapter Accepting ContributionsIf someone else sends you a piece of code to add to the program you areworking on, we need legal papers to use it---the same sort of legalpapers we will need to get from you. @emph{Each} significantcontributor to a program must sign some sort of legal papers in orderfor us to have clear title to the program. The main author alone is notenough.So, before adding in any contributions from other people, tell usso we can arrange to get the papers. Then wait until we tell youthat we have received the signed papers, before you actually use thecontribution.This applies both before you release the program and afterward. Ifyou receive diffs to fix a bug, and they make significant change, weneed legal papers for it.You don't need papers for changes of a few lines here or there, sincethey are not significant for copyright purposes. Also, you don't needpapers if all you get from the suggestion is some ideas, not actual codewhich you use. For example, if you write a different solution to theproblem, you don't need to get papers.I know this is frustrating; it's frustrating for us as well. But ifyou don't wait, you are going out on a limb---for example, what if thecontributor's employer won't sign a disclaimer? You might have to takethat code out again!The very worst thing is if you forget to tell us about the othercontributor. We could be very embarrassed in court some day as aresult.@node Change Logs@chapter Change LogsKeep a change log for each directory, describing the changes made tosource files in that directory. The purpose of this is so that peopleinvestigating bugs in the future will know about the changes thatmight have introduced the bug. Often a new bug can be found bylooking at what was recently changed. More importantly, change logscan help eliminate conceptual inconsistencies between different partsof a program; they can give you a history of how the conflictingconcepts arose.Use the Emacs command @kbd{M-x add-change} to start a new entry in thechange log. An entry should have an asterisk, the name of the changedfile, and then in parentheses the name of the changed functions,variables or whatever, followed by a colon. Then describe the changesyou made to that function or variable.Separate unrelated entries with blank lines. When two entriesrepresent parts of the same change, so that they work together, thendon't put blank lines between them. Then you can omit the file nameand the asterisk when successive entries are in the same file.Here are some examples:@example* register.el (insert-register): Return nil.(jump-to-register): Likewise.* sort.el (sort-subr): Return nil.* tex-mode.el (tex-bibtex-file, tex-file, tex-region):Restart the tex shell if process is gone or stopped.(tex-shell-running): New function.* expr.c (store_one_arg): Round size up for move_block_to_reg.(expand_call): Round up when emitting USE insns.* stmt.c (assign_parms): Round size up for move_block_from_reg.@end exampleThere's no need to describe here the full purpose of the changes or howthey work together. It is better to put this explanation in comments inthe code. That's why just ``New function'' is enough; there is acomment with the function in the source to explain what it does.However, sometimes it is useful to write one line to describe theoverall purpose of a large batch of changes.You can think of the change log as a conceptual ``undo list'' whichexplains how earlier versions were different from the current version.People can see the current version; they don't need the change logto tell them what is in it. What they want from a change log is aclear explanation of how the earlier version differed.When you change the calling sequence of a function in a simplefashion, and you change all the callers of the function, there is noneed to make individual entries for all the callers. Just write inthe entry for the function being called, ``All callers changed.''When you change just comments or doc strings, it is enough to write anentry for the file, without mentioning the functions. Write just,``Doc fix.'' There's no need to keep a change log for documentationfiles. This is because documentation is not susceptible to bugs thatare hard to fix. Documentation does not consist of parts that mustinteract in a precisely engineered fashion; to correct an error, youneed not know the history of the erroneous passage.@node Compatibility@chapter Compatibility with Other ImplementationsWith certain exceptions, utility programs and libraries for GNU shouldbe upward compatible with those in Berkeley Unix, and upward compatiblewith @sc{ANSI} C if @sc{ANSI} C specifies their behavior, and upwardcompatible with @sc{POSIX} if @sc{POSIX} specifies their behavior.When these standards conflict, it is useful to offer compatibilitymodes for each of them.@sc{ANSI} C and @sc{POSIX} prohibit many kinds of extensions. Feelfree to make the extensions anyway, and include a @samp{--ansi} or@samp{--compatible} option to turn them off. However, if the extensionhas a significant chance of breaking any real programs or scripts,then it is not really upward compatible. Try to redesign itsinterface.When a feature is used only by users (not by programs or commandfiles), and it is done poorly in Unix, feel free to replace itcompletely with something totally different and better. (For example,vi is replaced with Emacs.) But it is nice to offer a compatiblefeature as well. (There is a free vi clone, so we offer it.)Additional useful features not in Berkeley Unix are welcome.Additional programs with no counterpart in Unix may be useful,but our first priority is usually to duplicate what Unix alreadyhas.@node Makefiles@chapter Makefile ConventionsThis chapter describes conventions for writing Makefiles.@menu* Makefile Basics::* Standard Targets::* Command Variables::* Directory Variables::@end menu@node Makefile Basics@section General Conventions for MakefilesEvery Makefile should contain this line:@exampleSHELL = /bin/sh@end example@noindentto avoid trouble on systems where the @code{SHELL} variable might beinherited from the environment.Don't assume that @file{.} is in the path for command execution. Whenyou need to run programs that are a part of your package during themake, please make sure that it uses @file{./} if the program is built aspart of the make or @file{$(srcdir)/} if the file is an unchanging partof the source code. Without one of these prefixes, the current searchpath is used. The distinction between @file{./} and @file{$(srcdir)/} is importantwhen using the @samp{--srcdir} option to @file{configure}. A rule ofthe form:@examplefoo.1 : foo.man sedscript sed -e sedscript foo.man > foo.1@end example@noindentwill fail when the current directory is not the source directory,because @file{foo.man} and @file{sedscript} are not in the currentdirectory.Relying on @samp{VPATH} to find the source file will work in the casewhere there is a single dependency file, since the @file{make} automaticvariable @samp{$<} will represent the source file wherever it is. Amakefile target like@examplefoo.o : bar.c $(CC) $(CFLAGS) -I. -I$(srcdir) -c bar.c -o foo.o@end example@noindentshould instead be written as@examplefoo.o : bar.c $(CC) $(CFLAGS) $< -o $@@end example@noindentin order to allow @samp{VPATH} to work correctly. When the target hasmultiple dependencies, using an explicit @samp{$(srcdir)} is the easiestway to make the rule work well. For example, the target above for@file{foo.1} is best written as:@examplefoo.1 : foo.man sedscript sed -s $(srcdir)/sedscript $(srcdir)/foo.man > foo.1@end example@node Standard Targets@section Standard Targets for UsersAll GNU programs should have the following targets in their Makefiles:@table @samp@item allCompile the entire program.@item installCompile the program and copy the executables, libraries, and so on tothe file names where they should reside for actual use. If there is asimple test to verify that a program is properly installed then run thattest.Use @samp{-} before any command for installing a man page, so that@code{make} will ignore any errors. This is in case there are systemsthat don't have the Unix man page documentation system installed.@item cleanDelete all files from the current directory that are normally created bybuilding the program. Don't delete the files that record theconfiguration. Also preserve files that could be made by building, butnormally aren't because the distribution comes with them.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -