【问题标题】:SIP Python/C++ binding fails to compile for simple exampleSIP Python/C++ 绑定无法编译为简单示例
【发布时间】:2012-05-25 12:03:47
【问题描述】:

使用 SIP 成功编译 Python/C 绑定后,我想用 Python/C++ 做同样的事情。由于某种原因,这不起作用。

这是文件:

fib.cpp

#include "fib.h"

int fib1(int n)
{
    if (n <= 0) {
        return 0;
    } else if (n <= 2) {
        return 1;
    } else {
        return fib1(n-1) + fib1(n-2);
    }
}

fib.h

int fib1(int n);

fib.sip

%Module fib

%Include fib.h

我运行以下命令来构建中间文件: sip -c . fib.sip

到目前为止一切正常。

现在我想使用 distutils 构建 .pyd 文件。

setup.py

from distutils.core import setup, Extension
import sipdistutils

setup(
  name = 'fib',
  versione = '1.0',
  ext_modules=[
    Extension("fib", ["fib.sip", "fib.cpp"]),
    ],

   cmdclass = {'build_ext': sipdistutils.build_ext}
 )

我运行以下命令: python setup.py build

这会失败并出现以下错误:

build\temp.win32-2.7\Release\sipfibcmodule.cpp:29:29: error: 'fib1' was not declared in this scope
error: command 'gcc' failed with exit status 1

可能是什么问题?顺便说一句,c++ 不应该用作编译器而不是 gcc 吗?

任何帮助表示赞赏!

亲切的问候

大卫

【问题讨论】:

  • c++ 是一种语言而不是编译器。 gcc 可以编译 C、C++ 等语言……见gcc.gnu.org
  • 感谢回复,我知道C++是一门语言,但它也是MinGW自带的编译C++代码的可执行文件。

标签: c++ python python-sip


【解决方案1】:

我找到了一个足够好的解决方案:命名空间。下次我会更好地阅读文档。我还应该提到,使用斐波那契类的解决方案可以解决问题,但我觉得这并不令人满意。

可以在下面找到文件的内容。

fib.cpp

#include "fib.h"

namespace test
{
    int fib1(int n)
    {
        if (n <= 0) {
            return 0;
        } else if (n <= 2) {
            return 1;
        } else {
            return fib1(n-1) + fib1(n-2);
        }
    }
}

fib.h

namespace test
{
    int fib1(int n);
}

fib.sip

%Module fib

namespace test {
    %TypeHeaderCode
    #include "fib.h"
    %End

    int fib1(int n);
};

setup.py - 没有任何改变

使用的命令与前面提到的完全相同。

【讨论】:

  • 您所做的是使用了另一种方法。公开方法link
  • 我已经很久没有使用这个了,但是如果您的链接解决方案有效,那就太好了。谢谢!
猜你喜欢
  • 2012-07-26
  • 2017-03-26
  • 1970-01-01
  • 2019-05-10
  • 2017-08-05
  • 1970-01-01
  • 1970-01-01
  • 2014-06-20
相关资源
最近更新 更多