SWIG typedef 识别 [英] SWIG typedef recognition

查看:43
本文介绍了SWIG typedef 识别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我也尝试在 PHP 中使用我的 C++ 类.在我的 C++ 代码中,我将 typedef 声明为:

I am trying too use my C++ class in PHP. in my C++ code I have declared the typedef as:

typedef unsigned char byte;

所以我打算让 SWIG 在包装类中考虑我的 typedef,我的接口文件是这样的:

so I intended to let SWIG consider my typedef in wrapper class, my interface file is something like this:

%module xxx

typedef unsigned char byte;

%include "xxx.h"

....

%{

typedef unsigned char byte;

#include "xxx.h"

%}

在我的测试代码中,我将类型称为:

and in my test code I refer to type as:

byte *data;

但我有以下错误:

Fatal error: Class 'unsigned_char' not found in xxx.php

P.S:我也在我的接口文件中包含了stdint.i",但得到了同样的错误

P.S: I also include "stdint.i" in my interface file but got the same error

有什么想法吗?

推荐答案

我可以确认您展示的界面是可行的并且适用于简单的情况,例如我写了下面的头文件来测试:

I can confirm that the interface you've shown is viable and works for simple cases, e.g. I wrote the following header file to test:

byte *make() { return NULL; }
void consume(byte *data) {}

并使用了接口:

%module xxx

typedef unsigned char byte;

%include "xxx.h"

%{
typedef unsigned char byte;

#include "xxx.h"
%}

我能够使用以下 PHP 编译和测试:

Which I was able to compile and test with the following PHP:

<?php
include("xxx.php");
$r = xxx::make();
xxx::consume($r);
?>

它按预期工作.

有几点需要注意:

  1. 一般来说,我倾向于编写您想要传递给模块的代码(即 %{ %} 中的位在 %include 之前.
  2. 我倾向于使用标准的 int 类型之一,而不是为 byte 使用您自己的 typedef,例如uint8_t
  3. 从你的问题中不清楚你打算如何使用 byte *data - 大概它是一个数组,在这种情况下你会想要 向您的界面添加更多代码.(或者最好仍然使用 std::vector 因为它是 C++):

  1. In general I would be inclined to write the code you want passed through to the module (i.e. the bits inside the %{ %} before your %include.
  2. Rather than using your own typedef for byte I'd be inclined to use one of the standard int types, e.g. uint8_t
  3. It's not clear from your question quite how you intend to use byte *data - presumably it's an array in which case you'll want to add a little more code to your interface. (Or better still use std::vector<byte> since it's C++):

%module xxx

%{
typedef unsigned char byte;

#include "xxx.h"
%}

%include <carrays.i>

%array_class(byte,ByteArray);

typedef unsigned char byte;

%include "xxx.h"

然后可以在 PHP 中用作:

Which can then be used in PHP as:

$a = new ByteArray(100);
$a->setitem(0, 1);
$a->setitem(1, 2); //...
xxx::consume($a->cast());

ByteArray 是 SWIG 提供的实用程序类,用于保存和包装 byte 的原始 C 数组.

ByteArray is a utility class provided by SWIG to hold and wrap a raw C array of bytes.

这篇关于SWIG typedef 识别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆