代码之家  ›  专栏  ›  技术社区  ›  Vaaal88

C++中使用Python的Sigg的最小实例

  •  3
  • Vaaal88  · 技术社区  · 6 年前

    我试图编译一个Python包装的最简单例子,其中有些C++代码使用了OSX中的Sigg。

    /* File : example.c */
    double My_variable = 3.0;
    
    int fact(int n) {
        if (n <= 1) return 1;
        else return n*fact(n-1);
    }
    
    int my_mod(int x, int y) {
        return (x%y);
    }
    

    以及接口文件:

    /* example.i */
    %module example
    %{
        /* Put header files here or function declarations like below */
        extern int fact(int n);
        extern int my_mod(int x, int y);
    %}
    
    extern int fact(int n);
    extern int my_mod(int x, int y);
    

    我运行以下命令:

    swig -python -o example_wrap.c example.i 
    gcc -c -arch x86_64 -fPIC example.cxx -o example.o -I/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7
    ld -bundle -macosx_version_min 10.13 -flat_namespace -undefined suppress -o _example.so *.o
    

    然后我运行python2.7

    import example
    

    我得到了

    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "example.py", line 17, in <module>
        _example = swig_import_helper()
      File "example.py", line 16, in swig_import_helper
        return importlib.import_module('_example')
      File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
        __import__(name)
    ImportError: dynamic module does not define init function (init_example)
    

    1 回复  |  直到 6 年前
        1
  •  2
  •   Vaaal88    6 年前

    Swig -python example.i
    Gcc -fPIC -c example.c
    gcc -fPIC -c example_wrap.c -I/System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7/
    gcc -dynamiclib -o _example.so *.o -L/usr/lib/ -lpython2.7 -flat_namespace
    

    这里重要的是-flat\u名称空间。不知道为什么。

        2
  •  0
  •   Tim Seed    5 年前

    我也有这个问题,但我想使用我的python3.8(env安装)

    这个项目的布局是。。。

    布局

      /setup.py
      /example.i
      /example.h
      /example.c
      /Makefile.mak
    
    

    代码列表

    示例.h

    /* File: example.h */
    
    int fact(int n);
    
    

    /* File: example.i */
    %module example
    
    %{
    #define SWIG_FILE_WITH_INIT
    #include "example.h"
    %}
    
    int fact(int n);
    
    

    Makefile.mak文件

    
    obj = example_wrap.c _example.so
    
    .PHONY: all
    all : $(obj)
    
    clean:
        rm -f *.o
        rm -f *.so
        rm -f *wrap*.c*
        rm -Rf build
    
    example_wrap.c : example.i
        swig -python example.i
    
    _example.so: example.i
        python setup.py build_ext --inplace
    
    test:
        python -c "from _example import fact; print(f'Fact 10 is {fact(10)}')"
    
    

    示例.c

    /* File: example.c */
    
    #include "example.h"
    
    int fact(int n) {
        if (n < 0){ /* This should probably return an error, but this is simpler */
            return 0;
        }
        if (n == 0) {
            return 1;
        }
        else {
            /* testing for overflow would be a good idea here */
            return n * fact(n-1);
        }
    }
    
    
    

    #!/usr/bin/env python
    
    """
    setup.py file for SWIG example
    """
    
    from distutils.core import setup, Extension
    
    
    example_module = Extension('_example',
                               sources=['example_wrap.c', 'example.c'],
                               )
    
    setup (name = 'example',
           version = '0.1',
           author      = "SWIG Docs",
           description = """Simple swig example from docs""",
           ext_modules = [example_module],
           py_modules = ["example"],
           )
    
    

    建造

    • 切换到Python环境
    • 制作
    • 进行测试

    应该是这样。