简体   繁体   中英

How to access the global variable defined in compiled dynamic library from c++

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?

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM