swig 打包成 python包

  1. 安装

    win:  http://www.swig.org/download.html
    将解压目录,将swig.exe配置到PATH环境变量中
    在cmd中测试输入swig,出现‘Must specify an input file. Use -help for available options.’表示安装成功
    liunx : sudo apt install swig
    -y
  2. 打包命令

    swig -python -c++ example.i
    python setup.py build_ext --inplace

  3. 打包命令  

    gcc -c -fpic example.cpp example_wrap.cxx -I  /usr/local/include/  (python环境include的绝对路径  .h 文件路径)
    gcc -shared example.o example_wrap.o -o _example.so

example.i   头文件编写

%module SplzRadarFMT (模块名)
%{
#define SWIG_FILE_WITH_INIT  (固定写法)


#include "SplzRadarFMT.h"
#include "SplzRadarFMTInfo.h"
#include "SplzRadarFMTDef.h"   (引入所有.h 文件)
#include "Util.h"
#include "RadarError.h"
%}

%include "numpy.i"
%init %{             (固定写法)
import_array();
%}

%include "typemaps.i"
%include "cstring.i"  (固定写法)
%include "numpy.i"
                                          (根据 .h public 里 大写 C 开头的编写 )      
%cstring_bounded_output(char* ctime,24);  (输出ctime类型)--对应 .h 文件中--》 void GetTimeStamp(char* ctime);
%apply short *OUTPUT { short *snum };     (输出short类型)--对应 .h 文件中--》 void GetElNum(short* snum);
%apply float *OUTPUT { float *fnum };     (输出float类型)--对应 .h 文件中--》void GetRadarLat(float* fnum);
%apply (float* ARGOUT_ARRAY1,int DIM1) {(float* fdata,int len)}; 
                        (输出数组类型 DIM1 为长度)--对应 .h 文件中--》 int GetElList(float* fdata, int len);
%apply (float* IN_ARRAY1, int DIM1)     {(float* fDim1, int fDim1_len)};
                        (输入数组类型 DIM1 为长度)--对应 .h 文件中--》 int GetElList(float* fdata, int len);

                                          
%include "SplzRadarFMT.h"   (模块名, 和上面名字统一)

setup.py   编写

from distutils.core import setup, Extension
import numpy

SplzRadarFMT_module = Extension('_SplzRadarFMT',  # 模块名统一
                                # library_dirs=['./depends/lib/'],    # c 程序动态依赖指定目录
                                # libraries=['libbz2'],               # 动态库名字lib后缀的,用那个加那个, liunx下 都是libxxxx,
                                #                               理论上只需要填写去掉lib的部分,也就是只写xxxx就行,多个库文件逗号分隔
                                # extra_objects=['./depends64/lib/libbz2.dll'], # Linux的用到的静态连接库应该放到这个参数中,填写完整路径才行,不能只填写库名称(和动态库有区别)
                                # include_dirs=[numpy.get_include()],   # 报错  “numpy/arrayobject.h”: No such file or directory
                                sources=['SplzRadarFMT.cpp', 'SplzRadarFMT_wrap.cxx', ],
                                )
setup(name='SplzRadarFMT',  # 模块名统一
      version='0.1',
      author="ybhy",
      description=""" SplzRadarFMT - Sa/Sb bz2 file baseData read program """,
      ext_modules=[SplzRadarFMT_module],  # 模块名统一
      include_dirs=[numpy.get_include()],
      py_modules=["SplzRadarFMT"],  # 模块名统一
      )

  Liunx  打包so

  出现   “numpy/arrayobject.h”: No such file or directory

       解决   setup.py文件    添加    include_dirs=[np.get_include()]

  参考  https://blog.csdn.net/as472780551/article/details/83787882

      参考  https://blog.csdn.net/pxy7896/article/details/107104925

    https://blog.csdn.net/GEANNACAO/article/details/115582827

    https://www.cnblogs.com/guolongzheng/p/12145254.html

                    

原文地址:https://www.cnblogs.com/luochunxi/p/14688491.html