I am trying to call c++ function from c on Linux(Centos 6.9) and GCC version is 2.96 and optimization is off
on windows, function working perfectly. However, on Linux, address of global variable changed and working bad.
Animal.h
class CAnimal
{
public:
void CDoSomething(const int ID);
};
#ifdef __cplusplus
extern "C"{
#endif //__cplusplus
void DoSomething(const int ID);
#ifdef __cplusplus
}
#endif //__cplusplus
Animal.cpp
extern Animal[2048];
void CAnimal::CDoSomething(const int ID)
{
// Animal is global
// Animal address changed!!
// In this, Animal[ID] address 0x0100
// working bad!
Animal[ID].something;
}
void DoSomething(const int ID)
{
CAnimal animal;
animal.CDoSomething(ID);
}
object.h
typedef struct stANIMAl
{
int a;
char* b;
and etc..
}ANIMAL;
ANIMAL Animal[2048];
func.h
void Do(void);
func.c
extern Animal[2048];
void Do(void)
{
// Animal[ID] address 0x0000
DoSomething(0);
}