Wrapping a C++ class in Python using SWIG

后端 未结 3 1442
旧时难觅i
旧时难觅i 2021-02-14 15:39

example.h:

#ifndef EXAMPLE_H
#define EXAMPLE_H

class Math {
 public:
    int pi() const;
    void pi(int pi);
 private:
    int _pi;
};

#endif         


        
3条回答
  •  情书的邮戳
    2021-02-14 16:12

    Thanks for your replay!

    The -C++ option generated the C++ class for the wrapper. swig -c++ -v -python example.swig

    I used g++ to compile the wrapper.

    g++ -fPIC -c example.cpp example_wrap.cxx -I/usr/local/include/python2.6/
    

    And the following command to buikd the shared object. Ofcourse, we need to remove the superflous includes (-I) and libraries (-L). The important flags are '-shared' and '-fPIC'.

    g++ example_wrap.o example.o -L/u01/app/oracle/product/1020.full/lib  -I/usr/local/ssl/include  -L/usr/local/ssl/lib -lclntsh -lssl -lcrypto -ldl -L/usr/local/lib -L/lib64 -L/usr/local/lib/python2.6/ -lboost_system -lboost_filesystem -lboost_thread -lboost_date_time -lglog -lmodpbase64 -lpthread -ldl -lrt -shared -fPIC -o _example.so
    

提交回复
热议问题