关于条件编译的问题 [英] Problems about conditional compilation

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

问题描述




我可以问一些有关条件汇总的问题吗?假设我

有三个简单的文件:ac,bc和ch


/ * -------- ac ------- - * /

#include< stdio.h>

#include" c.h"


int main ()

{

int temp = XXXX;

printf("%d \ n",temp);

f();

返回0;

}

/ * -------- bc ---- ----- * /

#include< stdio.h>

#include" c.h"


int f()

{

int temp = XXXX;

printf("%d \ n",temp);

返回0;

}

/ * ------- ch --------- * /

#ifndef C_H_

#define C_H_

#define XXXX 100

#else

#define XXXX 900

#endif


然后,当我用gcc ac bc一起编译它们时,结果显示:

100

100


我想知道为什么条件编译不起作用。我的意思是,当a.c和b.c分别包含

c.h时,XXXX应定义为

两个不同的值。这种理解有什么问题?


事实上,这个问题来自另一个问题。当我尝试在头文件ch中定义一个

函数时(我知道,这不是一个好的样式),

编译器抱怨该函数有重复的定义。

但是函数定义部分是由编译

指令包装的,所以它不应该被包含两次。


我对条件编译感到非常困惑。请帮助我或

给出一些线索以表明清楚。提前谢谢大家。

Hi,

Could I ask some questions about the conditional compilaion? Suppose I
have three simple files: a.c, b.c and c.h

/* --------a.c--------- */
#include <stdio.h>
#include "c.h"

int main()
{
int temp = XXXX;
printf("%d\n", temp);
f();
return 0;
}
/* --------b.c--------- */
#include <stdio.h>
#include "c.h"

int f()
{
int temp = XXXX;
printf("%d\n", temp);
return 0;
}
/* -------c.h--------- */
#ifndef C_H_
#define C_H_
#define XXXX 100
#else
#define XXXX 900
#endif

Then, when I compile them together by gcc a.c b.c, the result shows:
100
100

I wonder why the conditional compilation does not work. I mean, when
c.h is included by a.c and b.c respectively, XXXX should by defined as
two different values. What''s wrong with this understanding?

In fact, this problem comes from another one. When I try to define a
function in the header file c.h (I know, this is not a good style),
compiler complains there are duplicated definition for that function.
But the function definition section is wrapped by compilation
directives, so it should not be included twice at all.

I feel very confused about conditional compilation. Please help me or
give some clues to make it clear. Thank you all in advance.

推荐答案

wanghz写于11/09/05:
wanghz wrote on 11/09/05 :


我可以问一些有关条件汇总的问题吗?假设我有三个简单的文件:ac,bc和ch

/ * -------- ac --------- * /
#include< stdio.h>
#include" c.h"

int main()
{
int temp = XXXX;
printf("%d \ n",temp);
f();
返回0;
}

/ * ------ --bc --------- * /
#include< stdio.h>
#include" c.h"

int f()
{temp /> int temp = XXXX;
printf("%d \ n",temp);
返回0;
}

/ * ------- ch --------- * /
#ifndef C_H_
#define C_H_
#define XXXX 100
#否则
#define XXXX 900
#endif
然后,当我用gcc ac bc一起编译它们时,结果显示:
100
100


好​​。

我想知道为什么条件编译不起作用。


确实如此。

我的意思是,当ac和bc分别包括ac和bc时,


'''''''是重要的单词。

XXXX应定义为两个不同的值。这种理解有什么问题?


这都错了。防范技巧是针对多种内容,而不是针对多种定义的


实际上,这个问题来自另一个问题。当我尝试在头文件中定义
函数时ch


不要这样做。

(我知道,这不是一个好的风格),


这是一个很好的理由你要发现。

编译器抱怨该函数有重复的定义。


当然。

但是函数定义部分是由编译
指令包装的,所以它不应该包含两次。


为什么?


警卫可以防止这种情况发生:


/ * bh * /

< ...>

#include" a.h"

< ...>

/ * xxx.c * /

#include" a.h"

#include" b.h" / *第二次包含* /

我对条件编译感到非常困惑。请帮助我或者提供一些线索来说清楚。提前谢谢大家。
Hi,

Could I ask some questions about the conditional compilaion? Suppose I
have three simple files: a.c, b.c and c.h

/* --------a.c--------- */
#include <stdio.h>
#include "c.h"

int main()
{
int temp = XXXX;
printf("%d\n", temp);
f();
return 0;
}

/* --------b.c--------- */
#include <stdio.h>
#include "c.h"

int f()
{
int temp = XXXX;
printf("%d\n", temp);
return 0;
}

/* -------c.h--------- */
#ifndef C_H_
#define C_H_
#define XXXX 100
#else
#define XXXX 900
#endif

Then, when I compile them together by gcc a.c b.c, the result shows:
100
100
Fine.
I wonder why the conditional compilation does not work.
It does.
I mean, when
c.h is included by a.c and b.c respectively,
''respectively''''is the important word.
XXXX should by defined as
two different values. What''s wrong with this understanding?
It''s all wrong. The guard trick is against multiple inclusions, not
against multple definitions.
In fact, this problem comes from another one. When I try to define a
function in the header file c.h
Don''t do that.
(I know, this is not a good style),
for a good reason your are to discover.
compiler complains there are duplicated definition for that function.
Sure.
But the function definition section is wrapped by compilation
directives, so it should not be included twice at all.
Why ?

The guard protects against this :

/* b.h */
<...>
#include "a.h"
<...>
/* xxx.c */
#include "a.h"
#include "b.h" /* second inclusion */
I feel very confused about conditional compilation. Please help me or
give some clues to make it clear. Thank you all in advance.




这很简单。有一些使用规则:


- 标题不包含以下内容:


* #include指令

*公共宏定义

*公共常量定义

*公共类型定义

*公共结构定义

*公共工会定义

*公共内联函数定义[C99]

*公共函数声明(强烈推荐原型表格)

*公共对象声明(extern)


- 标题被保护(再次包含多个内容)


#ifndef H_XXX

#define H_XXX


/ *包含以上内容。 * /


#endif / *警卫* /


你可以烧掉规则,但不要来这里哭。

-

Emmanuel

C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html

C库: http://www.dinkumware.com/refxc.html


我曾经问过一位专家COBOL程序员,如何在COBOL中声明
声明局部变量,回复是:

什么是局部变量?



It''s quite simple. There are usage rules that are to be followed:

- a header contains not more that the following :

* #include directives
* public macros definitions
* public constants definitions
* public types definitions
* public structures definitions
* public unions definitions
* public inline functions definitions [C99]
* public functions declarations (prototyped form highly recommended)
* public object declarations (extern)

- A header is guarded (again multiple inclusions)

#ifndef H_XXX
#define H_XXX

/* include the above here. */

#endif /* guard */

You can burn the rules, but don''t come here an cry.
--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

I once asked an expert COBOL programmer, how to
declare local variables in COBOL, the reply was:
"what is a local variable?"


(取代< mn ******************** ***@YOURBRAnoos.fr>)


wanghz于11/09/05写道:
(supersedes <mn***********************@YOURBRAnoos.fr>)

wanghz wrote on 11/09/05 :


我可以吗?问有关条件汇总的一些问题?假设我有三个简单的文件:ac,bc和ch

/ * -------- ac --------- * /
#include< stdio.h>
#include" c.h"

int main()
{
int temp = XXXX;
printf("%d \ n",temp);
f();
返回0;
}

/ * ------ --bc --------- * /
#include< stdio.h>
#include" c.h"

int f()
{temp /> int temp = XXXX;
printf("%d \ n",temp);
返回0;
}

/ * ------- ch --------- * /
#ifndef C_H_
#define C_H_
#define XXXX 100
#否则
#define XXXX 900
#endif
然后,当我用gcc ac bc一起编译它们时,结果显示:
100
100


好​​。

我想知道为什么条件编译不起作用。


确实如此。

我的意思是,当ac和bc分别包括ac和bc时,


'''''''是重要的单词。

XXXX应定义为两个不同的值。这种理解有什么问题?


这都错了。防范技巧是针对多个*包含*,而不是多个*定义*的b $ b。

实际上,这个问题来自另一个问题。当我尝试在头文件中定义
函数时ch


不要这样做。

(我知道,这不是一个好的风格),


因为你要发现的好理由。

编译器抱怨该功能有重复的定义。


当然。

但是函数定义部分是由编译
指令包装的,所以它不应该包含两次。


为什么?


警卫可以防止这种情况发生:


/ * bh * /

< ...>

#include" a.h"

< ...>


/ * xxx.c * /

#include" a.h"

#include" b.h" / *第二次包含* /

我对条件编译感到非常困惑。请帮助我或者提供一些线索来说清楚。提前谢谢大家。
Hi,

Could I ask some questions about the conditional compilaion? Suppose I
have three simple files: a.c, b.c and c.h

/* --------a.c--------- */
#include <stdio.h>
#include "c.h"

int main()
{
int temp = XXXX;
printf("%d\n", temp);
f();
return 0;
}

/* --------b.c--------- */
#include <stdio.h>
#include "c.h"

int f()
{
int temp = XXXX;
printf("%d\n", temp);
return 0;
}

/* -------c.h--------- */
#ifndef C_H_
#define C_H_
#define XXXX 100
#else
#define XXXX 900
#endif

Then, when I compile them together by gcc a.c b.c, the result shows:
100
100
Fine.
I wonder why the conditional compilation does not work.
It does.
I mean, when
c.h is included by a.c and b.c respectively,
''respectively''''is the important word.
XXXX should by defined as
two different values. What''s wrong with this understanding?
It''s all wrong. The guard trick is against multiple *inclusions*, not
against multiple *definitions*.
In fact, this problem comes from another one. When I try to define a
function in the header file c.h
Don''t do that.
(I know, this is not a good style),
for good reasons your are to discover.
compiler complains there are duplicated definition for that function.
Sure.
But the function definition section is wrapped by compilation
directives, so it should not be included twice at all.
Why ?

The guard protects against this :

/* b.h */
<...>
#include "a.h"
<...>

/* xxx.c */
#include "a.h"
#include "b.h" /* second inclusion */
I feel very confused about conditional compilation. Please help me or
give some clues to make it clear. Thank you all in advance.




这很简单。有一些使用规则:


- 标题不包含以下内容(按此顺序):


* #include指令

*公共宏定义

*公共常量定义

*公共类型定义

*公共结构定义

*公共工会定义

*公共内联函数定义[C99]

*公共函数声明(强烈建议使用原型)

*公共对象声明(extern)


- 标题被保护(针对多个内容)


#ifndef H_XXX

#define H_XXX


/ *包括上面的内容。 * /


#endif / *警卫* /


你可以烧掉规则,但不要来这里哭。


-

Emmanuel

C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
C库: http://www.dinkumware.com/ refxc.html


我曾经问过专家COBOL程序员,如何在COBOL中声明
声明局部变量,回复是:

什么是局部变量?



It''s quite simple. There are usage rules that are to be followed:

- A header contains not more that the following (in that order):

* #include directives
* public macros definitions
* public constants definitions
* public types definitions
* public structures definitions
* public unions definitions
* public inline functions definitions [C99]
* public functions declarations (prototyped form highly recommended)
* public object declarations (extern)

- A header is guarded (against multiple inclusions)

#ifndef H_XXX
#define H_XXX

/* include the above here. */

#endif /* guard */

You can burn out the rules, but don''t come here an cry.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

I once asked an expert COBOL programmer, how to
declare local variables in COBOL, the reply was:
"what is a local variable?"


wanghz写道:

我可以问一些有关条件汇总的问题吗?假设我有三个简单的文件:ac,bc和ch

/ * -------- ac --------- * /
#include< stdio.h>
#include" c.h"

int main()
{
int temp = XXXX;
printf("%d \ n",temp);
f();
返回0;
}

/ * ------ --bc --------- * /
#include< stdio.h>
#include" c.h"

int f()
{
int temp = XXXX;
printf("%d \ n",temp);
返回0;
}

/ * ------- ch --------- * /
#ifndef C_H_
#define C_H_
#define XXXX 100
#否则
#define XXXX 900
#endif
然后,当我用gcc ac bc一起编译它们时,结果显示:
100
100

我想知道为什么条件编译不起作用。我的意思是,当a.c和b.c分别包含
c.h时,XXXX应该定义为两个不同的值。这种理解有什么问题?

实际上,这个问题来自另一个问题。当我尝试在头文件ch中定义一个
函数时(我知道,这不是一个好的样式),
编译器抱怨该函数有重复的定义。
但函数定义section由编译
指令包装,因此它不应该被包含两次。

我对条件编译感到非常困惑。请帮助我或者提供一些线索来说清楚。提前谢谢大家。
Hi,

Could I ask some questions about the conditional compilaion? Suppose I
have three simple files: a.c, b.c and c.h

/* --------a.c--------- */
#include <stdio.h>
#include "c.h"

int main()
{
int temp = XXXX;
printf("%d\n", temp);
f();
return 0;
}
/* --------b.c--------- */
#include <stdio.h>
#include "c.h"

int f()
{
int temp = XXXX;
printf("%d\n", temp);
return 0;
}
/* -------c.h--------- */
#ifndef C_H_
#define C_H_
#define XXXX 100
#else
#define XXXX 900
#endif

Then, when I compile them together by gcc a.c b.c, the result shows:
100
100

I wonder why the conditional compilation does not work. I mean, when
c.h is included by a.c and b.c respectively, XXXX should by defined as
two different values. What''s wrong with this understanding?

In fact, this problem comes from another one. When I try to define a
function in the header file c.h (I know, this is not a good style),
compiler complains there are duplicated definition for that function.
But the function definition section is wrapped by compilation
directives, so it should not be included twice at all.

I feel very confused about conditional compilation. Please help me or
give some clues to make it clear. Thank you all in advance.




gcc将每个.c文件编译为单个程序。 (不知道一个正确的

名称,如果你知道一个,请纠正我),所以a.c中的包含不会b / b
干扰b.c中的包含,反之亦然。 程序将

然后链接到完整的可执行文件。


因此在包含ch之前从未定义C_H_因此它总是

将XXXX定义为100.这同样适用于您的功能问题。


HTH



gcc compiles every .c file as a single "program" (dont know a proper
name, correct me please if you know one), so an include in a.c does not
interfere with an include in b.c and vice versa. The "programs" will
then be linked together to the complete executable.

So C_H_ is never defined before you include c.h and hence it always
defines XXXX to be 100. The same applies to your function problem.

HTH


这篇关于关于条件编译的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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