M
Mug
i have quite curious problem on global variable on C++
suppose we have 3 files
-------------test.h----------------
#ifndef TEST_H
#define TEST_H
#include <stdio.h>
int a;
void whatever();
#endif
--------------------test.c --------------------
#include "test.h"
void whatever()
{
a=10;
printf("la la %d \n",a);
}
-----------------main.c-------------
#include "test.h"
int main()
{
a=9;
whatever();
return 0;
}
those files compile perfect with gcc,
with help of macro #ifndef
but with g++,it give me the mutiple defintion error on variable a.
i have to declare "a" as static to make it work.
so i have conclusion that the macro #ifndef dosen't works.
do someone can tell me why #ifndef doesn't work,and is there some
remedy for the problem?
Mug
suppose we have 3 files
-------------test.h----------------
#ifndef TEST_H
#define TEST_H
#include <stdio.h>
int a;
void whatever();
#endif
--------------------test.c --------------------
#include "test.h"
void whatever()
{
a=10;
printf("la la %d \n",a);
}
-----------------main.c-------------
#include "test.h"
int main()
{
a=9;
whatever();
return 0;
}
those files compile perfect with gcc,
with help of macro #ifndef
but with g++,it give me the mutiple defintion error on variable a.
i have to declare "a" as static to make it work.
so i have conclusion that the macro #ifndef dosen't works.
do someone can tell me why #ifndef doesn't work,and is there some
remedy for the problem?
Mug