gpt4 book ai didi

java - 在 SWIG 接口(interface)上找不到 C++ 类

转载 作者:太空宇宙 更新时间:2023-11-04 11:49:06 24 4
gpt4 key购买 nike

大家早上好

我发现要使 SWIG 接口(interface)适用于 C++ 会遇到麻烦。我有几个 .cpp 和 .h 文件,我只想为其中的几个文件创建一个接口(interface)(我将在我的 Java 代码中使用),因此我的 .i 文件如下所示:

/* File : AlgoritmoElectrico.i */
%module alg

/* Header files that are referred in the ones I want to create the interface with */
%{
#include "AlgoritmoElectrico.h"
#include "Proyecto.h"
#include "Indice.h"

/* ... I skipped a few to make it shorter ... */

#include "ParserTime.h"
%}

/* Header files of classes I want to use in Java */
%include "AlgoritmoElectrico.h"
%include "AlgoritmoElectrico.h"

所以我运行了 swig -c++ -java AlgoritmoElectrico.i 并得到了几个 .java 文件,加上 .cxx 包装器,我用 javac *.java< 编译了所有的 .java 文件 并使用 native 代码和包装器代码创建了 .so 库。

我的 Java 代码如下所示:

package mr;

/* ... Stuff ... */

public class MRAlgoritmo {

public static class Map extends Mapper<LongWritable, Text, IntWritable, Text> {
public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
// Obtiene instante y circulaciones
Pattern pattern = Pattern.compile("\t[ ||| ]"); // FIXME revisar regex
String[] info = pattern.split(value.toString());

// Captura datos de proyecto
System.loadLibrary("algoritmo");
Proyecto proyecto = new Proyecto("Proyecto1");
proyecto.ReadFile("infraestructura");
proyecto.getParametros().setIntervalo(1);

// Ejecuta algortimo con datos de circulaciones
AlgoritmoElectrico algoritmo = new AlgoritmoElectrico(proyecto);
String [] resultados = algoritmo.Ejecutar(info);

/* ... stuff ... */
}
}



public static void main(String[] args) throws Exception {
/* ... stuff not related with the above, working with Hadoop MR ... */
}

}

ProyectoAlgoritmoElectrico 是 C++ 类,但未找到它们。想法?

谢谢!!

最佳答案

我发现了问题,这是一个打包错误。我强制 swig 向 .java 文件添加一个包语句,然后导入工作正常

我用了swig -c++ -java -package <package_name> <files> ,然后我编译并构建了保留包结构的 jar。之后我可以像往常一样引用它的内容并且它工作正常。

关于java - 在 SWIG 接口(interface)上找不到 C++ 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19023631/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com