外部函数声明(另见问题1.11) 全局变量声明 将声明或定义放入其中尤为重要 a头文件将在其他几个 文件之间共享。 (特别是,永远不要把外部函数原型 放在.c文件中。另见问题1.7。) 另一方面,当定义或声明时应该 对一个.c文件保密,可以将它保留在那里。 另见问题1.7和10.7。 参考文献:K& R2 Sec。 4.5页.81-2; H& S Sec。 9.2.3 p。 267; CT& P 秒4.6 pp.66-7。 否。不要这样做。强调:不要那样做。包含带有.c的 文件扩展是一个设计不良的代码,这可能会导致潜在的维护者立刻感到恶心。 你究竟尝试了什么?典型的设置是: ac:函数foo的定义。 啊:函数foo的声明。 main。 c:main的定义,包括ah和引用foo。 单独编译main.c和ac。 链接main.o和ao生成可执行文件。 你尝试了什么不起作用? No. *不要这样做。 *强调:不要那样做。 *包含带有.c的 文件。扩展是一个设计不良的代码,这可能会导致潜在的维护者立刻感到恶心。 我有时会进行优化(它允许编译器为某些编译器内联 )。 它确实有另一个有益的副作用 - 它通常会强制你 来创建幂等头文件。 然后我创建两个项目 - 正常构建项目有单独的C 文件,优化项目有一个这样的文件: #include" project.h" 的#include" file01.c" 的#include" file02.c" 的#include" file03.c" #包括" file04.c" 的#include" file05.c" 的#include" file06.c" 的#include" ; file07.c" 的#include" file08.c" 的#include" file09.c" 的#include" file10 .c" 当然,实际的文件会有比这更好的名字。 另一方面,我做的很棒和你在一起除非你需要最后一点点油腻的速度,否则那种邪恶的诡计就不是个好主意。 而且有些编译器足够聪明,可以完成同样的事情。 /> 没有俗气的内联技巧。 [snip] Hallo allemaal,This is my first message to this group. Until now I mainly wroteprograms in Turbo Pascal. But now I''m involved in a project wereknowledge of C is needed. I''m not unfamiliar with C, but the last timeI used it was maybe 8 years ago. I wrote quite some programs in TP and one goal is to translate theminto C. In time I created some common, so called, units. I alreadyfound out that I can include a C source using ''#include "unit.c"''. Butall programs I know of use ''#include <xxxx.h>''. I found some programsusing routines declared in other programs and both seemed to be linkedby an H file. But whatever I tried, I received error messages. (I''musing Turbo C 3.1 for the moment) Of course I tried to search the Internet for answers but I eitherfound nothing or was flooded with links that didn''t provide the answerI needed (mostly caused by not being native English spoken). Cananybody give me some links to show me how to use #include or tell mehere? Many thanks in advance!--___/ __|__/ / |_/ Groetjes, Ruud Baltissen\ \__|_\\___| http://Ruud.C64.org 解决方案 From the C-FAQ: 10.6:I''m splitting up a program into multiple source files for thefirst time, and I''m wondering what to put in .c files and whatto put in .h files. (What does ".h" mean, anyway?) A:As a general rule, you should put these things in header (.h)files: macro definitions (preprocessor #defines)structure, union, and enumeration declarationstypedef declarationsexternal function declarations (see also question 1.11)global variable declarations It''s especially important to put a declaration or definition ina header file when it will be shared between several otherfiles. (In particular, never put external function prototypesin .c files. See also question 1.7.) On the other hand, when a definition or declaration shouldremain private to one .c file, it''s fine to leave it there. See also questions 1.7 and 10.7. References: K&R2 Sec. 4.5 pp. 81-2; H&S Sec. 9.2.3 p. 267; CT&PSec. 4.6 pp. 66-7.No. Do not do that. For emphasis: DO NOT DO THAT. Including afile with a ".c" extension is a sure sign of poorly designedcode that will cause instant nausea in a potential maintainer. What exactly did you try? The typical set up is: a.c: definition of function foo.a.h: declaration of function foo.main.c: definition of main which includes a.h and references foo. compile main.c and a.c separately.link main.o and a.o to produce executable. What did you try that didn''t work? No. *Do not do that. *For emphasis: DO NOT DO THAT. *Including afile with a ".c" extension is a sure sign of poorly designedcode that will cause instant nausea in a potential maintainer.I do it sometimes for optimization (it allows the compiler to inlinebetter for some compilers).It does have another beneficial side effect -- it usually forces youto create idempotent header files. Then I create two projects -- the normal build project has separate Cfiles and the optimization project has one file like this: #include "project.h"#include "file01.c"#include "file02.c"#include "file03.c"#include "file04.c"#include "file05.c"#include "file06.c"#include "file07.c"#include "file08.c"#include "file09.c"#include "file10.c" Of course, the actual files will have better names than that.On the other hand, I do agree with you. That sort of evil trickery isnot a good idea unless you need the last little greasy bit of speed.And some compilers are smart enough to accomplish the same thingwithout the cheesy inline trick.[snip] 这篇关于如何使用H文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-30 20:27