使用#ifndef [英] using #ifndef

查看:83
本文介绍了使用#ifndef的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序,我的main.c和program.c文件都使用program.h

文件。所以我#include" program.h"在.c文件中。 program.h文件



#ifndef PROGRAM_H

#define PROGRAM_H

....

#endif


然而,当我构建我的程序时,DJGPP编译器告诉我每个函数有多个

定义。


关于我在这里做错了什么想法?


谢谢!


John

I have a program where both my main.c and program.c files use the program.h
file. So I #include "program.h" in both the .c files. The program.h file
has
#ifndef PROGRAM_H
#define PROGRAM_H
....
#endif

Yet when I build my program, the DJGPP compiler tells me there are multiple
definitions of each of my functions.

Any ideas as to what I doing wrong here?

Thanks!

John

推荐答案

在文章< 10 *************** @ proxy2.srv.ualberta .ca>,

John Hanley< jd ****** @ telusplanet.net>写道:
In article <10***************@proxy2.srv.ualberta.ca>,
John Hanley <jd******@telusplanet.net> wrote:
然而,当我构建我的程序时,DJGPP编译器告诉我每个函数有多个
定义。
Yet when I build my program, the DJGPP compiler tells me there are multiple
definitions of each of my functions.




.h文件中的*是什么?它应该只是函数原型,而不是完整的函数定义。


- Richard



What''s *in* the .h file? It should just be function prototypes, not
complete function definitions.

-- Richard


>我有一个程序,我的main.c和program.c文件都使用program.h
>I have a program where both my main.c and program.c files use the program.h
文件。所以我#include" program.h"在.c文件中。 program.h文件已经
#ifndef PROGRAM_H
#define PROGRAM_H
...
#endif

当我建立我的程序,DJGPP编译器告诉我每个函数有多个
定义。
file. So I #include "program.h" in both the .c files. The program.h file
has
#ifndef PROGRAM_H
#define PROGRAM_H
...
#endif

Yet when I build my program, the DJGPP compiler tells me there are multiple
definitions of each of my functions.




你把这些函数放在program.h中?这就是为什么。头文件通常用于函数PROTOTYPES,类型定义,#define,

以及有时全局变量(带有extern)。在头文件中定义一个函数

,然后将它包含在多个文件中,这些文件将被链接到同一个程序中,这是一个问题。你观察到,你得到了多个函数定义。


#ifndef只能在同一个编译单元中工作。例如,

它将保护您不包括program.h。两次,或来自

,包括program.h和foo.h其中包括program.h。它
不会保护你免受program.h的侵害。已被包含在

另一个编译单元中。编译器不是通灵的。它确实没有你知道你要将你已经完成的编译单元和当前的编译单元链接到同一个程序中。即使它是b / b
,也不是#ifndef的工作方式。你从每个编译单元开始时的

a clean slate开始。


Gordon L. Burditt



You put the functions in program.h? That''s why. Header files are
generally used for function PROTOTYPES, type definitions, #defines,
and sometimes global variables (with "extern"). Defining a function
in a header file, then including it in more than one file which
will be linked together into the same program, is a problem. As
you observed, you get multiple definitions of functions.

#ifndef only works within the same compilation unit. For example,
it WILL protect you from including "program.h" twice, or from
including "program.h" and "foo.h" which includes "program.h". It
will NOT protect you from "program.h" already being included in
another compilation unit. The compiler is not psychic. It does
not know that you are going to link that compilation unit you already
did and the current one together into the same program. And even if
it was, that''s not how #ifndef is supposed to work. You start with
a clean slate at the start of every compilation unit.

Gordon L. Burditt

< br>

" John Hanley" < JD ****** @ telusplanet.net>写道:
"John Hanley" <jd******@telusplanet.net> writes:
我有一个程序,我的main.c和program.c文件都使用program.h
文件。所以我#include" program.h"在.c文件中。 program.h文件已经
#ifndef PROGRAM_H
#define PROGRAM_H
...
#endif

当我建立我的程序,DJGPP编译器告诉我每个函数都有多个
定义。

我在这里做错了什么想法?
I have a program where both my main.c and program.c files use the program.h
file. So I #include "program.h" in both the .c files. The program.h file
has
#ifndef PROGRAM_H
#define PROGRAM_H
...
#endif

Yet when I build my program, the DJGPP compiler tells me there are multiple
definitions of each of my functions.

Any ideas as to what I doing wrong here?



是的,你在program.c的第137行输了一个错字。


说真的,我没有看到你的错发布。尝试

将你的程序缩小到一组非常小的文件(比如每个10-20

行)仍然存在问题。当你这样做时,你可能会发现这个问题;如果没有,请再次发布。


-

Keith Thompson(The_Other_Keith) ks***@mib.org < http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< *> < http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。



Yes, you''ve got a typo on line 137 of program.c.

Seriously, I don''t see anything wrong in what you''ve posted. Try
narrowing your program down to a set of very small files (say 10-20
lines each) that still exhibits the problem. You just might discover
the problem while you''re doing this; if not, post again.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


这篇关于使用#ifndef的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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