Gopi Krishnan
Gopi Krishnan

Reputation: 104

Segmentation Fault (Core Dumped) when calling a python file from C++ .so

I am trying to generate C++ based .SO file along with wrapper by using swig. This .SO was generated to make a call from python(Ubuntu Environment).

It works well if i tried with Simple C++ code, but when i try to build with OpenCV, facing some issues.

opencvtest.cpp:

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main()
{
    Mat image;    
    image = imread("/home/swigtest/MyPic.jpg",1);   // Read the file
    namedWindow( "Display window", WINDOW_AUTOSIZE );// Create a window for display.
    imshow( "Display window", image );                   // Show our image inside it.
    destroyWindow("Display Window"); 
    waitKey(0);                                          // Wait for a keystroke in the window
    return 0;
}

opencvtest.i

 %module opencvtest
         %{
         /* Put header files here or function declarations like below */

         extern int main();

         %}

        extern int main();

So for Completed the Following steps:

  1. opencvtest.cpp - Sample code that i wrote.

  2. opencvtest.i - The corresponding intermediate file that i wrote

  3. Executing the command : swig -c++ -python opencvtest.i

  4. GCC command to create .o's(with wrapper) : g++ -fpic -c opencvtest.cpp opencvtest_wrap.cxx -I/usr/include/python2.7 -I/usr/local/include -I/usr/local/include/opencv -I/usr/local/include/opencv2

  5. Command to create .so : g++ -shared opencvtest.o opencvtest_wrap.o -o _opencvtest.so -L/usr/local/lib /usr/local/lib/libopencv_calib3d.so /usr/local/lib/libopencv_core.so /usr/local/lib/libopencv_features2d.so /usr/local/lib/libopencv_flann.so /usr/local/lib/libopencv_highgui.so /usr/local/lib/libopencv_imgcodecs.so /usr/local/lib/libopencv_imgproc.so /usr/local/lib/libopencv_ml.so /usr/local/lib/libopencv_objdetect.so /usr/local/lib/libopencv_photo.so /usr/local/lib/libopencv_shape.so /usr/local/lib/libopencv_stitching.so /usr/local/lib/libopencv_superres.so /usr/local/lib/libopencv_ts.a /usr/local/lib/libopencv_video.so /usr/local/lib/libopencv_videoio.so /usr/local/lib/libopencv_videostab.so

6.Generated .so file successfully

Then tried to test the python, followed the steps:

7.python

8.>>> import opencvtest

9.>>> opencvtest.main()

I got the error : Segmentation fault (core dumped)

Any one of the expert can help to clear the issue?

Upvotes: 0

Views: 1474

Answers (1)

Gopi Krishnan
Gopi Krishnan

Reputation: 104

Just changed the method name main in opencvtest.cpp.

Remember that change the method name at step 9 also.

It's working fine for me!.

But I don't know the process running behind this method call.

I changed the method name from main to one.

In Step 9: >>> opencvtest.one()

Upvotes: 2

Related Questions