# Calling C++ method from Python

**URL:** <https://www.ruby-forum.com/t/calling-c-method-from-python/242590>\
**Category:** GNU Radio\
**Created:** [July 6, 2015, 4:10am UTC](https://www.ruby-forum.com/t/calling-c-method-from-python/242590 "2015-07-06T04:10:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Detlef\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/58f4c7/32.png) [@Detlef\_R](https://www.ruby-forum.com/u/Detlef_R)\
**Post date:** [July 6, 2015, 4:10am UTC](https://www.ruby-forum.com/t/calling-c-method-from-python/242590/1 "2015-07-06T04:10:22Z")

</div>

Hi,

I have worked my way through the Guided Tutorials on [gnuradio.org](http://gnuradio.org). I  
have written OOT blocks in Python and managed to expose some set methods  
in the block as callbacks. Now I have written an OOT C++ block  
sweeper\_cpp\_f and I want to expose a couple of setter methods in my  
block’s class as callbacks that can be invoked from Python. Initially I  
just defined the methods in my sweeper\_cpp\_f\_impl.cc file and added the  
following to the matching XML file:

set\_direction($direction)  
set\_trigger\_level($trigger\_level)

This builds fine and I am able to use the block in a GRC flow-graph. But  
at runtime when one of the above methods is invoked as a result of GUI  
input I get an error message:

File “/home/wpats/ettus/grc/new\_sweep\_cpp.py”, line 141, in  
set\_variable\_chooser\_direction  
self.tutorial\_sweeper\_cpp\_f\_0.set\_direction(self.variable\_chooser\_direction)  
AttributeError: ‘sweeper\_cpp\_f\_sptr’ object has no attribute  
‘set\_direction’

I looked at built in module sig\_source\_X\_impl.h.t and  
sig\_source\_X\_impl.cc.t (this is the analog signal source block) in  
gr-analog as an example for what I am trying to accomplish. I converted  
my sources into a similar format and modified the CMakeLists.txt in the  
…/gr-tutorial/lib directory to add:

########################################################################

# Invoke macro to generate various sources and headers

########################################################################  
include(GrMiscUtils)  
GR\_EXPAND\_X\_CC\_H(tutorial sweeper\_cpp\_X\_impl f)

This again builds fine after I edit the generate\_helper.py generated  
script to find an import module. I assumed that the above would do the  
necessary magic to generate the wrappers for my callback methods but no  
such luck. I still get the attribute error when invoking my C++ method.

What is the right way to do this ? Any help/suggestions/pointers would  
be appreciated. I can’t be the first one to try to do this seemingly  
simple task…

Thanks,

–Patrick

---

<div class="post-metadata">

**Author:** ![Patrick\_Sathyanatha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ecb155/32.png) [@Patrick\_Sathyanatha](https://www.ruby-forum.com/u/Patrick_Sathyanatha)\
**Post date:** [July 6, 2015, 9:54am UTC](https://www.ruby-forum.com/t/calling-c-method-from-python/242590/2 "2015-07-06T09:54:27Z")

</div>

Hi Patrick,

the typical problem with C++ blocks is that people add public methods to  
their \_impl, but forget to declare these same methods in their public  
non-impl class.  
Have you done the same expand magic with the header in your  
include/CMakeLists.txt?

Best regards,  
Marcus

---

<div class="post-metadata">

**Author:** ![Patrick\_Sathyanatha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ecb155/32.png) [@Patrick\_Sathyanatha](https://www.ruby-forum.com/u/Patrick_Sathyanatha)\
**Post date:** [July 7, 2015, 2:25am UTC](https://www.ruby-forum.com/t/calling-c-method-from-python/242590/3 "2015-07-07T02:25:23Z")

</div>

Thanks much, Marcus. That was the issue. I had missed adding the  
declarations for the callbacks in the non-impl class. As in the analog  
signal source block, just adding pure virtual function declarations for  
the callbacks did the trick.

I’m not familiar with SWIG and I don’t know why this works. But I’m  
happy it does without having to dig into SWIG details.

Thanks,

–Patrick
