使用Swig

时间:2018-10-07 14:16:22

标签: python c++ swig

我是C ++新手,但尝试映射一个函数,该函数接受vector<string>的参数并使用Swig返回类型vector<double>,以便可以在Python中使用它。目前,此函数通过main()从文件的命令行执行中获取参数,但这只是将参数传递给我希望Python访问的函数(get_equities())。

我一直在努力解决这个问题,因为大多数示例都涉及映射类或非常基本的函数示例(例如swig tut),而且我也不确定文件引用其他文件的含义。通过包含另一个目录中的文件来包含其他文件中的类。

我正在跟踪示例here

包含我的函数的代码为myfile.cpp

#include "omp/EquityCalculator.h"
#include <iostream>
using namespace omp;
using namespace std;

// Function prototype (declaration)
vector<double> get_equities(vector<string> args);

int main(int argc, char *argv[])
{
    vector<string> args(argv, argv + argc);
    vector<double> equities;
    equities = get_equities(args);
};

vector<double> get_equities(vector<string> args) {

    int argc = args.size();

    string board_string = args[1];

    EquityCalculator eq;
    vector<CardRange> ranges;
    for( int i = 2; i < argc; i++ ) {
        ranges.push_back(args[i]);
    };

    uint64_t board = CardRange::getCardMask(board_string);

    eq.start(ranges, board);
    eq.wait();

    vector<double> result;

    auto r = eq.getResults();
    // 2 to 4; 2 to 3
    //todo: 
    for( int i = 2; i < argc; i++ ) {
        result.push_back(r.equity[i - 2]);
    }
    return result;
}

我的界面文件当前看起来像这样,我认为可能是错误的:

/* example.i */
%module example
#include "EquityCalculator.h" 
%include <std_string.i>
%include <std_vector.i>

%{
    /* Put header files here or function declarations like below */
    extern std::vector<std::double> get_equities(std::vector<std::string>);
%}

extern vector<double> get_equities(vector<string> args);

我用swig -c++ -python -o example_wrap.cxx example.i创建包装器。

这似乎可以,但是随后使用python3 setup.py build_ext --inplace运行setup.py进行以下操作:

#!/usr/bin/env python3

"""
setup.py file for SWIG example
"""

from distutils.core import setup, Extension


example_module = Extension('_example',
                           sources=['example_wrap.cxx', 'my_file.cpp'],
                           )

setup (name = 'example',
       version = '0.1',
       author      = "SWIG Docs",
       description = """Simple swig example from docs""",
       ext_modules = [example_module],
       py_modules = ["example"],
       )

有很多错误,例如:

In file included from example.cpp:1:
In file included from ./omp/EquityCalculator.h:4:
In file included from ./omp/CombinedRange.h:4:
In file included from ./omp/HandEvaluator.h:4:
./omp/Util.h:104:37: error: expected '(' for function-style cast or type construction
    if (alignment < OMP_ALIGNOF(void*))
                                ~~~~^
./omp/Util.h:95:36: note: expanded from macro 'OMP_ALIGNOF'
    #define OMP_ALIGNOF(x) alignof(x)
                                   ^
./omp/Util.h:104:21: error: expected expression
    if (alignment < OMP_ALIGNOF(void*))
                    ^
./omp/Util.h:95:37: note: expanded from macro 'OMP_ALIGNOF'
    #define OMP_ALIGNOF(x) alignof(x)
                                    ^
./omp/Util.h:105:37: error: expected '(' for function-style cast or type construction
        alignment = OMP_ALIGNOF(void*);
                                ~~~~^
./omp/Util.h:95:36: note: expanded from macro 'OMP_ALIGNOF'
    #define OMP_ALIGNOF(x) alignof(x)
                                   ^
./omp/Util.h:105:21: error: expected expression
        alignment = OMP_ALIGNOF(void*);

我在做什么错了?

0 个答案:

没有答案