discuss-gnuradio
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Discuss-gnuradio] Linking to an external library in an OOT c++ modu


From: Marcus Müller
Subject: Re: [Discuss-gnuradio] Linking to an external library in an OOT c++ module?
Date: Wed, 26 Jul 2017 23:37:31 +0200
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.2.1

Hi William,

generally, your CMake code looks right. So, I can't really rule out that the segfaults happens due to the fact that if you're running a python GNU Radio program, as you noted, your C++/python interface might be accessing python structures that the python runtime already uses. I honestly can't tell; maybe a backtrace would enlighten us (basically, `gdb --args python2 yourflowgraph.py`; `run` (crash); `backtrace`)? Generally, what you're doing might be a bit problematic; from the matplotlib-cpp README:

> This library is not thread safe. Protect all concurrent access with a mutex. Sadly, this is not easy to fix since it is not caused by the library itself but by the python interpreter, which is itself not thread-safe.

and general_work is running in a thread that is *separate* from the main python thread, whilst this looks like it assumes it is able to spawn the main python thread. To make matters worse, I can barely fathom what happens if c++->matplotlib->c++->Qt happens…

What's the other lib that causes segfaults?

Best regards,
Marcus


On 26.07.2017 23:05, William Johnson wrote:
I have been having some difficulty successfully using external libraries in a custom OOT module that I have not been able to understand.

I have had this issue with a few different external libraries.  The symptom is a segmentation fault when a function in the external library is called.  The below example is just representative and a bit circular as we are going from python->c++->python.

For this example, I would like to use matplotlib within a C++ OOT module to visualize some data during the debug of my module.  I understand that this is not something I would want to do within a gnu radio module during normal usage and that there are other ways to visualize the data.  

I am using matplotlib.cpp (https://github.com/lava/matplotlib-cpp) for this purpose.  This is simply a header file that requires linking to the python libraries.

To do this I have:

1. Included matplotlibcpp.h in my /lib folder.

2. In /lib/CMakeList.txt I have added the following:

find_package(PythonLibs 2.7)
include_directories(${Boost_INCLUDE_DIR} ${PYTHON_INCLUDE_DIRS} )
link_directories(${Boost_LIBRARY_DIRS}  ${PYTHON_LIBRARIES} )
...
target_link_libraries( gnuradio-mymodule   ${Boost_LIBRARIES} ${GNURADIO_ALL_LIBRARIES} ${PYTHON_LIBRARIES}) 


3. Within my general_work function I have added the following (as a test only):

#include <cmath>
#include "matplotlibcpp.h"
namespace plt = matplotlibcpp;
...
general_work(...
...
      // This just creates an interesting figure to plot.
      int n = 5000;
      std::vector<double> x(n), y(n), z(n), w(n,2);
      for(int i=0; i<n; ++i) {
              x.at(i) = i*i;
              y.at(i) = sin(2*M_PI*i/360.0);
              z.at(i) = log(i);
      }

      plt::plot(x, y);
      plt::show();
...

I am able to build successfully but when I run my qa_XYZ.py unit test I get a segmentation fault at the plt::plot(x,y) line.  removing these two plt:: lines results in no segmentation fault.  I have seen this same behavior when linking with other (not python) libraries for different reasons so I am suspecting that there is some fundamental issue with how I am configuring CMakeLists.txt or what I am trying to do is not possible for some other reason.

----
The following code runs without any issues on the same machine outside of gnuradio.
--------------------------------------------------
#include <cmath>
#include "matplotlibcpp.h"

namespace plt = matplotlibcpp;

int main() 
{
// Prepare data.
int n = 5000;
std::vector<double> x(n), y(n), z(n), w(n,2);
for(int i=0; i<n; ++i) {
x.at(i) = i*i;
y.at(i) = sin(2*M_PI*i/360.0);
z.at(i) = log(i);
}

plt::plot(x, y);
plt::show();
}
--------------------------------------------------
>g++ test.cpp -I/usr/include/python2.7 -lpython2.7

Any help would be appreciated.



_______________________________________________
Discuss-gnuradio mailing list
address@hidden
https://lists.gnu.org/mailman/listinfo/discuss-gnuradio


reply via email to

[Prev in Thread] Current Thread [Next in Thread]