TyWEFF不与SWIG(Python包装C++代码)一起工作

2024-10-02 04:30:50 发布

您现在位置:Python中文网/ 问答频道 /正文

您好,感谢您的帮助!在

我正在编写一个Python包装器(Sigg 2 + Python 2.7),用于C++代码。C++代码中有我需要在Python包装器中访问的Type。不幸的是,我在执行Python代码时遇到以下错误:

 tag = CNInt32(0)
   NameError: global name 'CNInt32' is not defined

我查看了SWIG文档的第5.3.5节,其中解释了size_t是typedef,但我不能让它起作用。在

以下是重现错误的简单代码:

C++头:< /H1> ^{pr2}$

C++源:

/* File : example.cpp */
#include "example.h"
#include <iostream>
using namespace std;

/* I'm a file containing use of typedef variables */
ExampleClass::ExampleClass() {
}

ExampleClass::~ExampleClass() {
}

void ExampleClass::printFunction  (int value) {
cout << "Value = "<< value << endl;
}

void ExampleClass::updateInt(CNInt32& var) {
  var = 10;
}

接口文件:

/* File : example.i */
%module example

typedef int CNInt32;  

%{
    #include "example.h"
%}

%include <windows.i>
%include "example.h"  

Python代码:

# file: runme.py  
from example import *

# Try to set the values of some typedef variables

exampleObj = ExampleClass()
exampleObj.printFunction (20)

var = CNInt32(5)
exampleObj.updateInt (var)

再次感谢你的帮助。在

桑托什


Tags: of代码includeexamplevar错误variablesfile
1条回答
网友
1楼 · 发布于 2024-10-02 04:30:50

我成功了。我不得不在接口文件中使用类型映射,见下文:
-非常感谢Swig邮件列表上的“David Froger”。
-另外,感谢doctorlove的初步提示。在

%include typemaps.i
%apply CNInt32& INOUT { CNInt32& };

然后在python文件中:

^{pr2}$

再次感谢!
桑托什

相关问题 更多 >

    热门问题