简体   繁体   English

SWIG C ++结构到Java

[英]SWIG C++ structure to java

I'm trying to get a simple SWIG example to work that uses a struct as a return type, but my generated file is incorrect. 我试图使一个简单的SWIG示例工作,该示例使用结构作为返回类型,但是我生成的文件不正确。 My files look like this. 我的文件如下所示。

SwigTest.h
#pragma once
#include "MyHeader.h"
class SwigTest
{
public:
    MyHeader testMe();
};

MyHeader.h
struct MyHeader {
    int x;
}

and my swig interface file is: 我的swig接口文件是:

%module MyModule
%{
#include "SwigTest.h"
#include "MyHeader.h"
%}
extern MyHeader testMe();

The resulting JNI file has the following method declaration 生成的JNI文件具有以下方法声明

public class MyModuleJNI {
  public final static native long testMe();
}

If my method returns a primitive, it works fine, but not with the struct. 如果我的方法返回一个原语,则可以正常工作,但不能与struct一起工作。 I'm running on windows with swig.exe -java -c++ MyModule.i 我在Windows上使用swig.exe -java -c++ MyModule.i

EDIT: I think I need to declare a struct in the .i file as well. 编辑:我想我也需要在.i文件中声明一个结构。 Could someone confirm (or dispute) that? 有人可以确认(或提出异议)吗? Thanks. 谢谢。

thanks, Jeff 谢谢,杰夫

Yes you need to declare the struct in the interface file as well. 是的,您还需要在接口文件中声明该结构。

Try this: 尝试这个:

%module MyModule
%{
#include "MyHeader.h"
#include "SwigTest.h"
%}

%include "MyHeader.h"
%include "SwigTest.h"

Also its safer to declare the struct before the code that makes use of it. 在使用该结构的代码之前声明该结构也更安全。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM