简体   繁体   English

如何从C ++访问已编译动态库中定义的全局变量

[英]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++. 我是编程新手,我尝试使用python访问在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? 上面的代码是c ++代码,我定义了一个全局变量(int acc)以访问mj数据qacc,一旦编译了整个c ++代码并形成了.so库,我便尝试在我的python代码中使用变量acc。 ,该acc不存在,有人可以告诉我问题是什么吗?

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? 还是有什么好的方法来定义全局变量,以便python代码可以访问该库并找到全局变量?

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. 通常,当您想在C / C ++项目中的各个文件之间共享全局变量时,可以在声明中添加关键字extern ,以便将变量的可见性扩展到正在开发的整个程序中。

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? 对于从Python访问C ++库及其变量的问题,也许此链接可能对从Python调用C / C ++有用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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