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.
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) {
y.at(i) = sin(2*M_PI*i/360.0);
}
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) {
y.at(i) =
sin(2*M_PI*i/360.0);
}
plt::plot(x, y);
plt::show();
}
--------------------------------------------------
>g++ test.cpp -I/usr/include/python2.7 -lpython2.7
Any help would be appreciated.