# I wish to create a GNU Radio program in C++ that doesn't need GRC or Python, is there a reference or template for this?

I want to create a functionally equivalent C++ file to the Python top_block.py file that is generated by GRC when executing a (blank example) flow chart.

I know that C++ can be used to define blocks specifically, and so I would assume that by creating a C++ file and including and using the methods from defined blocks through the relevant header files would allow for, essentially, the creation of GNURadio programs without the use of GRC or Python.

Has anyone created similar programs to this? Is there any chance I can make GRC generate such a file, or can someone point me to reference programs / documentation?

At it's core my question is how would I make the following Python file in C++?

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
##################################################
# GNU Radio Python Flow Graph
# Title: Top Block
# Generated: Wed Oct 16 17:45:58 2019
##################################################

if __name__ == '__main__':
import ctypes
import sys
if sys.platform.startswith('linux'):
try:
except:

from PyQt4 import Qt
from optparse import OptionParser
import sys

class top_block(gr.top_block, Qt.QWidget):

def __init__(self):
gr.top_block.__init__(self, "Top Block")
Qt.QWidget.__init__(self)
self.setWindowTitle("Top Block")
try:
except:
pass
self.top_scroll_layout = Qt.QVBoxLayout()
self.setLayout(self.top_scroll_layout)
self.top_scroll = Qt.QScrollArea()
self.top_scroll.setFrameStyle(Qt.QFrame.NoFrame)
self.top_scroll.setWidgetResizable(True)
self.top_widget = Qt.QWidget()
self.top_scroll.setWidget(self.top_widget)
self.top_layout = Qt.QVBoxLayout(self.top_widget)
self.top_grid_layout = Qt.QGridLayout()

self.restoreGeometry(self.settings.value("geometry").toByteArray())

##################################################
# Variables
##################################################
self.samp_rate = samp_rate = 32000

##################################################
# Blocks
##################################################

def closeEvent(self, event):
self.settings.setValue("geometry", self.saveGeometry())
event.accept()

def get_samp_rate(self):
return self.samp_rate

def set_samp_rate(self, samp_rate):
self.samp_rate = samp_rate

def main(top_block_cls=top_block, options=None):

from distutils.version import StrictVersion
if StrictVersion(Qt.qVersion()) >= StrictVersion("4.5.0"):
style = gr.prefs().get_string('qtgui', 'style', 'raster')
Qt.QApplication.setGraphicsSystem(style)
qapp = Qt.QApplication(sys.argv)

tb = top_block_cls()
tb.start()
tb.show()

def quitting():
tb.stop()
tb.wait()
qapp.exec_()

if __name__ == '__main__':
main()


Apologies as I am unsure if this question is better suited here or on stack overflow, feel free to mark it for migration if it is not appropriate here!

• Hi! Not so sure this isn't more of a programming question than a signal processing problem, but sure: gr::top_block is, like everything else, a C++ class. Its usage in C++ is pretty similar to in Python. (the python things are mostly just automatically wrapped C++.) Learning to use Qt will, however, take a bit of effort. – Marcus Müller Oct 17 '19 at 19:32
• by the way, if you have a current (as in: 3.8, or mostly in 3.7.13.5), GRC can generate C++ source code – by far not for all blocks, but hey, you asked for a template :) – Marcus Müller Oct 17 '19 at 19:42
• However: You don't need GRC to execute this python file you're showing, and implementing the same in C++ has zero performance advantage. I'd recommend joining the GNU Radio discuss mailing list and explaining your use case there. – Marcus Müller Oct 17 '19 at 19:42
• Hi Marcus, thanks for your comments! What you've said makes sense, and I can generate C++ code blocks using modtool, but I guess I am asking if there is an accepted way to run a C++ code block without converting it to a Python script or yaml? My use case is that I wish to get the output of a flowgraph from an external C++ program by calling some run/execute function without having to use up memory overhead from running programs in C++ and Python simultaneously. I will do as you suggest and ask on the mailing list! Thanks again! – madmissmolly Oct 18 '19 at 13:45

you can create flowgraph in c++ like gqrx and i think GNU Radio Manual and C++ API Reference documents will help you. This is gnuradio c++ document for top_block if you could not use it let me know to write a sample for you. These documents are for gnuradio version 3.7 and in gnuradio 3.8 there is c++ code generation and gnuradio blocks are porting to c++ and you can track them in this link. i hope it helps.

• Hi Fonqri! Thanks for your reply, as it turns out the version of Ubuntu I have (16.4) and the latest release of GNU Radio (which allowed for the generation of C++ rather than Python) were incompatible which is what caused the issue I was having where I could either a) Have an earlier GNU Radio version (3.7) installed that opened and ran but lacked the C++ option or b) Have the latest release of GNU Radio (3.8) that should allow C++ but did not compile correctly on my PC. So you are completely correct here but it turned out my issue was not where I thought it was! – madmissmolly Jan 17 at 10:13

Just to clarify the issue I was having and the outcome for future reference:

I was using Ubuntu 16.04 and trying to compile GNU Radio 3.8. Although GRC was present as an executable it did not open/run. This turned out to be because of some Python incompatibility which is referenced briefly on the UbuntuInstall section of the GNU Radio Wiki:

"Building GNU Radio 3.8.x is difficult on Ubuntu 16.04 due to various dependency issues (especially due to CMake and Python versions). Please consider using GNU Radio 3.7.x instead."

I was able to download GNU Radio 3.7 and run GRC with no issues but it didn't provide the C++ code generation I needed for my application.

Because 3.8 compiled and didn't throw any obvious errors (GRC would appear to run but open no GUI) and the C++ generation code was present, I had assumed (falsely) that the new version was working as intended and that the C++ code generation was done without the GRC GUI.

Once I had found some forum posts where others discussed the Ubuntu 16.04 and GNU Radio 3.8 compatibility issue I discovered it was in fact due to Python pathing errors and solving them was beyond the scope of my current project (as in the time required to fix the bug was not worth the added benefit of GRC for my project in particular).

Thanks to all who answered and commented!

• GRC= GNU Radio Companion. That's just python, and doesn't need ot be compiled. What you mean is "I tried to compile GNU Radio 3.8". And, no, that won't work on the obsolete Ubuntu 16.04, because a lot of the libraries that we depend on simply aren't available or hopelessly outdated on Ubuntu 16.04, so please update your Ubuntu to something less ancient. Then, you can just directly install GNU Radio 3.8 as a binary package through the GNU Radio Releases PPA. – Marcus Müller Jan 17 at 11:51
• "Because 3.8 compiled and didn't throw any obvious errors" <--- as the GNU Radio project maintainer, I know that the build process will accurately tell you that it disables most of the functionality due to outdated/missing dependencies. – Marcus Müller Jan 17 at 11:52
• Apologies for any incorrect phrasing! As I said in my original post I was completely new to GNU Radio and GRC so any mix ups are on me! The computer on which I use Ubuntu 16.04 is my work one so it's not really in my power to change it, but thanks anyway! – madmissmolly Jan 17 at 12:03