📄 faqcat204f.html
字号:
leave it there.(Private file-scope functions andvariablesshouldalsobe declared<TT>static</TT>.See also question <a href="faqcat6b6b.html?sec=struct#opaquetypes">2.4</a>.)</p><p>Finally,you should not put actual code(i.e. functionbodies)or global variable definitions(that is, definingor initializinginstances)in header files.Also,when you are building a projectout of multiple source files,you should compile each of them separately(using a compiler optionto request compilation only),and use the linkerto link the resultant object files together.(In an integrated development environment,all of this may be taken care of for you.)Don't try to ``link'' all of your source files togetherwith <TT>#include</TT>;the <TT>#include</TT> directive should be usedto pull inheader files,not other <TT>.c</TT> files.</p><p>See alsoquestions <a href="faqcatd3c2.html?sec=decl#decldef">1.7</a>, <a href="faqcat204f.html?sec=cpp#nestincl">10.7</a>, and <a href="faqcataae2.html?sec=style#srcfiles">17.2</a>.</p><p>Additional links:<a href="../../cpp/sd7.html" rel=subdocument>more explanation</a></p><p>References:K&R2 Sec. 4.5 pp. 81-2<br>H&S Sec. 9.2.3 p. 267<br>CT&P Sec. 4.6 pp. 66-7<hr><hr><hr><a name="nestincl"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../cpp/nestincl.html"><!-- qtag -->Question 10.7</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>Is it acceptable for one header file to <TT>#include</TT> another?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>It's a question of style,and thus receives considerable debate.Many people believe that``nested <TT>#include</TT> files''are to be avoided:the prestigious Indian Hill Style Guide(see question<a href="faqcataae2.html?sec=style#guides">17.9</a>)disparages them;they can make it harder to find relevant definitions;they can lead to multiple-definition errors if a file is <TT>#include</TT>d twice;they can lead toincreased compilation time;andthey make manual Makefile maintenance very difficult.On the other hand,they make it possible to use header files in a modular way (aheader file can <TT>#include</TT> what it needs itself,rather thanrequiring each <TT>#include</TT>r to do so);a tool like <TT>grep</TT>(or a tags file)makes it easy to find definitions no matter where they are;a popular trickalong the lines of:<pre> #ifndef HFILENAME_USED #define HFILENAME_USED ...header file contents... #endif</pre>(where a differentbracketingmacro nameis used for each header file)makes a header file ``idempotent''so that it can safely be <TT>#include</TT>d multiple times;a clever compiler can avoid expending any more timeon later instances ofan already-included header;and automated Makefile maintenance tools(which are a virtual necessity in large projects anyway;see question <a href="faqcatccbd.html?sec=resources#tools">18.1</a>)handle dependency generation in the face of nested <TT>#include</TT> files easily.See also question <a href="faqcataae2.html?sec=style#stylewars">17.10</a>.</p><p>References:Rationale Sec. 4.1.2<hr><hr><hr><a name="inclkinds"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../cpp/inclkinds.html"><!-- qtag -->Question 10.8a</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>What's the difference between<TT>#include <></TT>and<TT>#include ""</TT>?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>The<TT><></TT> syntax istypically used withStandard or system-supplied headers,while <TT>""</TT> is typically used for a program's own header files.See also question <a href="faqcat204f.html?sec=cpp#cppsearchpath">10.8b</a>.</p><p>Additional links:<br><br><a href="../../cpp/inclk.kaz.html">nice explanation</a>by Kaz Kylheku<br><br>An<a href="../../cpp/incl.970507.html">article</a>and a longer<a href="../../cpp/incl.970605.html">followup</a>I posted in 1997 exploring some of the finer points whichsometimes arise in deciding between <TT>#include ""</TT>and <TT>#include <></TT>.(Warning:among the possibilities exploredis that of using <TT>#include <></TT> for your own header filesunder certain circumstances,a practice which is <em>not</em> standardand is not condoned by the readership of <TT>comp.lang.c</TT>.)<hr><hr><hr><a name="cppsearchpath"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../cpp/cppsearchpath.html"><!-- qtag -->Question 10.8b</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>What are the complete rules for header file searching?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>The exact behavior is implementation-defined(which means that it is supposed to be documented;see question <a href="faqcat7d4b.html?sec=ansi#undef">11.33</a>).Typically, headersnamed with <TT><></TT>syntaxare searched forin one or more standard places.<a href="../../cpp/bihdr.html" rel=subdocument>[footnote]</a>Header files named with <TT>""</TT>syntaxarefirst searched forin the ``current directory,''then(if not found)in the same standard places.(This last rule,that<TT>""</TT> filesare additionally searchedfor as if they were <TT><></TT> files,istheonly rulespecified by the Standard.)</p><p>Another distinction is the definition of ``current directory''for <TT>""</TT> files.Traditionally(especially under Unix compilers),the current directory istaken to bethe directorycontaining the filecontainingthe <TT>#include</TT> directive.Under other compilers, however,the current directoryis the directory in which the compiler was initially invoked.(Compilers running on systems without directoriesorwithoutthe notion of a current directorymay of course use still different rules.)</p><p>It is also common for there to be a way(usually a command line option involving capital I,or maybe an environment variable)toadd additional directoriesto the list of standard places tosearch.Check yourcompilerdocumentation.</p><p>Additional links:<br><br><a href="../../cpp/include.dmr.html">further elaboration by Dennis Ritchie</a><br><br>A<a href="../../cpp/incl.970605.html">long article</a>of mine exploring some of the bore subtle implications of the search rules</p><p>References:K&R2 Sec. A12.4 p. 231<br>ISO Sec. 6.8.2<br>H&S Sec. 3.4 p. 55<hr><hr><hr><a name="headerglom"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../cpp/headerglom.html"><!-- qtag -->Question 10.9</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>I'm getting strangesyntax errorson the very firstdeclaration ina file,but it looks fine.</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>Perhaps there's a missing semicolon at the end of the lastdeclaration in the last header file you're #including.See alsoquestions <a href="faqcat6b6b.html?sec=struct#retcrash">2.18</a>, <a href="faqcat7d4b.html?sec=ansi#preansi">11.29a</a>, <a href="faqcat5e04.html?sec=strangeprob#emptyloop">16.1</a>, and <a href="faqcat5e04.html?sec=strangeprob#unclosed">16.1b</a>.<hr><hr><hr><a name="def3rdparty"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../cpp/def3rdparty.html"><!-- qtag -->Question 10.10</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>I'm using header fileswhichaccompany two different third-party libraries,and they are ``helpfully'' definingcommon macros such as <TT>TRUE</TT>,<TT>FALSE</TT>,<TT>Min()</TT>,and <TT>Max()</TT>,but the definitions clash with each otherand with definitions I'dalready established in my own header files.What can I do?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>This is indeed an annoying situation.It's a classic namespace problem;see questions <a href="faqcatd3c2.html?sec=decl#semiglobal">1.9</a> and <a href="faqcatd3c2.html?sec=decl#namespace">1.29</a>.Ideally,third-party vendors would be conscientious when defining symbols(both preprocessor <TT>#define</TT>s and global variable and function names)to assure that namespace collisions wereunlikely.The best solutionisto get the vendor(s)to fix their header files.</p><p>As a workaround,you cansometimesundefine or redefinetheoffendingmacrosbetween the conflicting <TT>#include</TT> directives.<hr><hr><hr><a name="extlibs"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../cpp/extlibs.html"><!-- qtag -->Question 10.10b</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>I'm #including therightheader file forthe library function I'm using,but the linker keeps saying it's undefined.</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>See question <a href="faqcat1067.html?sec=lib#extlibs">13.25</a>.<hr><hr><hr><a name="missinghdr"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../cpp/missinghdr.html"><!-- qtag -->Question 10.11</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>I'm compiling a program, andI seem to be missingoneof theheader files itrequires.Can someone send mea copy?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>There are several situations, depending on what sort of headerfile it is that's ``missing''.</p><p>If the missing header file istrulya standard one(that is,one defined by the ANSI C Standard,such as <TT><stdio.h></TT>),there's a problem with your compiler.Eitherthe compilerwasn't installed properly,or your project is somehow not configuredto find the standard header files.You'll need to contact your vendor,or someone knowledgeable about your particular compiler,for help.</p><p>Inthe case of nonstandard headers,the situation is considerably more complicated.Someheaders(such as <TT><dos.h></TT>)are completely system- or compiler-specific.Someare completely unnecessary,and should be replaced bytheir Standard equivalents.(For example,instead ofthe nonstandard<TT><malloc.h></TT>and<TT><memory.h></TT>,portablesource code should instead <TT>#include</TT><TT><stdlib.h></TT>and<TT><string.h></TT>,respectively.)Otherheaders,such as those associated with popular add-on libraries,may be reasonably portable.</p><p>If the missing header file is an OS-specific one,such as<TT><sgtty.h></TT>,<TT><sys/stat.h></TT>,<TT><netinet/in.h></TT>,or<TT><dos.h></TT>,it may be that the program you're compiling wastailored toan operating system other than the one you're using.(It's also possible thatsome conditional compilation settings need to be adjusted.)It likely won't be possible to get the program workingwithout rewriting its system-dependent portions.Simply getting copies of the missing header files wouldn't help--theytypically contain declarations of the interfacesto the various system calls and libraries.Obtaining copies of the header files wouldn't get you copies of the libraries,and the libraries wouldn't be of any use(that is, they wouldn't work)without the underlying system calls, anyway.See alsoquestions <a href="faqcatea63.html?sec=osdep#cbreak">19.1</a>, <a href="faqcatea63.html?sec=osdep#termcap">19.4</a>, <a href="faqcatea63.html?sec=osdep#modtime">19.12b</a>, and <a href="faqcatea63.html?sec=osdep#int86">19.40c</a>.</p><p>If the missing header file is forsome external, add-on, third-partylibrary,look for the header where you got the library.If you have some source codewhich #includes what appears to be an add-on header,butwhichyou don't have,you probably don't have the library, either,and you'll need both.See question <a href="faqcat1067.html?sec=lib#extlibs">13.25</a>;see also question <a href="faqcatccbd.html?sec=resources#sources">18.16</a>.</p><p>If the header file is unique to
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -