0

I am new to programming, I try to use python to access the global variable defined in the compiled dynamic library which compiled in c++.

int acc;
void Cassie2d::Step(ControllerTorque* action)
{
  dyn_model_.setState(mj_data_->qpos, mj_data_->qvel);
  dyn_state_.UpdateDynamicState(&dyn_model_);

  mju_copy(mj_data_->ctrl, action->torques, nU);
  mj_step(mj_model_, mj_data_);
  acc = mj_data_->qacc;
  Render();
}

The code above is c++ code, I define a global variable (int acc) to access the mj data qacc, once I compiled the whole c++ code and form a .so library, I try to use the variable acc in my python code, however, the acc did not exist, is anyone could tell me the problem is?

Or is there any good way to define the global variable in which the python code could access the library and find the global variable?

guilt11
  • 85
  • 1
  • 7

1 Answers1

0

Usually, when you want to share a global variable across various files in C/C++ projects, you add a keyword extern in the declaration, in order to extend the visibility of the variable to the entire program you are developing.

extern int acc;

For what concerns accessing C++ libraries and their variables from Python, maybe this link could be helpful Calling C/C++ from Python?

danmar
  • 44
  • 7