W
wanghz
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.
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.